-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenization.py
More file actions
88 lines (76 loc) · 3.03 KB
/
Copy pathTokenization.py
File metadata and controls
88 lines (76 loc) · 3.03 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
from bs4 import BeautifulSoup
import requests
import nltk
from nltk.tokenize import word_tokenize, RegexpTokenizer
from nltk.corpus import stopwords
from string import punctuation
import os
import sys
import time
import matplotlib.pyplot as plt
# input and output path arguments from commandline
inputpath = sys.argv[1]
outputpath = sys.argv[2]
print(inputpath)
filenamelist = []
# Reading filenames from the below input directory
def getfilenames():
for path, dirs, files in os.walk(inputpath):
for f in files:
filename = os.path.join(path, f)
filenamelist.append(filename)
return filenamelist
# To tokenize the words with the help of BeautifulSoup library
# list of all tokenized words and total time taken to process eversy single input file
tokenizedwordlist = []
timetaken = []
wordfrquency = {}
frequency = {}
def tokenizewords():
filenames = getfilenames()
start = time.time()
for file in filenames:
with open(file, 'r', encoding='utf8', errors='ignore') as Html_File:
soupfile = BeautifulSoup(Html_File, features='html.parser')
text = soupfile.get_text()
text = text.lower()
text = text.strip()
# assignment 2 continuation, please ignore below two lines for now
# tokenizedwordlist = nltk.word_tokenize(text)
# stopwordslist = set(stopwords.words('english') + list(punctuation))
regexptokenizer = RegexpTokenizer(r'[a-zA-Z]+')
tokenizedwordlist = regexptokenizer.tokenize(text)
for word in tokenizedwordlist:
# print("printing word", word)
if word not in wordfrquency:
wordfrquency[word] = 1
else:
wordfrquency[word] += 1
# Writing an output of every html document to new text file.
outputfile = os.path.join(outputpath, os.path.basename(file))
with open(outputfile, 'w', encoding='utf8') as opf:
for token in tokenizedwordlist:
opf.write(str(token) + "\n")
end = time.time()
timetaken.append(end-start)
return tokenizedwordlist
print(timetaken)
t = tokenizewords()
print(wordfrquency)
# token as in word based sorted list
Sortedkey = sorted(wordfrquency)
with open(os.path.join(outputpath, 'Tokens.txt'), 'w', encoding='utf8') as newtokenizedfile:
for i in range(0, len(Sortedkey)):
newtokenizedfile.write(Sortedkey[i] + '\n')
# To sort based on word frequency
a = list(sorted(wordfrquency.items(), key=lambda x: x[1], reverse=True))
print(a)
with open(os.path.join(outputpath, 'Tokens_sorted_by_frequency.txt'), 'w', encoding='utf8') as newtokenizedfile:
for i in range(0, len(a)):
newtokenizedfile.write(str(a[i][0]) + ' : ' + str(wordfrquency[a[i][0]]) +'\n')
plt.xlabel('Files')
plt.ylabel('Time taken in seconds')
x_values = filenamelist
y_values = timetaken
plt.plot(x_values, y_values)
plt.show()