Created
September 16, 2019 14:01
-
-
Save 0leynik/0081a8e298dbbf0ac93ace2e8f32dd8b to your computer and use it in GitHub Desktop.
Add word or words to dictionary file in txt-format
This file contains 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
import sys | |
import re | |
import os | |
add_mode = sys.argv[1] | |
if add_mode == '--word': | |
words = [sys.stdin.read().strip()] | |
elif add_mode == '--words': | |
split_pattern = r'[\ \,\.\:\;\!\?\(\)\{\}\[\]\\\/\|\<\>\$\%\*]+' | |
words = re.split(split_pattern, sys.stdin.read()) | |
words = [w.strip().lower() for w in words] | |
words = [w for w in words if w] | |
unique_linked_words = [] | |
for w in words: | |
if w not in unique_linked_words: | |
unique_linked_words.append(w) | |
words = unique_linked_words | |
print(repr(words)) | |
dict_filepath = sys.argv[2] | |
if not os.path.isfile(dict_filepath): | |
with open(dict_filepath, 'w') as f: | |
pass | |
with open(dict_filepath, 'r+b') as f: | |
# add '\n' to end file if need | |
f.seek(0, os.SEEK_END) | |
if f.tell() != 0: | |
f.seek(-1, os.SEEK_END) | |
if f.read() != b'\n': | |
f.write(b'\n') | |
# write words | |
for w in words: | |
print(w) | |
f.write(w.encode()) | |
f.write(b'\n') | |
# /Users/dmitryoleynik/Library/Services/add word to dict.workflow | |
# /usr/local/bin/python3.7 /Users/dmitryoleynik/Library/Services/add_to_dict.py --word /Users/dmitryoleynik/new_words.txt | |
# /Users/dmitryoleynik/Library/Services/add words to dict.workflow | |
# /usr/local/bin/python3.7 /Users/dmitryoleynik/Library/Services/add_to_dict.py --words /Users/dmitryoleynik/new_words.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment