Textonyms: Difference between revisions
Content added Content deleted
(→{{header|Ruby}}: Add Python) |
(→{{header|Python}}: Refactored.) |
||
Line 66: | Line 66: | ||
import urllib.request |
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} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
num2words = dict(num2words) |
|||
def getwords(url): |
|||
⚫ | |||
⚫ | |||
def mapnum2words(words): |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return dict(number2words), reject |
|||
def interactiveconversions(): |
|||
global inp, ch, num |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
inp, ('' if inp in wordset else "n't"), num, ', '.join(num2words[num]))) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if __name__ == '__main__': |
|||
words = getwords(URL) |
|||
⚫ | |||
⚫ | |||
num2words, reject = mapnum2words(words) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
There are {0} words in {1} which can be represnted by the Textonyms mapping. |
There are {0} words in {1} which can be represnted by the Textonyms mapping. |
||
They require {2} digit combinations to represent them. |
They require {2} digit combinations to represent them. |
||
{3} digit combinations represent Textonyms.\ |
{3} digit combinations represent Textonyms.\ |
||
""".format(len(words) - reject, |
""".format(len(words) - reject, URL, len(num2words), morethan1word)) |
||
print("\nThe numbers mapping to the most words map to %i words each:" % maxwordpernum) |
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) |
maxwpn = sorted((key, val) for key, val in num2words.items() if len(val) == maxwordpernum) |
||
for num, wrds in maxwpn: |
for num, wrds in maxwpn: |
||
print(" %s maps to: %s" % (num, ', '.join(wrds))) |
print(" %s maps to: %s" % (num, ', '.join(wrds))) |
||
interactiveconversions()</lang> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{{out}} |
{{out}} |