This repository was archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
414 lines (287 loc) · 11.4 KB
/
main.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import nltk
from nltk.stem.snowball import SnowballStemmer # type: ignore
from nltk.corpus import stopwords
from nltk.corpus import wordnet as wn
import numpy as np # type: ignore
import pandas as pd # type: ignore
import tkinter as tk
from tkinter import filedialog
import re
#############################################################################################
# function to create the excel files
def create_excel_dataframe(input , name, columns, index):
df = pd.DataFrame(input, columns = columns, index = index) # Create a pandas dataframe
df.to_excel(name) # Create an excel file
return df
nltk.download('wordnet')
nltk.download('stopwords')
stop_words = stopwords.words('english')
stemmer = SnowballStemmer(language='english')
dictionary = []
#############################################################################################
def is_meaningful(word) -> bool:
if wn.synsets(word):
return True
else:
return False
def convert_to_list(doc) -> list:
doc = doc.split()
return doc
def create_dictionary(doc, dictionary) -> list:
for word_index, word in enumerate(doc):
word = word.lower()
if word.isalnum() and word not in stop_words and is_meaningful(word) and len(word) > 1:
stem_word = stemmer.stem(word)
doc[word_index] = stem_word
if stem_word not in dictionary:
dictionary.append(stem_word)
return doc
def join_document(doc) -> str:
doc = ' '.join(doc)
return doc
def process_document(document) -> str:
document = convert_to_list(document)
document = create_dictionary(document, dictionary)
document = join_document(document)
return document
#####################################################################################################
def select_file():
global raw_documents
file_name = filedialog.askopenfilename(title = "Select Dataset",
initialdir = "IR-Project",
filetypes = (("All files","*.*"), ("Text files", "*.txt")))
with open(file_name, "r") as f:
raw_documents = f.read()
window.destroy()
window = tk.Tk()
window.geometry("200x100")
window.title("Select Dataset")
button = tk.Button(
window,
text = "Select",
command = select_file).pack()
window.mainloop()
#########################################################################################################
raw_dataset = raw_documents # the raw dataset that read from the one file
raw_dataset = raw_dataset.split(".I") # split the docs by index
raw_dataset.remove(raw_dataset[0]) # remove the empty first index
doc_index = 0
# write each index in 'raw_dataset'
for doc in raw_dataset:
doc_index += 1
doc_path = "./docs" + "/doc" + str(doc_index) + ".txt"
doc = process_document(doc)
with open(doc_path, "w") as document: # write each index into the separated doc
document.write(doc)
doc_index = 0
for doc in raw_dataset:
doc_index += 1
doc_path = "./Original docs" + "/docs" + str(doc_index) + ".txt"
with open(doc_path, "w") as document: # write each index into the separated doc
document.write(doc)
df = pd.DataFrame(dictionary, columns=["Words"])
df.to_excel("dictionary.xlsx")
#############################################################################################################3
tf_array = np.zeros((len(dictionary), doc_index))
idf_array = np.zeros((len(dictionary), 1))
tf_idf_array = np.zeros((len(dictionary), doc_index))
def calculate_term_frequency(doc, dictionary, doc_no):
for word in dictionary:
if word in doc:
frequency = doc.count(word)
row = dictionary.index(word)
column = doc_no - 1
tf_array[row, column] += frequency
def calculate_document_frequency(doc, dictionary):
for word in dictionary:
if word in doc:
row = dictionary.index(word)
idf_array[row, 0] += 1
doc_names_list = []
for num in range(1, doc_index + 1):
doc_path = "./docs" + "/doc" + str(num) + ".txt"
doc_names_list.append("doc" + str(num) + ".txt")
with open(doc_path, "r") as file:
doc = file.read()
calculate_term_frequency(doc, dictionary, num)
calculate_document_frequency(doc, dictionary)
row, column = np.shape(tf_array)
for r in range(row):
for c in range(column):
if tf_array[r, c] > 0:
extracted_element = tf_array[r, c]
tf_array[r, c] = 1 + np.log10(extracted_element)
else:
tf_array[r, c] = 0
count_of_documents = column
row, column = np.shape(idf_array)
for r in range(row):
if idf_array[r, 0] > 0:
extracted_element = idf_array[r, 0]
idf_array[r, 0] = np.log10(count_of_documents / extracted_element)
else:
idf_array[r, 0] = 0
row, column = np.shape(tf_idf_array)
r, c = 0, 0
while r < row:
tf_idf_array[r, c]= tf_array[r, c] * idf_array[r, 0]
c += 1
if c == column:
r += 1
c = 0
tf_dataframe = create_excel_dataframe(tf_array, "tf_excel.xlsx", doc_names_list, dictionary)
idf_dataframe = create_excel_dataframe(idf_array, "idf_excel.xlsx", ["IDF"], dictionary)
tf_idf_dataframe = create_excel_dataframe(tf_idf_array, "tf_idf_excel.xlsx", doc_names_list, dictionary)
#####################################################################################################################################################
def rank_cosine(export_string: str,
cosine_docs: list,
cosine_values: list) -> None:
cosine_rank = sorted(dict(zip(cosine_docs, cosine_values)).items(), key = lambda x: x[1], reverse = True)
text_box.delete("0.0", tk.END)
for index, doc in enumerate(cosine_rank[:10], 1):
text_box.insert(tk.END, str(index)+ "." + str(doc[0]) + " : " + str(doc[1]) + "\n")
df = pd.DataFrame([doc[1] for doc in cosine_rank], index = [doc[0] for doc in cosine_rank], columns = ['cosine'])
df.to_excel(export_string +".xlsx")
def calculate_cosine(query_tfidf: np.array) -> tuple:
cosine_values = []
cosine_docs = []
for number in range(1, column + 1):
doc_name = "doc"+ str(number) +".txt"
doc_column = tf_idf_dataframe[doc_name].tolist()
nominator = np.dot(query_tfidf, doc_column)
doc_column_norm = np.linalg.norm(doc_column)
query_tfidf_norm = np.linalg.norm(query_tfidf)
denominator = doc_column_norm * query_tfidf_norm
cosine_theta = nominator / denominator
cosine_values.append(cosine_theta)
cosine_docs.append(doc_name)
return cosine_values, cosine_docs
def take_query_from_user():
global query_tfidf
global query_words_list
query = query_var.get()
query_entry.delete("0", tk.END)
query_lower = query.lower()
query_words_list = query_lower.split()
temp = []
for word in query_words_list:
if word not in stop_words and is_meaningful(word) and len(word) > 1:
temp.append(word)
query_words_list = temp
temp = [stemmer.stem(word) for word in query_words_list]
query_words = temp
query_words_list = list(set(temp))
query_words_str = " ".join(query_words)
query_idf_lst = [idf_array[dictionary.index(word)][0] for word in query_words_list]
query_tf_lst = [np.log10(query_words_str.count(word)) + 1 for word in query_words_list]
query_tfidf = [idf*query_tf_lst[count] for count, idf in enumerate(query_idf_lst)]
row = np.shape(tf_idf_array)[0]
query_tfidf = np.pad(query_tfidf, (0, row - len(query_tfidf)), 'constant', constant_values = (0))
for count, value in enumerate(query_tfidf):
if count < len(query_words_list):
if value > 0:
if count != dictionary.index(query_words_list[count]):
dest_index = dictionary.index(query_words_list[count])
query_tfidf[dest_index] = value
query_tfidf[count] = 0
cosine_values, cosine_docs = calculate_cosine(query_tfidf)
rank_cosine("cosine_similarity_rank",
cosine_docs,
cosine_values)
def rerank():
def get_doc_names() -> list:
temp = []
pattern = r'docs(\d+)\.txt'
for path in files_path:
temp.append(re.findall(pattern, path))
return [int(value[0]) for value in temp]
def add_tfidf_selected_docs():
for doc in target_doc_numbers:
for count, word in enumerate(query_words_list):
word_index_in_dictionary = dictionary.index(word)
query_tfidf[word_index_in_dictionary] += tf_idf_array[word_index_in_dictionary ,doc]
target_doc_numbers = get_doc_names()
add_tfidf_selected_docs()
cosine_values, cosine_docs = calculate_cosine(query_tfidf)
rank_cosine("cosine_similarity_rerank",
cosine_docs,
cosine_values)
window = tk.Tk()
window.geometry("930x300")
def choose():
global files_path
files_path = filedialog.askopenfilenames(
parent = window,
initialdir = "./Original docs",
title = "Choose the docs",
filetypes = [("text name","*.txt")]
)
rerank()
frame = tk.Frame(window)
frame.columnconfigure(0, weight = 1)
frame.columnconfigure(1, weight = 1)
frame.columnconfigure(2, weight = 1)
STICKY = tk.E + tk.W
PAD_VALUE = 10
query_var = tk.StringVar()
query_label = tk.Label(
frame,
text = "Query: "
)
query_label.grid(
row = 0,
column = 0,
sticky = STICKY,
padx = PAD_VALUE,
pady = PAD_VALUE
)
query_entry = tk.Entry(
frame,
textvariable = query_var
)
query_entry.grid(
row = 0,
column = 1,
sticky = STICKY,
padx = PAD_VALUE,
pady = PAD_VALUE
)
query_btn = tk.Button(
frame,
text = 'Enter Query',
command = take_query_from_user,
font = ('Arial', 10)
)
query_btn.grid(
row = 0,
column = 2,
sticky = STICKY,
padx = PAD_VALUE,
pady = PAD_VALUE
)
rerank_btn = tk.Button(
frame,
text = 'Rerank',
command = choose,
font = ('Arial', 10)
)
rerank_btn.grid(
row = 1,
column = 2,
sticky = STICKY,
padx = PAD_VALUE,
pady = PAD_VALUE
)
text_box = tk.Text(
frame,
height = 10
)
text_box.grid(
row = 1,
column = 1,
sticky = STICKY,
padx = PAD_VALUE,
pady = PAD_VALUE
)
frame.pack()
window.mainloop()