-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen.py
More file actions
56 lines (45 loc) · 1.1 KB
/
gen.py
File metadata and controls
56 lines (45 loc) · 1.1 KB
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
import json
import jieba
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
import pysparnn.cluster_index as ci
import pickle
# path
# 数据保存路径
qa_path = './data/qa.json'
tv_path = './data/tv.pkl'
cp_path = './data/cp.pkl'
# qa = [
# {
# 'q': 'question',
# 'a': 'answer'
# }
# ]
qa = json.load(open(qa_path))
# Generate corpus
# 将 QA 连接起来并分词
corpus = []
for id,item in enumerate(qa):
tmp = item['q'] + item['a']
tmp = jieba.cut(tmp)
tmp = ' '.join(tmp)
corpus.append(tmp)
# Generate bag of word
# TfidfVectorizer is a combination of CountVectorizer and TfidfTransformer
# Here we use TfidfVectorizer
tv = TfidfVectorizer()
# deal with corpus
tv.fit(corpus)
# get all words
# 词典
words = tv.get_feature_names()
# get feature
# 获取每对 QA 的TF-IDF
tfidf = tv.transform(corpus)
# build index
# 创建索引
cp = ci.MultiClusterIndex(tfidf, range(len(qa)))
# save
pickle.dump(tv, open(tv_path, 'wb'))
pickle.dump(cp, open(cp_path, 'wb'))