-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitModel.py
57 lines (41 loc) · 1.13 KB
/
initModel.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
#!/usr/bin/env python
# -*- coding: utf-8 -*
from gensim import corpora, models, similarities
import sys
stdout = sys.stdout
reload(sys)
sys.stdout = stdout
sys.setdefaultencoding('utf-8')
'''
Module name: initModel
Purpose: create corpus and lsimodel based on existing data
Author: : Zian Zhao
Version: 1.0 3.6,1017
'''
'''
Module name: initModel
Revision: store the similarity matrix
Author: : Zian Zhao
Version: 1.1 3.7,1017
'''
# get the corpus
infile = open('texts.txt', 'r')
texts = infile.readlines()
for i in range(len(texts)):
texts[i] = texts[i].split()
infile.close()
# model 只需要2选1
# create and save the dictionary
dictionary = corpora.Dictionary(texts)
dictionary.save('dictionary.dict')
corpus = [dictionary.doc2bow(text) for text in texts]
corpora.MmCorpus.serialize('cases.mm', corpus)
# create and save the tf-idf model
# tf_idf = models.TfidfModel(corpus)
# tf_idf.save('tfidf.model')
# create and save the LSI model
lsi_model = models.LsiModel(corpus, id2word=dictionary)
lsi_model.save('lsi.model')
# create the similar matrix
index = similarities.MatrixSimilarity(lsi_model[corpus])
index.save('sims.index')