Textonyms: Difference between revisions

3,199 bytes added ,  3 years ago
Added 11l
m (Swift - simplified code)
(Added 11l)
Line 43:
{{Template:Strings}}
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>[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()</lang>
 
{{out}}
<pre>
Read 25104 words from 'unixdict.txt'
 
There are 24978 words in 'unixdict.txt' which can be represented by the Textonyms mapping.
They require 22903 digit combinations to represent them.
1473 digit combinations represent Textonyms.
 
The numbers mapping to the most words map to 9 words each:
269 maps to: amy, any, bmw, bow, box, boy, cow, cox, coy
729 maps to: paw, pax, pay, paz, raw, ray, saw, sax, say
 
Type a number or a word to get the translation and textonyms: rosetta
Word rosetta is in the dictionary and is number 7673882 with textonyms: rosetta
 
Type a number or a word to get the translation and textonyms: code
Word code is in the dictionary and is number 2633 with textonyms: bode, code, coed
 
Type a number or a word to get the translation and textonyms: 2468
Number 2468 has the following textonyms in the dictionary: ainu, chou
 
Type a number or a word to get the translation and textonyms: 3579
Number 3579 has no textonyms in the dictionary.
</pre>
 
=={{header|ALGOL 68}}==
1,471

edits