-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlsi_modelling.py
70 lines (52 loc) · 1.84 KB
/
lsi_modelling.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
from gensim import corpora, models, similarities
from gensim.parsing import PorterStemmer
from numpy import array, sqrt
#Code for document preprocessing
#Build set of stopwords
f=open('stopwords')
stoplist = []
for line in f :
stoplist.append(line[0:-1])
f.close()
stoplist = set(stoplist)
#Initialize stemmer
stemmer = PorterStemmer()
def to_vector_model(corpusfile, tfidf=False):
"""
Convert a corpus to vector space model for processing
If tfidf = True, returns a tf-idf based model
Returns the model and dictionary of words.
"""
f = open(corpusfile, "r")
documents = f.readlines()
f.close()
#Remove stopwords and stem
documents = [list(set([stemmer.stem(word) for word in \
document.lower().translate(None, '?,.:;()[]""').split() \
if word not in stoplist])) for document in documents]
#Build a gensim dictionary
dictionary = corpora.Dictionary(documents)
#Build a vector space model from corpus
corpus = [dictionary.doc2bow(text) for text in documents]
if not tfidf:
return corpus, dictionary
#If tfidf is true, build tf-idf based model
tfidf = models.TfidfModel(corpus)
corpus = tfidf[corpus]
return corpus, dictionary, tfidf
def to_lsi(vector_model, dictionary, concepts=None):
"""
Converts a vector model to lower dimension space using Latent Semantic Indexing
Return the LSI model and the transformed corpus
"""
if not concepts:
concepts = int(sqrt(len(dictionary)))
lsi = models.LsiModel(vector_model, id2word=dictionary, num_topics=concepts)
corpus_lsi = lsi[vector_model]
lsi_vectors = []
for x in corpus_lsi:
temp = [0 for i in range(concepts)]
for y in x:
temp[y[0]] = y[1]
lsi_vectors.append(array(temp))
return lsi, lsi_vectors