stoichiograph.py (3826B)
1 #!/usr/bin/python3 2 3 import argparse 4 import collections 5 import json 6 import logging 7 import pathlib 8 9 from stoichiograph import speller 10 11 __title__ = 'stoichiograph' 12 __author__ = 'Amin Mesbah' 13 __version__ = '0.0.1' 14 __description__ = 'Spell words with elemental symbols from the periodic table.' 15 16 17 def get_args(): 18 parser = argparse.ArgumentParser( 19 prog=__title__, 20 description=__description__ 21 ) 22 parser.add_argument( 23 'words', 24 help='word(s) for which to find elemental spellings', 25 type=str, 26 nargs='*' 27 ) 28 parser.add_argument( 29 '-b', '--batch-file', 30 help='text file containing one word per line' 31 ) 32 parser.add_argument( 33 '-c', '--clobber', action='store_true', 34 help='overwrite output file if it exists' 35 ) 36 parser.add_argument( 37 '--debug', action='store_true', 38 help='print debug log' 39 ) 40 parser.add_argument( 41 '--list-elements', action='store_true', 42 help='print list of elemental symbols and exit' 43 ) 44 parser.add_argument( 45 '--export-graph', action='store_true', 46 help='export graph of first word as dot code' 47 ) 48 parser.add_argument( 49 '-o', '--output-file', 50 help='path of output json file' 51 ) 52 parser.add_argument( 53 '-s', '--sort', action='store_true', 54 help='sort words by length' 55 ) 56 parser.add_argument( 57 '-t', '--tuples', action='store_true', 58 help='display spellings as tuples' 59 ) 60 parser.add_argument( 61 '-v', '--verbose', action='store_true', 62 help='print a detailed log' 63 ) 64 parser.add_argument( 65 '-V', '--version', action='store_true', 66 help='print version info and exit' 67 ) 68 69 return parser.parse_args() 70 71 72 def main(): 73 args = get_args() 74 75 if args.version: 76 print('{} {}'.format(__title__, __version__)) 77 raise SystemExit 78 79 if args.list_elements: 80 print('{} Elements:'.format(len(speller.ELEMENTS))) 81 print(sorted(list(speller.ELEMENTS))) 82 raise SystemExit 83 84 if args.debug: 85 CONSOLE_LOG_LEVEL = logging.DEBUG 86 elif args.verbose: 87 CONSOLE_LOG_LEVEL = logging.INFO 88 else: 89 CONSOLE_LOG_LEVEL = logging.WARNING 90 91 logging.basicConfig(level=CONSOLE_LOG_LEVEL) 92 logging.debug('{} {}'.format(__title__, __version__)) 93 94 SORT_WORDS = args.sort 95 TUPLES = args.tuples 96 97 if args.output_file: 98 OUTPUT_FILE = pathlib.Path(args.output_file) 99 CLOBBER = args.clobber 100 101 if not CLOBBER and OUTPUT_FILE.exists(): 102 logging.warning( 103 "{} exists. To overwrite, use '--clobber'.".format(OUTPUT_FILE) 104 ) 105 raise SystemExit 106 107 if args.batch_file: 108 words_file = pathlib.Path(args.batch_file) 109 with words_file.open('r') as f: 110 dictionary = f.readlines() 111 112 # TODO(amin): Handle punctuation, apostraphies, etc. 113 words = [word.rstrip('\n') for word in dictionary if "'" not in word] 114 else: 115 words = args.words 116 117 if SORT_WORDS: 118 words.sort(key=len, reverse=True) 119 120 if args.export_graph and words: 121 g = speller.Graph() 122 speller.build_spelling_graph(words[0], g) 123 print(g.export()) 124 raise SystemExit 125 126 spellable = collections.OrderedDict() 127 128 for word in words: 129 if TUPLES: 130 spellings = speller.spell(word) 131 else: 132 spellings = [''.join(s) for s in speller.spell(word)] 133 134 if spellings: 135 spellable[word] = spellings 136 for spelling in spellings: 137 print(spelling) 138 139 if args.output_file: 140 with OUTPUT_FILE.open('w') as f: 141 json.dump(spellable, f, indent=4, sort_keys=False) 142 143 logging.debug('Done!') 144 145 146 if __name__ == '__main__': 147 main()