-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateTokenizedCorpus.py
175 lines (152 loc) · 5.56 KB
/
GenerateTokenizedCorpus.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
# importing necessary libraries
import glob
import os
import shutil
import re
import string
import sys
from bs4 import BeautifulSoup
from math import ceil
# files to be used
DIR_RAW_HTML='CASM-Files/Corpus'
STOPPED_WORDS_FILE='CASM-Files/common_words.txt'
STEMMED_QUERIES='CASM-Files/cacm_stem.txt'
# output directories
DIR_CORPUS='CorpusGeneration/TokenizedCorpus'
DIR_CORPUS_WITH_STOPPING='CorpusGeneration/TokenizedCorpusWithStopping'
DIR_CORPUS_WITH_STEMMING='CorpusGeneration/TokenizedCorpusWithStemming'
# method to convert HTML content into plain text
# Given: HTML content
# Returns: plain text
def parseHTML(htmlContent):
soup = BeautifulSoup(htmlContent, 'html.parser')
print(soup.get_text())
text=soup.get_text()
matchObj=re.search(r'[ \t\n\r\f\v]AM|[ \t\n\r\f\v]PM',text,re.M|re.I)
if matchObj:
return text[:matchObj.end()]
return text
# method to case-fold the text provided
# Given: plain text
# Return: case folded plain text
def caseFold(plainText):
return plainText.lower()
# method to remove punctuation from the text provided
# Given: plain text
# Return: plain text with removed punctuation
def removePunctuation(tokens):
newList=[]
for tok in tokens:
tok=tok.strip(string.punctuation)
matchNum=re.compile(r'^[\-]?[0-9]*\.?[0-9]+$')
if not matchNum.match(tok):
tok=re.sub(r'[^a-zA-Z0-9\--]','',tok)
tok=tok.strip(string.punctuation)
newList.append(tok)
return newList
# method to generate tokens from plain text
# Given: plain text
# Return: list of tokens
def generateTokens(plainText):
return list(filter(re.compile('[a-zA-Z0-9_]').search,plainText.split()))
def parser(fileName):
f = open(fileName, 'r')
# fileName = "CASM - Files / Corpus / CACM - 0001.html"
fileName = fileName.replace(" ", "")
docName=fileName.split('/')[-1].split('.')[0]+'.txt'
content = f.read()
f.close()
plainText=parseHTML(content)
caseFoldedPlainText=caseFold(plainText)
tokens=generateTokens(caseFoldedPlainText)
tokens=removePunctuation(tokens)
return (tokens,docName)
def writeTokenizedFiles(joinTokens,docName,corpusDirectory):
try:
if not os.path.exists(corpusDirectory):
os.makedirs(corpusDirectory)
if os.path.exists(corpusDirectory+docName):
print('same name file exists' ,corpusDirectory+'/'+docName)
tokenFile=open(corpusDirectory+'/'+docName,'w')
tokenFile.write(joinTokens)
tokenFile.close()
except Exception:
print(Exception)
def performStopping(tokens):
f=open(STOPPED_WORDS_FILE,'r')
stopList=f.read().split()
newTokenList=[]
for t in tokens:
if t not in stopList:
newTokenList.append(t)
f.close()
return newTokenList
def stemParser(corpusDirectory):
f=open(STEMMED_QUERIES,'r')
stemmedCorpus={}
for line in f.readlines():
if line[0]=='#':
line=line.strip('\n')
docName='CACM-'+'0'*(abs(len(line[2:])-4))+line[2:]
stemmedCorpus[docName]=""
else:
line=line.strip('\n')
stemmedCorpus[docName]+=line
i=1
for docName in stemmedCorpus:
data=stemmedCorpus[docName]
matchObj=re.search(r'[ \t\n\r\f\v]AM|[ \t\n\r\f\v]PM',data,re.M|re.I)
if matchObj:
data=data[:matchObj.end()]
stemmedCorpus[docName]=data
writeTokenizedFiles(data, docName+'.txt',corpusDirectory)
if i%160==0:
print("Parsing and Tokenization "+str(ceil(i/float(len(stemmedCorpus))*100))+'% complete')
i+=1
f.close()
def selectTypeOfTextTransformation():
while True:
print("\nSelect transformation technique:")
print("Enter 1 No Stopping No Stemming")
print("Enter 2 With Stopping")
print("Enter 3 With Stemming")
print("Enter 4 to exit")
x=int(input("Enter choice: "))
if x==1:
corpusDirectory=DIR_CORPUS
path = os.path.join(DIR_RAW_HTML, r"*.html")
rawFiles = glob.glob(path)
i = 1
print("Starting to generate corpus.....")
print("Parsing and Tokenization 0% complete")
for fileName in rawFiles:
# print(fileName)
tokens, docName = parser(fileName)
joinTokens = " ".join(tokens)
writeTokenizedFiles(joinTokens, docName, corpusDirectory)
if i % 160 == 0:
print("Parsing and Tokenization " + str(ceil(i / float(len(rawFiles)) * 100)) + '% complete')
i += 1
elif x==2:
corpusDirectory=DIR_CORPUS_WITH_STOPPING
path = os.path.join(DIR_RAW_HTML, r"*.html")
rawFiles = glob.glob(path)
i = 1
print("Starting to generate corpus.....")
print("Parsing and Tokenization 0% complete")
for fileName in rawFiles:
#print(fileName)
tokens, docName = parser(fileName)
tokens = performStopping(tokens)
joinTokens = " ".join(tokens)
writeTokenizedFiles(joinTokens, docName, corpusDirectory)
if i % 160 == 0:
print("Parsing and Tokenization " + str(ceil(i / float(len(rawFiles)) * 100)) + '% complete')
i += 1
elif x==3:
corpusDirectory=DIR_CORPUS_WITH_STEMMING
stemParser(corpusDirectory)
else:
break
if __name__ == '__main__':
selectTypeOfTextTransformation()