source code of /small-scripts/make-phonewords

Last modified
Lines 82

Parent directory Download CGIread sitemap Main page

Quick links: annoyed main phonify

  1. #!/usr/bin/python
  2. import string
  3. def phonify(s):
  4.     replace = {
  5.         'a': '2',
  6.         'b': '22',
  7.         'c': '222',
  8.         'd': '3',
  9.         'e': '33',
  10.         'f': '333',
  11.         'g': '4',
  12.         'h': '44',
  13.         'i': '444',
  14.         'j': '5',
  15.         'k': '55',
  16.         'l': '555',
  17.         'm': '6',
  18.         'n': '66',
  19.         'o': '666',
  20.         'p': '7',
  21.         'q': '77',
  22.         'r': '777',
  23.         's': '7777',
  24.         't': '8',
  25.         'u': '88',
  26.         'v': '888',
  27.         'w': '9',
  28.         'x': '99',
  29.         'y': '999',
  30.         'z': '9999',
  31.     }
  32.     filtered = filter(lambda x: x in string.letters, list(s))
  33.     translated = map(lambda x: replace[x.lower()], filtered)
  34.     return '-'.join(translated)
  35. def annoyed(s):
  36.     '''
  37.     out = ''
  38.     previous = ''
  39.     potential_pause = False
  40.     for ch in s:
  41.         if ch in string.digits:
  42.             if potential_pause and ch == previous:
  43.                 out += '--'
  44.                 potential_pause = False
  45.             previous = ch
  46.             out += ch
  47.         elif ch == '-':
  48.             potential_pause = True
  49.         else:
  50.             assert False, ch
  51.     return out
  52.     '''
  53.     for x in range(2, 10):
  54.         for y in range(2, 10):
  55.             if x != y:
  56.                 s = s.replace('{}-{}'.format(x, y), '{}{}'.format(x, y))
  57.     return s.replace('-', '--')
  58. def main(wordlist_path):
  59.     
  60.     f = open(wordlist_path)
  61.     lines = [line[:-1] for line in f]
  62.     f.close()
  63.     
  64.     stage = [(annoyed(phonify(line)), line) for line in lines]
  65.     del lines
  66.     
  67.     outlist = [(len(phone), phone, text) for phone, text in stage]
  68.     del stage
  69.     
  70.     outlist.sort(key=lambda x: x[0], reverse=True)
  71.     maxlen = outlist[0][0]
  72.     
  73.     for length, phone, text in outlist:
  74.         print('{}{} {}'.format(phone, ' '*(maxlen-length), text))
  75. if __name__ == '__main__':
  76.     main('/usr/share/dict/words')