/
tf-idf.py
203 lines (162 loc) · 5.8 KB
/
tf-idf.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
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory
#
# import os
import nltk
#from bs4 import BeautifulSoup
import string
import os, glob
import re
import math
from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer, word_tokenize, sent_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.stem.porter import PorterStemmer
#
# def discard_html(text):
# data=BeautifulSoup(text,html)
# html_free= data.get_text()
# print(html_free)
text_data = []
def file_read():
for directory in os.listdir("C:/Users/sampa/Documents/dataset/movie-reviews/Dataset_Copy"):
text_dict = {}
i=0
filename=("C:/Users/sampa/Documents/dataset/movie-reviews/Dataset_Copy/"+directory+"/")
# print("**********************"+directory+"*******************")
os.chdir(filename)
for file in glob.glob("*.txt"):
i+=1
with open(file) as infile:
text_dict={'doc_id':i,
'doc_data':infile.read()}
text_data.append(text_dict)
return text_data
def remove_all_special_character(text):
parsed_string = re.sub('[^\w\s]', ' ', text)
parsed_string = re.sub('_', ' ', parsed_string)
parsed_string = re.sub('\s+', ' ', parsed_string)
parsed_string = parsed_string.strip()
return parsed_string
def read_data():
for directory in os.listdir("F:/TTU/Spring 2020-1st sem/Information retrieval/data_set/Reviews"):
text_data = []
filename = "F:/TTU/Spring 2020-1st sem/Information retrieval/data_set/Reviews/" + directory + "/"
# print("**********************"+directory+"*******************")
os.chdir(filename)
for file in glob.glob("*.txt"):
with open(file) as infile:
text_data.append(infile.read())
print(text_data)
def discard_punctuation(text):
data = "".join([c for c in text if c not in string.punctuation])
return data
def tokenize_data(text):
tokenizer = RegexpTokenizer(r'\w+')
data = tokenizer.tokenize(text.lower())
return data
def remove_stopwords(text):
words = [w for w in text if w not in stopwords.words('english')]
return words
def remove_apostrope(text):
data = np.char.replace(text, "'", "")
return data
def remove_a_character(text):
data = ""
for words in text:
if len(words) > 1:
data = data + " " + words
return data
def lemmatize_data(text):
lemmatizer = WordNetLemmatizer()
# data=[lemmatizer.lemmatize(word) for word in text]
data = " ".join([lemmatizer.lemmatize(word) for word in text])
return data
def stemming_data(text):
stemmer = PorterStemmer()
data = " ".join([stemmer.stem(word) for word in text])
return data
def count_words(text):
count = 0
words = word_tokenize(text)
for word in words:
count += 1
return count
def get_doc_info(text_data):
doc_info = []
i = 0
for text in text_data:
i += 1
count = count_words(text)
temp = {'doc_id': i, 'doc_length': count}
doc_info.append(temp)
return doc_info
def create_freq(text):
i = 0
freq_list = []
for data in text:
i += 1
freq_dict = {}
words = word_tokenize(data)
for word in words:
if word in freq_dict:
freq_dict[word] += 1
else:
freq_dict[word] = 1
temp = {'doc_id': i,
'freq_dict': freq_dict}
freq_list.append(temp)
return freq_list
def computeTF(doc_info, freq_list):
tf_score = []
for tempdict in freq_list:
id = tempdict['doc_id']
for num in tempdict['freq_dict']:
temp = {'key': num, 'doc_id': id,
'tf_score': tempdict['freq_dict'][num] / doc_info[id - 1]['doc_length']}
tf_score.append(temp)
return tf_score
def computeIDF(doc_info, freq_list):
Idf_score = []
counter = 0
for tempdict in freq_list:
counter += 1
for num in tempdict['freq_dict'].keys():
count = sum([num in tDict['freq_dict'] for tDict in freq_list])
temp = {'key': num, 'doc_id': counter,
'Idf_score': math.log(len(doc_info) / count)}
Idf_score.append(temp)
return Idf_score
class preprocess:
# text = "I have to say this movie is very \n tense. The disasters in // it make you think, will the/n world's really end this /n way? It's one of those films " \
# "where //it gets your mind thinking about // the world around us and how natural disasters can and will happen."
# # discard_html(text)
text=file_read()
for dict in text:
text= dict['doc_data']
parsed_data = discard_punctuation(text)
parsed_data = tokenize_data(parsed_data)
parsed_data = remove_stopwords(parsed_data)
parsed_data = remove_apostrope(parsed_data)
parsed_data = remove_a_character(parsed_data)
parsed_data = tokenize_data(parsed_data)
parsed_data = lemmatize_data(parsed_data)
parsed_data = sent_tokenize(parsed_data)
parsed_data = [remove_all_special_character(data) for data in parsed_data]
doc_info = get_doc_info(parsed_data)
freq_info = create_freq(parsed_data)
tf_score = computeTF(doc_info, freq_info)
idf_score = computeIDF(doc_info, freq_info)
print(tf_score)
print("\n")
print(idf_score)
# print(parsed_data)
# parsed_data=stemming_data(parsed_data)
# print(parsed_data)