Last active
August 23, 2021 16:02
-
-
Save thisisparker/d6aac6d6cd70c130503b5d4894640bc2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import sys | |
from pathlib import Path | |
def wordlist_to_dictionary(path): | |
with path.open() as f: | |
lines = [l.strip() for l in f.readlines()] | |
return {l.split(';')[0]:int(l.split(';')[1]) if len(l.split(';')) == 2 | |
else None | |
for l in lines} | |
def main(): | |
parser = argparse.ArgumentParser(description='Compare two .dict files and produce files showing what has been added, removed, and rescored.') | |
parser.add_argument('olddict', type=Path) | |
parser.add_argument('newdict', type=Path) | |
args = parser.parse_args() | |
oldlist = wordlist_to_dictionary(args.olddict) | |
newlist = wordlist_to_dictionary(args.newdict) | |
added = {w:newlist[w] for w in newlist.keys() if w not in oldlist.keys()} | |
removed = {w:oldlist[w] for w in oldlist.keys() if w not in newlist.keys()} | |
rescored = {w:[oldlist[w], newlist[w]] for w in oldlist.keys() if | |
w in newlist.keys() and oldlist[w] != newlist[w]} | |
print('Added: {}'.format(len(added))) | |
print('Removed: {}'.format(len(removed))) | |
print('Rescored: {}'.format(len(rescored))) | |
oldlist_name = args.olddict.name[:-len(''.join(args.olddict.suffixes))] | |
newlist_name = args.newdict.name[:-len(''.join(args.newdict.suffixes))] | |
with open('-'.join([oldlist_name, newlist_name, 'ADDED.dict']), 'w') as f: | |
f.write('\n'.join([';'.join([w, str(added[w])]) for w in added.keys()])) | |
with open('-'.join([oldlist_name, newlist_name, 'REMOVED.dict']), 'w') as f: | |
f.write('\n'.join([';'.join([w, str(removed[w])]) for w in removed.keys()])) | |
with open('-'.join([oldlist_name, newlist_name, 'RESCORED.txt']), 'w') as f: | |
f.write('\n'.join(['{}: {} => {}'.format(w, | |
rescored[w][0], rescored[w][1]) | |
for w in rescored.keys()])) | |
with open('-'.join([oldlist_name, newlist_name, 'RESCORED.dict']), 'w') as f: | |
f.write('\n'.join([';'.join([w, str(newlist[w])]) for w in rescored.keys()])) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment