Skip to content

Instantly share code, notes, and snippets.

@enosh
Created May 29, 2017 19:37
Show Gist options
  • Select an option

  • Save enosh/b4e7cca9b85d7a37421161bddff9f2eb to your computer and use it in GitHub Desktop.

Select an option

Save enosh/b4e7cca9b85d7a37421161bddff9f2eb to your computer and use it in GitHub Desktop.
"""Acronym Finder, rearranges letters to find words then ranks them based on a frequency list.
Usage:
acrofinder [-of --language=<lc> --format=<fmt> --save=<file>] <letters>
acrofinder --list-supported
acrofinder -h | --help
Options:
-h --help Show this screen.
-l --language=<lc> Language code of the database to run against [defualt: he].
-o --oneletter Include single-letters.
-f --full Run against the full list (and not the 50K most common).
-F --format=<fmt> Format of exported table (csv or tsv) [defualt: tsv].
-s --save=<file> File name of exported table.
--list-supported List supported language codes.
"""
from docopt import docopt
import urllib.request
from sys import exit
from itertools import permutations
DEFAULT_LANGUAGE = 'he'
SUPPORTED_LANGUAGES = ('af', 'ar', 'bg', 'bn', 'br', 'bs', 'ca', 'cs', 'da', 'de', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'gl', 'he', 'hi', 'hr', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'ka', 'kk', 'ko', 'lt', 'lv', 'mk', 'ml', 'ms', 'nl', 'no', 'pl', 'pt', 'pt_br', 'ro', 'ru', 'si', 'sk', 'sl', 'sq', 'sr', 'sv', 'ta', 'te', 'th', 'tl', 'tr', 'uk', 'vi', 'zh', 'zh_tw')
def get_frequency_dictionary(language=DEFAULT_LANGUAGE, fullness='50k'):
"""Retrives frequency list and makes it into a dictionary."""
url = 'https://github.com/hermitdave/FrequencyWords/raw/master/content/2016/{lc}/{lc}_{fullness}.txt'.format(lc=language, fullness=fullness)
response = urllib.request.urlopen(url)
data = response.read()
text = data.decode('utf-8')
frequencies = {}
for line in text.split("\n"):
arr = line.split(" ")
if (arr != ['']):
frequencies[arr[0]] = int(arr[1])
return frequencies
def permutate(letters, spaces=1):
for x in letters:
for y in permutations(x + ' '*spaces):
if (y[-1] == ' '):
continue
yield ''.join(y).split()
def filter_rank_word(words, dictionary, oneletter=False):
rank = 0
for word in words:
if (word in dictionary) and (oneletter or len(word) != 1):
rank += dictionary[word]
else:
return None
return (rank / len(words)**5)
if __name__ == '__main__':
arguments = docopt(__doc__)
if (arguments['--list-supported']):
print("The supported language codes are:")
print(', '.join(SUPPORTED_LANGUAGES[:-1]) + '.')
exit(-1)
if (arguments['--language'] == None) or (not arguments['--language'] in SUPPORTED_LANGUAGES):
print("Unsupported language code.")
exit(-1)
frequencies = get_frequency_dictionary(arguments['--language'])
if arguments['--format'] == 'csv':
line_format = "{:.5f},{}\n"
else:
line_format = "{:.5f}\t{}\n"
out = ""
for word_set in permutate([arguments['<letters>']]):
rank = filter_rank_word(word_set, frequencies, arguments['--oneletter'])
if rank != None:
out += line_format.format(rank, ' '.join(word_set))
if (arguments['--save'] == None):
print(out)
else:
with open(arguments['--save'], 'w') as f:
f.write(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment