-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector_models.py
188 lines (164 loc) · 7.13 KB
/
vector_models.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from gensim import corpora, models
from sklearn.feature_extraction.text import TfidfVectorizer
import pickle, json, os
def generate_tfidf_ls():
corpus_filename = 'data/ls_debates.mm'
p_corpus = corpora.MmCorpus(corpus_filename)
tfidf = models.TfidfModel(p_corpus)
pfile = open('data/ls_tfidf.pkl', 'w')
pickle.dump(tfidf[p_corpus], pfile)
pfile.close()
return tfidf[p_corpus]
def generate_tfidf_rs():
corpus_filename = 'data/rs_debates.mm'
p_corpus = corpora.MmCorpus(corpus_filename)
tfidf = models.TfidfModel(p_corpus)
pfile = open('data/rs_tfidf.pkl', 'w')
pickle.dump(tfidf[p_corpus], pfile)
pfile.close()
return tfidf[p_corpus]
def generate_tfidf_ngram_ls(folder_name="data/ls_debates/"):
session_enum = dict()
session_docs = list()
for file_name in os.listdir(folder_name):
with open(folder_name + file_name, 'r', encoding="utf8") as txt_file:
current_doc = txt_file.read().replace('\n', ' ')
txt_file.close()
if len(current_doc) == 0:
continue
month = int(file_name[3:5])
session_name = file_name[6:10]
if 2 <= month <= 5:
session_name += "-Budget"
elif 7 <= month <= 9:
session_name += "-Monsoon"
else:
session_name += "-Winter"
session_no = session_enum.get(session_name, None)
if session_no is None:
session_no = len(session_docs)
session_enum[session_name] = session_no
session_docs.append(current_doc)
# session_docs.append([" ".join(current_doc)])
else:
session_docs[session_no] += ' ' + current_doc
# session_docs[session_no].append(" ".join(current_doc))
with open("data/ls_session_enum_tfidf.json", 'w') as json_file:
json.dump(session_enum, json_file)
tfidf_vectorizer = TfidfVectorizer(max_df=0.8, max_features=200000,
min_df=0.1, stop_words='english',
use_idf=True, ngram_range=(1, 5))
tfidf_matrix = tfidf_vectorizer.fit_transform(session_docs) # fit the vectorizer to synopses
# pfile = open('data/ls_tfidf_ngram.pkl', 'w')
# pickle.dump(tfidf_matrix, pfile)
# pfile.close()
print(tfidf_matrix.shape)
return tfidf_matrix.A
def generate_tfidf_ngram_rs(folder_name="data/rs_debates/"):
session_enum = dict()
session_docs = list()
for file_name in os.listdir(folder_name):
with open(folder_name + file_name, 'r', encoding="utf8") as txt_file:
current_doc = txt_file.read().replace('\n', ' ')
txt_file.close()
if len(current_doc) == 0:
continue
if file_name[1] == 'S':
month = int(file_name[5:7])
session_name = '20' + file_name[8:10]
elif file_name[0] == 'S':
month = int(file_name[4:6])
session_name = '20' + file_name[7:9]
else:
month = int(file_name[3:5])
session_name = '20' + file_name[6:8]
if 2 <= month <= 5:
session_name += "-Budget"
elif 7 <= month <= 9:
session_name += "-Monsoon"
else:
session_name += "-Winter"
session_no = session_enum.get(session_name, None)
if session_no is None:
session_no = len(session_docs)
session_enum[session_name] = session_no
session_docs.append(current_doc)
else:
session_docs[session_no] += " " + current_doc
with open("data/combined_session_enum_tfidf.json", 'w') as json_file:
json.dump(session_enum, json_file)
tfidf_vectorizer = TfidfVectorizer(max_df=0.8, max_features=200000,
min_df=0.1, stop_words='english',
use_idf=True, ngram_range=(1, 5))
tfidf_matrix = tfidf_vectorizer.fit_transform(session_docs) # fit the vectorizer to synopses
print(tfidf_matrix.shape)
return tfidf_matrix.A
def generate_tfidf_ngram_combined(folder_name="data/"):
session_enum = dict()
session_docs = list()
for file_name in os.listdir(folder_name + "rs_debates/"):
with open(folder_name + "rs_debates/" + file_name, 'r', encoding="utf8") as txt_file:
current_doc = txt_file.read().replace('\n', ' ')
txt_file.close()
if len(current_doc) == 0:
continue
if file_name[1] == 'S':
month = int(file_name[5:7])
session_name = 'RS-20' + file_name[8:10]
elif file_name[0] == 'S':
month = int(file_name[4:6])
session_name = 'RS-20' + file_name[7:9]
else:
month = int(file_name[3:5])
session_name = 'RS-20' + file_name[6:8]
if 2 <= month <= 5:
session_name += "-Budget"
elif 7 <= month <= 9:
session_name += "-Monsoon"
else:
session_name += "-Winter"
session_no = session_enum.get(session_name, None)
if session_no is None:
session_no = len(session_docs)
session_enum[session_name] = session_no
session_docs.append(current_doc)
else:
session_docs[session_no] += " " + current_doc
for file_name in os.listdir(folder_name + "ls_debates/"):
with open(folder_name + "ls_debates/" + file_name, 'r', encoding="utf8") as txt_file:
current_doc = txt_file.read().replace('\n', ' ')
txt_file.close()
if len(current_doc) == 0:
continue
month = int(file_name[3:5])
session_name = "LS-" + file_name[6:10]
if 2 <= month <= 5:
session_name += "-Budget"
elif 7 <= month <= 9:
session_name += "-Monsoon"
else:
session_name += "-Winter"
session_no = session_enum.get(session_name, None)
if session_no is None:
session_no = len(session_docs)
session_enum[session_name] = session_no
session_docs.append(current_doc)
else:
session_docs[session_no] += ' ' + current_doc
with open("data/combined_session_enum_tfidf.json", 'w') as json_file:
json.dump(session_enum, json_file)
tfidf_vectorizer = TfidfVectorizer(max_df=0.8, max_features=200000,
min_df=0.1, stop_words='english',
use_idf=True, ngram_range=(1, 5))
tfidf_matrix = tfidf_vectorizer.fit_transform(session_docs) # fit the vectorizer to synopses
print(tfidf_matrix.shape)
return tfidf_matrix.A
def generate_lsi_topics_ls():
dictionary_filename = 'data/rs_debates.dict'
corpus_filename = 'data/rs_debates.mm'
p_dictionary = corpora.Dictionary.load(dictionary_filename)
corpus_tfidf = generate_tfidf_ls()
lsi = models.LsiModel(corpus_tfidf, id2word=p_dictionary, num_topics=500)
corpus_lsi = lsi[corpus_tfidf]
for x in lsi.show_topics(num_topics=10, num_words=20):
print(x)