-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphase2.py
135 lines (90 loc) · 2.87 KB
/
phase2.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
import os
import nltk
import numpy as np
import json
import pandas as pd
import math
import nltk
from nltk.tokenize import word_tokenize
from natsort import natsorted
from nltk.stem import PorterStemmer
nltk.download('punkt')
if __name__ == "__main__":
print("Term Frequency:\n")
document_of_tokens=[]
files_name = natsorted(os.listdir('files'))
for file_name in files_name:
with open(f'files/{file_name}','r') as f:
document = f.read()
tokenized_doc = word_tokenize(document)
tokens=[]
for word in tokenized_doc:
tokens.append(word)
document_of_tokens.append(tokens)
all_words = []
for doc in document_of_tokens:
for word in doc:
all_words.append(word)
def get_term_freq(doc):
words_found = dict.fromkeys(all_words, 0)
for word in doc:
words_found[word] += 1
return words_found
term_freq = pd.DataFrame(get_term_freq(document_of_tokens[0]).values(), index=get_term_freq(document_of_tokens[0]).keys())
for i in range(1, len(document_of_tokens)):
term_freq[i] = get_term_freq(document_of_tokens[i]).values()
term_freq.columns = ['doc'+str(i) for i in range(1, 11)]
if __name__ == "__main__":
print(term_freq)
print("\n\n")
if __name__ == "__main__":
print("Weighted Frequency:\n")
def weighted_tf(x):
if x > 0:
return math.log10(x) + 1
return 0
w_tf = term_freq.copy()
for i in range(0, len(document_of_tokens)):
w_tf['doc'+str(i+1)] = term_freq['doc'+str(i+1)].apply(weighted_tf)
if __name__ == "__main__":
print(w_tf)
if __name__ == "__main__":
print("Document Frequency:\n")
tdf = pd.DataFrame(columns=['df', 'idf'])
for i in range(len(term_freq)):
in_term = w_tf.iloc[i].values.sum()
tdf.loc[i, 'df'] = in_term
tdf.loc[i, 'idf'] = math.log10(10 / (float(in_term)))
tdf.index=w_tf.index
if __name__ == "__main__":
print(tdf)
print("\n\n")
if __name__ == "__main__":
print("TF*IDF:\n")
tf_idf = w_tf.multiply(tdf['idf'], axis=0)
if __name__ == "__main__":
print(tf_idf)
print("\n\n")
if __name__ == "__main__":
print("Document length:\n")
def get_doc_len(col):
return (np.sqrt(tf_idf[col].apply(lambda x: x**2).sum()))
doc_len = pd.DataFrame()
for col in tf_idf.columns:
doc_len.loc[0, col+'_length']= get_doc_len(col)
if __name__ == "__main__":
print(doc_len)
print("\n\n")
if __name__ == "__main__":
print("Normalized TF.IDF:\n")
def get_normalized_tf_idf(col, x):
try:
return x / doc_len[col+'_length'].values[0]
except:
return 0
normalized_tf_idf = pd.DataFrame()
for col in tf_idf.columns:
normalized_tf_idf[col] = tf_idf[col].apply(lambda x : get_normalized_tf_idf(col, x))
if __name__ == "__main__":
print(normalized_tf_idf)
print("\n\n")