-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhtml_bow_old.py
More file actions
297 lines (228 loc) · 10.1 KB
/
html_bow_old.py
File metadata and controls
297 lines (228 loc) · 10.1 KB
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
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from urllib.request import urlopen
import re
import os
import numpy as np
from random import shuffle
from sklearn.feature_extraction.text import CountVectorizer
from nltk.stem.porter import *
from nltk.tokenize import RegexpTokenizer
from sklearn.feature_extraction.text import TfidfTransformer
from nltk import sent_tokenize
from nltk import ne_chunk, pos_tag, word_tokenize
from nltk.tree import Tree
from nltk.corpus import stopwords
from nltk.tag.stanford import StanfordNERTagger
from sklearn.decomposition import TruncatedSVD
def term_frequency_matrix(documents, terms):
# Module to create the term frequency matrix
td_matrix = []
for itr in documents:
doc_terms = [itr[0].count(t) for t in terms]
td_matrix.append(doc_terms)
return np.array(td_matrix)
# def extract_phone_numbers(string):
# r = re.compile(
# r'(\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4})')
# phone_numbers = r.findall(string)
# return [re.sub(r'\D', '', number) for number in phone_numbers]
# def extract_email_addresses(string):
# r = re.compile(r'[\w\.-]+@[\w\.-]+')
# return r.findall(string)
# def ie_preprocess(document):
# stop = stopwords.words('english')
# document = ' '.join([i for i in document.split() if i not in stop])
# sentences = sent_tokenize(document)
# sentences = [word_tokenize(sent) for sent in sentences]
# sentences = [pos_tag(sent) for sent in sentences]
# return sentences
# def extract_names(document):
# names = []
# sentences = ie_preprocess(document)
# for tagged_sentence in sentences:
# for chunk in ne_chunk(tagged_sentence):
# if type(chunk) == Tree:
# if chunk.label() == 'PERSON':
# names.append(' '.join([c[0] for c in chunk]))
# return names
def main():
# url_link = "file:///home/sanjoy/Desktop/course-cotrain-data/fulltext/course/http:%5E%5Ecs.cornell.edu%5EInfo%5ECourses%5ECurrent%5ECS415%5ECS414.html"
text_container = []
unique_words = []
path = '/home/raja/Raja/Sem2/SMAI/Project/course-cotrain-data/fulltext/course/'
stemmer = PorterStemmer()
tokenizer = RegexpTokenizer(r'\w+')
class_label = []
for filename in os.listdir(path):
filename = 'file:///home/raja/Raja/Sem2/SMAI/Project/course-cotrain-data/fulltext/course/' + filename
sock = urlopen(filename)
htmlSource = sock.read()
htmlSource = htmlSource.decode("windows-1252")
sock.close()
class_label.append(0)
# for obtaining text inside <> tags
cleanr = re.compile('<.*?>')
htmlSource = re.sub(cleanr, '', htmlSource)
# basic preprocessing
# word_tokens = word_tokenize(htmlSource)
# numbers = extract_phone_numbers(htmlSource)
# emails = extract_email_addresses(htmlSource)
# names = extract_names(htmlSource)
# text_content = ""
# for i in numbers:
# if(i not in numbers):
# text_content += i
# for i in emails:
# if(i not in emails):
# text_content += i
# for i in names:
# if(i not in names):
# text_content += i
# htmlSource = text_content
# print (ne_chunk(pos_tag(word_tokenize(htmlSource))))
word_tokens = tokenizer.tokenize(htmlSource.lower())
word_list = [stemmer.stem(line) for line in word_tokens if line not in '']
stop_words = set(stopwords.words('english'))
word_tokens = [w for w in word_list if not w in stop_words]
unique_words += list(set(word_tokens))
unique_words = list(set(unique_words))
dummy_str = ""
for i in word_tokens:
dummy_str += i + " "
dummy_list = [dummy_str]
text_container.append(dummy_list)
# print(unique_words)
path = '/home/raja/Raja/Sem2/SMAI/Project/course-cotrain-data/fulltext/non-course/'
for filename in os.listdir(path):
filename = 'file:///home/raja/Raja/Sem2/SMAI/Project/course-cotrain-data/fulltext/non-course/' + filename
sock = urlopen(filename)
htmlSource = sock.read()
htmlSource = htmlSource.decode("windows-1252")
sock.close()
class_label.append(1)
# for obtaining text inside <> tags
cleanr = re.compile('<.*?>')
htmlSource = re.sub(cleanr, '', htmlSource)
# basic preprocessing
word_tokens = word_tokenize(htmlSource)
word_list = [stemmer.stem(line) for line in word_tokens if line not in '']
stop_words = set(stopwords.words('english'))
word_tokens = [w for w in word_list if not w in stop_words]
unique_words += list(set(word_tokens))
unique_words = list(set(unique_words))
dummy_str = ""
for i in word_tokens:
dummy_str += i + " "
dummy_list = [dummy_str]
text_container.append(dummy_list)
class_label = np.asarray(class_label)
class_label = class_label.reshape(class_label.shape[0], 1)
# print(class_label.shape)
tf_matrix = term_frequency_matrix(text_container, unique_words)
tf = TfidfTransformer(norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False)
tf_idf_matrix = tf.fit_transform(tf_matrix).todense()
size = int(tf_idf_matrix.shape[0] * 0.7)
print(tf_idf_matrix)
svd = TruncatedSVD(n_components=1050, random_state=42)
tf_idf_matrix_SVD = svd.fit_transform(tf_idf_matrix)
tf_idf_matrix_with_labels = np.concatenate(
(tf_idf_matrix_SVD, class_label), axis=1)
np.random.shuffle(tf_idf_matrix)
test_tf_idf_matrix = tf_idf_matrix_with_labels[size:, :]
train_tf_idf_matrix = tf_idf_matrix_with_labels[:size, :]
fp = open('tfidf_matrix_fulltext_train.txt', 'w')
for i in range(train_tf_idf_matrix.shape[0]):
for j in range(train_tf_idf_matrix.shape[1]):
fp.write(str(train_tf_idf_matrix[i][j]) + " ")
fp.write("\n")
fp.close()
fp = open('tfidf_matrix_fulltext_test.txt', 'w')
for i in range(test_tf_idf_matrix.shape[0]):
for j in range(test_tf_idf_matrix.shape[1]):
fp.write(str(test_tf_idf_matrix[i][j]) + " ")
fp.write("\n")
fp.close()
# for inlinks view
text_container = []
unique_words = []
path = '/home/raja/Raja/Sem2/SMAI/Project/course-cotrain-data/inlinks/course/'
stemmer = PorterStemmer()
tokenizer = RegexpTokenizer(r'\w+')
class_label = []
for filename in os.listdir(path):
filename = 'file:///home/raja/Raja/Sem2/SMAI/Project/course-cotrain-data/inlinks/course/' + filename
sock = urlopen(filename)
htmlSource = sock.read()
htmlSource = htmlSource.decode("windows-1252")
sock.close()
class_label.append(0)
# for obtaining text inside <> tags
cleanr = re.compile('<.*?>')
htmlSource = re.sub(cleanr, '', htmlSource)
word_tokens = tokenizer.tokenize(htmlSource.lower())
word_list = [stemmer.stem(line)
for line in word_tokens if line not in '']
stop_words = set(stopwords.words('english'))
word_tokens = [w for w in word_list if not w in stop_words]
unique_words += list(set(word_tokens))
unique_words = list(set(unique_words))
dummy_str = ""
for i in word_tokens:
dummy_str += i + " "
dummy_list = [dummy_str]
text_container.append(dummy_list)
# print(unique_words)
path = '/home/raja/Raja/Sem2/SMAI/Project/course-cotrain-data/inlinks/non-course/'
for filename in os.listdir(path):
filename = 'file:///home/raja/Raja/Sem2/SMAI/Project/course-cotrain-data/inlinks/non-course/' + filename
sock = urlopen(filename)
htmlSource = sock.read()
htmlSource = htmlSource.decode("windows-1252")
sock.close()
class_label.append(1)
# for obtaining text inside <> tags
cleanr = re.compile('<.*?>')
htmlSource = re.sub(cleanr, '', htmlSource)
# basic preprocessing
word_tokens = word_tokenize(htmlSource)
word_list = [stemmer.stem(line)
for line in word_tokens if line not in '']
stop_words = set(stopwords.words('english'))
word_tokens = [w for w in word_list if not w in stop_words]
unique_words += list(set(word_tokens))
unique_words = list(set(unique_words))
dummy_str = ""
for i in word_tokens:
dummy_str += i + " "
dummy_list = [dummy_str]
text_container.append(dummy_list)
class_label = np.asarray(class_label)
class_label = class_label.reshape(class_label.shape[0], 1)
# print(class_label.shape)
tf_matrix = term_frequency_matrix(text_container, unique_words)
tf = TfidfTransformer(norm='l2', use_idf=True,
smooth_idf=True, sublinear_tf=False)
tf_idf_matrix = tf.fit_transform(tf_matrix).todense()
size = int(tf_idf_matrix.shape[0] * 0.7)
print(tf_idf_matrix)
svd = TruncatedSVD(n_components=1050, random_state=42)
tf_idf_matrix_SVD = svd.fit_transform(tf_idf_matrix)
tf_idf_matrix_with_labels = np.concatenate(
(tf_idf_matrix_SVD, class_label), axis=1)
np.random.shuffle(tf_idf_matrix)
test_tf_idf_matrix = tf_idf_matrix_with_labels[size:, :]
train_tf_idf_matrix = tf_idf_matrix_with_labels[:size, :]
fp = open('tfidf_matrix_inlinks_train.txt', 'w')
for i in range(train_tf_idf_matrix.shape[0]):
for j in range(train_tf_idf_matrix.shape[1]):
fp.write(str(train_tf_idf_matrix[i][j]) + " ")
fp.write("\n")
fp.close()
fp = open('tfidf_matrix_inlinks_test.txt', 'w')
for i in range(test_tf_idf_matrix.shape[0]):
for j in range(test_tf_idf_matrix.shape[1]):
fp.write(str(test_tf_idf_matrix[i][j]) + " ")
fp.write("\n")
fp.close()
main()