This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsafe_IO.py
241 lines (206 loc) · 6.48 KB
/
safe_IO.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import json
import logging
import logging.config
import os
import shutil
import click
strange_key = ['\x00']
logger = logging.getLogger('FAIDK.safe_IO')
def get_name(path):
'''
get the file Name
'''
try:
names = os.listdir(path)
except:
os.mkdir(path)
names = os.listdir(path)
return names
def getch():
'''
跨平台getch
'''
try:
from msvcrt import getch as _getch
except:
from getch import getch as _getch
temp = _getch()
if type(temp) == bytes:
temp = temp.decode('utf-8')
print(temp)
return temp
def mv_file(path1, path2):
try:
shutil.move(path1, path2)
except Exception as e:
logger.error(e)
logger.info("move article failed")
def try_make_dir(path):
try:
os.mkdir(path)
except Exception as e:
logger.debug(e)
def check_output_file(file_path):
MESSAGE = file_path+' exists, Y to overwrite N to append'
if os.path.exists(file_path) and click.confirm(MESSAGE, default=False):
os.remove(file_path)
logger.info(file_path+' deleted')
def check_flag(mode):
FLAG = None
if mode is not None:
try:
FLAG = mode
except Exception as e:
logger.debug(e)
FLAG = None
else:
FLAG = None
choice = ['1', '2', 'q']
while FLAG not in choice:
logger.info('\tprint 1 to build model\n\t\t\tprint 2 to find model\n\t\t\tprint q to exit\n\t\t\t \
here to get some help:https://zhuanlan.zhihu.com/p/25003457\n')
FLAG = safe_get_input(choice)
logger.debug('check_flag:'+FLAG)
return FLAG
def my_input(output, choise=['1','2','q']):
'''
input
'''
logger.info(output)
judge = getch()
while judge == '\x00':
judge = getch()
if judge not in choise:
logger.info('Input error! Plz try again:')
judge = my_input(output)
return judge
return judge
def write_each_words(path, name, new_words):
'''
write new words by each article
'''
try:
os.makedirs(path)
except Exception as e:
logger.debug(e)
pass
try:
with open(path + name, 'w') as f_words:
f_words.write('\n'.join(new_words))
logger.info('write words to file \'' + path + name + '\'')
except:
logger.info('failed to creat file of \'' + path + name + '\'')
def read_article_from_file(path):
'''
read file with different encoding
'''
try:
with open(path, encoding='gbk')as f_article:
article = f_article.read()
except:
with open(path, encoding='utf8')as f_article:
article = f_article.read()
return article
def write_marked_article_to_file(path, name, articel):
try:
os.makedirs(path)
except Exception as e:
logger.debug(e)
pass
try:
with open(path + ''.join(name.split('.')[:-1])+'_MA.md', 'w', encoding='utf8') as f_words:
f_words.write(articel)
logger.info('write marked article to file \'' + path + name + '\'')
except:
logger.info('failed to creat file of \'' + path + name + '\'')
def write_important_sentances_to_file(path, name, articel):
try:
os.makedirs(path)
except Exception as e:
logger.debug(e)
pass
try:
with open(path + ''.join(name.split('.')[:-1])+'_IS.md', 'w', encoding='utf8') as f_words:
f_words.write(articel)
logger.info('write important sentances to file \'' + path + name + '\'')
except:
logger.info('failed to creat file of \'' + path + name + '\'')
def write_vocabulary_to_file(path, name, articel):
try:
os.makedirs(path)
except Exception as e:
logger.debug(e)
pass
try:
with open(path + ''.join(name.split('.')[:-1])+'_VO.md', 'w', encoding='utf8') as f_words:
f_words.write(articel)
logger.info('write vocabulary to file \'' + path + name + '\'')
except:
logger.info('failed to creat file of \'' + path + name + '\'')
def input_without_strange_key():
while True:
key = getch()
if key not in strange_key:
break
return key
def safe_get_input(expect_key, Error_msg='Input Error, plz retry.', output_msg='plz input:\n'):
while True:
logger.info(output_msg)
key = input_without_strange_key()
if if_expect_key(key, expect_key):
return key
logger.info(Error_msg)
def if_expect_key(key, expect_key):
if key not in expect_key:
return False
return True
def load_json(path):
config = {
'CONFIG_PATH': 'FAIDK.config',
'MAIN_PATH': './',
'NEW_WORDS_PATH': 'new.txt',
'OLD_WORDS_PATH': 'old.txt',
'ARTICLES_PATH': 'articles/',
'OLD_ARTICLES_PATH': 'old_articles/',
'NEW_WORDS_EACH_ARTICLE_PATH': 'new_words_of_each_article/',
'LEMMATIZATION_PATH': 'lemmatization-en.txt',
'LEMMATIZATION_MODE': 'list',
'LEMMATIZATION_MODE_AVAILABLE': "['None,'list','NLTK','both']",
'SPLIT_EVERY': '100',
'SPECIAL_PUNCTUATION': ';:;:‘’“”\'\"【】\[\]@#$%^&*()!@#¥%……&*()\-—',
'KEY_FOR_KNOW': '1',
'KEY_FOR_NOT': '2',
'KEY_FOR_QUIT': 'q',
'OUT_PUT_MARKED_ARTICLE':True,
'MARK':'**',
}
logger.info('Loading config from ' + path + '...')
try:
local_config = json.load(open(path))
for key in config:
local_config[key]
except Exception as e:
logger.warning(e)
if click.confirm('Loading config failed\n Write default config to ' +
config['MAIN_PATH'] + '?', default=True):
json.dump(config, open(config['CONFIG_PATH'], 'w'), indent=4)
return config
if click.confirm('Use default config?', default=True):
logger.info(config)
logger.info('Using default config')
return config
exit()
if local_config['CONFIG_PATH'] != path:
config = load_json(local_config['CONFIG_PATH'])
return local_config
def load_lemmatization_list_to_dic(mode):
if mode in ['list', 'both']:
logger.info('loading dic')
import pandas as pd
dic_data = pd.read_csv('./lemmatization-en.txt', sep='\t', header=None)
value = list(dic_data.iloc[:, 0])
key = list(dic_data.iloc[:, 1])
fix_dic = dict(zip(key, value))
logger.info('Done')
return fix_dic
return None