Running this script:
import java.util.regex.Pattern
keypadMap = [0:"", 1:"", 2:"abc", 3:"def", 4:"ghi",
5:"jkl", 6:"mno", 7:"pqrs", 8:"tuv", 9:"wxyz"]
dictionary = new File("words")
regexBuilder = { digit -> '[' + keypadMap[digit.toInteger()] + ']' }
patternBuilder = { number ->
Pattern.compile('^' + number.collect(regexBuilder).join() + '$') }
def matchWords(phNumber) {
def p = patternBuilder(phNumber)
def matches = []
dictionary.eachLine{ line -> if (p.matcher(line).matches()) matches << line }
if (matches)
println "Matches for $phNumber: " + matches.join(' ')
else
println "No matches found for $phNumber"
}
def matchPairs(phNumber) {
def p1 = patternBuilder(phNumber[0..2])
def p2 = patternBuilder(phNumber[3..6])
def first = [], second = []
dictionary.eachLine { line ->
if (p1.matcher(line).matches()) first << line
if (p2.matcher(line).matches()) second << line
}
if (first && second)
println "$phNumber matches word pairs: " +
[first, second].combinations().collect{ it.join '-' }.join(', ')
else
println "Couldn't find word pair for $phNumber"
}
[ '2345678', '2455466', '2276228', '6662342', '2476642',
'2742742', '7338433', '7678243', '3742253', '7264253',
'7683353', '7684453', '7837453', '7378453', '3749953',
'7877363', '2668363', '5367273', '7377473', '7877673',
'7278873', '2646283', '3377483'
].each{ matchWords it }
println '----------------'
[ '8765432', '9689474', '2896673', '6847687', '9687283', '2283278'
].each{ matchPairs it }
|
against the attached words file yields these results:
No matches found for 2345678 Matches for 2455466: billion Matches for 2276228: acrobat Matches for 6662342: monadic nomadic Matches for 2476642: chromic chronic Matches for 2742742: aphasia aphasic Matches for 7338433: refugee seethed Matches for 7678243: portage postage Matches for 3742253: dribble friable Matches for 7264253: panicle sanicle Matches for 7683353: snuffle souffle Matches for 7684453: smuggle snuggle Matches for 7837453: puerile sterile Matches for 7378453: reptile servile Matches for 3749953: drizzle frizzle Matches for 7877363: supreme suspend Matches for 2668363: contend convene Matches for 5367273: jeopard leopard Matches for 7377473: reprise respire Matches for 7877673: purpose suppose Matches for 7278873: pasture rapture Matches for 2646283: animate cognate Matches for 3377483: deprive despite ferrite ---------------- Couldn't find word pair for 8765432 9689474 matches word pairs: you-wish 2896673 matches word pairs: buy-more, buy-nose 6847687 matches word pairs: mug-pour, mug-soup, mug-sour 9687283 matches word pairs: you-pate, you-pave, you-rate, you-rave, you-save, you-scud 2283278 matches word pairs: act-dart, bat-dart, cat-dart, act-east, bat-east, cat-east, act-fast, bat-fast, cat-fast |