-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary_creator.py
135 lines (104 loc) · 4.15 KB
/
dictionary_creator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
import argparse
import sys
import re
SPLITTER = re.compile(r"[^\w'-]")
NUMBER = re.compile(r"[0-9]+")
DASH = re.compile(r"-")
LINE_BREAK_DETECTOR = re.compile(r"(\w+)-$")
WORD_GETTER = re.compile(r"^\w+")
def is_not_none_or_number_or_dash(string):
return (string and not NUMBER.search(string) and not
DASH.match(string))
def detect_line_break(string):
line_break_word = LINE_BREAK_DETECTOR.search(string)
if line_break_word and is_not_none_or_number_or_dash(
line_break_word.group(1)):
return line_break_word.group(1)
return None
def get_first_word_from_line(string):
first_word = WORD_GETTER.search(string)
if first_word and is_not_none_or_number_or_dash(first_word.group(0)):
return first_word.group(0)
return None
def get_words_from_text(file):
words = set()
line_break_word = None
for line in file:
if line_break_word:
first_word = get_first_word_from_line(line)
if first_word:
line = line[len(first_word):]
line_break_word += first_word
words.add(line_break_word.casefold())
line_break_word = detect_line_break(line)
if line_break_word:
line = line[:-(len(line_break_word) + 2)]
for word in filter(
is_not_none_or_number_or_dash, SPLITTER.split(line)):
words.add(word.casefold())
return words
def get_words_from_dict(file):
return {word.rstrip() for word in file}
def create(args):
with open(args.text, 'r', encoding=args.encoding) as f:
words = get_words_from_text(f)
with open(args.output_file, 'w', encoding='utf8') as f:
for word in sorted(words):
print(word, file=f)
def merge(args):
with open(args.first_dict, 'r', encoding='utf8') as f:
dict1 = get_words_from_dict(f)
with open(args.second_dict, 'r', encoding='utf8') as f:
dict2 = get_words_from_dict(f)
with open(args.output_file, 'w', encoding='utf8') as f:
for word in sorted(dict1 | dict2):
print(word, file=f)
def append(args):
args.output_file = args.first_dict
merge(args)
def add(args):
if (SPLITTER.search(args.word) or
not is_not_none_or_number_or_dash(args.word)):
print("Given string is not a word")
return
with open(args.dict, 'r', encoding='utf8') as f:
_dict = get_words_from_dict(f)
_dict.add(args.word.casefold())
with open(args.dict, 'w', encoding='utf8') as f:
for word in sorted(_dict):
print(word, file=f)
def parse_args():
"""Настройка argparse"""
parser = argparse.ArgumentParser(
description='Utility for creating, appending, merging dictionaries')
subparsers = parser.add_subparsers(dest='Mode')
subparsers.required = True
parser_add = subparsers.add_parser('add', help='Adds word to dictionary')
parser_add.add_argument('word')
parser_add.add_argument('dict')
parser_add.set_defaults(func=add)
parser_append = subparsers.add_parser(
'append', help='Appends one dictionary to another')
parser_append.add_argument('first_dict', help='First dictionary')
parser_append.add_argument(
'second_dict', help='Second dictionary(to be appended)')
parser_append.set_defaults(func=append)
parser_create = subparsers.add_parser(
'create', help='Creates a dictionary from given text')
parser_create.add_argument('text', help='Text file')
parser_create.add_argument('output_file', help='File to store dictionary')
parser_create.add_argument('encoding', nargs='?', default='utf8')
parser_create.set_defaults(func=create)
parser_merge = subparsers.add_parser(
'merge', help='Merges two dictionaries and creates a new file')
parser_merge.add_argument('first_dict', help='First dictionary')
parser_merge.add_argument('second_dict', help='Second dictionary')
parser_merge.add_argument('output_file', help='File to be stored in')
parser_merge.set_defaults(func=merge)
return parser.parse_args()
def main():
args = parse_args()
args.func(args)
if __name__ == '__main__':
main()