forked from seed9D/Product_Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp_func.py
50 lines (41 loc) · 1.12 KB
/
help_func.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
import logging
import os
def check_dir_exist(path):
if not os.path.exists(path):
os.makedirs(path)
print('makdir {}'.format(path))
def create_log():
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(levelname)s:[%(filename)s %(funcName)s:%(lineno)d] %(message)s')
console.setFormatter(formatter)
logger.addHandler(console)
return logger
def read_data(path):
with open(path, 'r') as rf:
def dedupe(items):
seen = set()
for item in items:
if item not in seen:
yield item
seen.add(item)
string_list = []
readlines = dedupe(rf.readlines())
for line in readlines:
line = line.strip()
string_list.append(line)
return string_list
def read_word_fre(path):
string_list = read_data(path)
word_fre_pair = []
for string in string_list:
string_split = string.split('\t')
word_fre_pair.append((string_split[0], string_split[1]))
return word_fre_pair
def write_data(path, list_iter):
with open(path, 'w') as wf:
for ele in list_iter:
if ele:
wf.write('{}\n'.format(ele))