forked from zhanzecheng/Chinese_segment_augment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
53 lines (43 loc) · 1.19 KB
/
utils.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
# -*- coding: utf-8 -*-
"""
# @Time : 2018/5/26 下午5:20
# @Author : zhanzecheng
# @File : utils.py
# @Software: PyCharm
"""
import pickle
def get_stopwords():
with open('data/stopword.txt', 'r') as f:
stopword = [line.strip() for line in f]
return set(stopword)
def generate_ngram(input_list, n):
result = []
for i in range(1, n+1):
result.extend(zip(*[input_list[j:] for j in range(i)]))
return result
def load_dictionary(filename):
"""
加载外部词频记录
:param filename:
:return:
"""
word_freq = {}
print('------> 加载外部词集')
with open(filename, 'r') as f:
for line in f:
try:
line_list = line.strip().split(' ')
# 规定最少词频
if int(line_list[1]) > 2:
word_freq[line_list[0]] = line_list[1]
except IndexError as e:
print(line)
continue
return word_freq
def save_model(model, filename):
with open(filename, 'wb') as fw:
pickle.dump(model, fw)
def load_model(filename):
with open(filename, 'rb') as fr:
model = pickle.load(fr)
return model