Textonyms: Difference between revisions

(→‎{{header|Ruby}}: Add Python)
(→‎{{header|Python}}: Refactored.)
Line 66:
import urllib.request
 
ch2numCH2NUM = {ch: str(num) for num, chars in enumerate('abc def ghi jkl mno pqrs tuv wxyz'.split(), 2) for ch in chars}
urlURL = 'http://www.puzzlers.org/pub/wordlists/unixdict.txt'
 
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)
 
def getwords(url):
morethan1word = sum(1 for w in num2words if len(w) > 1)
words = return urllib.request.urlopen(url).read().decode("utf-8").lower().split()
 
def mapnum2words(words):
maxwordpernum = max(len(values) for values in num2words.values())
num2words number2words = defaultdict(list)
print("""
reject = 0
for word in words:
try:
num2words number2words[''.join(ch2numCH2NUM[ch] for ch in word)].append(word)
except KeyError:
# Reject words with non a-z e.g. '10th'
reject += 1
return dict(number2words), reject
 
def interactiveconversions():
global inp, ch, num
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])))
inp, ('' if inp in wordset else "n't"), num, ', '.join(num2words[numinp])))
else:
print(" Number {0} has no textonyms in the dictionary.".format(inp))
elif all(ch in ch2numCH2NUM for ch in inp):
num = ''.join(ch2numCH2NUM[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>
 
 
if __name__ == '__main__':
words = getwords(URL)
print("Read %i words from %r" % (len(words), urlURL))
wordset = set(words)
num2words, reject = mapnum2words(words)
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, urlURL, 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)))
 
interactiveconversions()</lang>
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}}
Anonymous user