Anonymous user
Textonyms: Difference between revisions
→{{header|Python}}: Refactored.
(→{{header|Ruby}}: Add Python) |
(→{{header|Python}}: Refactored.) |
||
Line 66:
import urllib.request
▲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▼
def getwords(url):
morethan1word = sum(1 for w in num2words if len(w) > 1)▼
def mapnum2words(words):
maxwordpernum = max(len(values) for values in num2words.values())▼
print("""▼
▲ reject = 0
▲ for word in words:
▲ try:
▲ 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(
else:▼
print(" Number {0} has no textonyms in the dictionary.".format(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")▼
if __name__ == '__main__':
words = getwords(URL)
▲ 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,
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}}
|