49 lines
2.1 KiB
Text
49 lines
2.1 KiB
Text
|
|
[Char = String] CH2NUM
|
|||
|
|
L(chars) ‘abc def ghi jkl mno pqrs tuv wxyz’.split(‘ ’)
|
|||
|
|
V num = L.index + 2
|
|||
|
|
L(ch) chars
|
|||
|
|
CH2NUM[ch] = String(num)
|
|||
|
|
|
|||
|
|
F mapnum2words(words)
|
|||
|
|
DefaultDict[String, [String]] number2words
|
|||
|
|
V reject = 0
|
|||
|
|
L(word) words
|
|||
|
|
X.try
|
|||
|
|
number2words[word.map(ch -> :CH2NUM[ch]).join(‘’)].append(word)
|
|||
|
|
X.catch KeyError
|
|||
|
|
reject++
|
|||
|
|
R (number2words, reject)
|
|||
|
|
|
|||
|
|
V words = File(‘unixdict.txt’).read().rtrim("\n").split("\n")
|
|||
|
|
print(‘Read #. words from 'unixdict.txt'’.format(words.len))
|
|||
|
|
V wordset = Set(words)
|
|||
|
|
V (num2words, reject) = mapnum2words(words)
|
|||
|
|
|
|||
|
|
F interactiveconversions()
|
|||
|
|
L(inp) (‘rosetta’, ‘code’, ‘2468’, ‘3579’)
|
|||
|
|
print("\nType a number or a word to get the translation and textonyms: "inp)
|
|||
|
|
I all(inp.map(ch -> ch C ‘23456789’))
|
|||
|
|
I inp C :num2words
|
|||
|
|
print(‘ Number #. has the following textonyms in the dictionary: #.’.format(inp, (:num2words[inp]).join(‘, ’)))
|
|||
|
|
E
|
|||
|
|
print(‘ Number #. has no textonyms in the dictionary.’.format(inp))
|
|||
|
|
E I all(inp.map(ch -> ch C :CH2NUM))
|
|||
|
|
V num = inp.map(ch -> :CH2NUM[ch]).join(‘’)
|
|||
|
|
print(‘ Word #. is#. in the dictionary and is number #. with textonyms: #.’.format(inp, (I inp C :wordset {‘’} E ‘n't’), num, (:num2words[num]).join(‘, ’)))
|
|||
|
|
E
|
|||
|
|
print(‘ I don't understand '#.'’.format(inp))
|
|||
|
|
|
|||
|
|
V morethan1word = sum(num2words.keys().filter(w -> :num2words[w].len > 1).map(w -> 1))
|
|||
|
|
V maxwordpernum = max(num2words.values().map(values -> values.len))
|
|||
|
|
print(‘
|
|||
|
|
There are #. words in #. which can be represented by the Textonyms mapping.
|
|||
|
|
They require #. digit combinations to represent them.
|
|||
|
|
#. digit combinations represent Textonyms.’.format(words.len - reject, ‘'unixdict.txt'’, num2words.len, morethan1word))
|
|||
|
|
|
|||
|
|
print("\nThe numbers mapping to the most words map to #. words each:".format(maxwordpernum))
|
|||
|
|
V maxwpn = sorted(num2words.filter((key, val) -> val.len == :maxwordpernum))
|
|||
|
|
L(num, wrds) maxwpn
|
|||
|
|
print(‘ #. maps to: #.’.format(num, wrds.join(‘, ’)))
|
|||
|
|
|
|||
|
|
interactiveconversions()
|