Textonyms: Difference between revisions

Content deleted Content added
Line 61: Line 61:


</pre>
</pre>

=={{header|Python}}==
<lang python>from collections import defaultdict
import urllib.request

ch2num = {ch: str(num) for num, chars in enumerate('abc def ghi jkl mno pqrs tuv wxyz'.split(), 2) for ch in chars}

url = 'http://www.puzzlers.org/pub/wordlists/unixdict.txt'
words = urllib.request.urlopen(url).read().decode("utf-8").lower().split()
wordset = set(words)
print("Read %i words from %r" % (len(words), url))
num2words = defaultdict(list)
reject = 0
for word in words:
try:
num2words[''.join(ch2num[ch] for ch in word)].append(word)
except KeyError:
# Reject words with non a-z e.g. '10th'
reject += 1
num2words = dict(num2words)

morethan1word = sum(1 for w in num2words if len(w) > 1)

maxwordpernum = max(len(values) for values in num2words.values())
print("""
There are {0} words in {1} which can be represnted by the Textonyms mapping.
They require {2} digit combinations to represent them.
{3} digit combinations represent Textonyms.\
""".format(len(words) - reject, url, len(num2words), morethan1word))

print("\nThe numbers mapping to the most words map to %i words each:" % maxwordpernum)
maxwpn = sorted((key, val) for key, val in num2words.items() if len(val) == maxwordpernum)
for num, wrds in maxwpn:
print(" %s maps to: %s" % (num, ', '.join(wrds)))

while True:
inp = input("\nType a number or a word to get the translation and textonyms: ").strip().lower()
if inp:
if all(ch in '23456789' for ch in inp):
if inp in num2words:
print(" Number {0} has the following textonyms in the dictionary: {1}".format(inp, ', '.join(num2words[inp])))
else:
print(" Number {0} has no textonyms in the dictionary.".format(inp))
elif all(ch in ch2num for ch in inp):
num = ''.join(ch2num[ch] for ch in inp)
print(" Word {0} is{1} in the dictionary and is number {2} with textonyms: {3}".format(
inp, ('' if inp in wordset else "n't"), num, ', '.join(num2words[num])))
else:
print(" I don't understand %r" % inp)
else:
print("Thank you")
break</lang>

{{out}}
<pre>Read 25104 words from 'http://www.puzzlers.org/pub/wordlists/unixdict.txt'

There are 24978 words in http://www.puzzlers.org/pub/wordlists/unixdict.txt which can be represnted by the Textonyms mapping.
They require 22903 digit combinations to represent them.
22895 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.

Type a number or a word to get the translation and textonyms:
Thank you</pre>


=={{header|Ruby}}==
=={{header|Ruby}}==