-
Notifications
You must be signed in to change notification settings - Fork 1
/
parseCACM.py
347 lines (323 loc) · 9.61 KB
/
parseCACM.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#Author: Krishna Sai
#Student at Northeastern University,
#Boston, MA, US
#
#Parse CACM collection and write words as files to a temporary directory
#so that the size of the corpus have no affect on the memory
#To run this file, please create a wordFiles named directory where this program is
#on disk. In this directory, the temporary word files gets created
#This file takes input as the document(s) to search at
#This file outputs four different files and a number of word files as mentioned
#below
#usr/bin/python
import linecache, string, operator, re, sys
from collections import defaultdict
from types import ListType
from porterStemmer import PorterStemmer
import os,shutil
f=open(sys.argv[1])
filetoOpen=sys.argv[1]
#open the common words provided in glassgow
s=open("./othersFromGlassgow/common_words")
stopwords_new=[]
stopwords=s.readlines()
#stop words that are not important
for word in stopwords:
stopwords_new.append(word.rstrip('\n'))
stopwords= stopwords_new
readLines=f.readlines()
#variables, arrays and dictionaries needed
docID=[]
docBibilo={}
docC={}
docTitle=defaultdict(list)
docK=defaultdict(list)
docN=defaultdict(list)
docAuthors=defaultdict(list)
docInfo=defaultdict(list)
line=0
lineNumber=0
dlen=0
#filetowrite=[]
stringtoAdd=""
stringtoAdd2=""
authortowrite=[]
filetowrite=[]
docLen={}
mainDict=defaultdict(list)
totalToCheck=defaultdict(list)
count=0
doclen=0
#process author for each document
def processAuthor(docID, line, lineNumber):
n=1
while(linecache.getline(filetoOpen,lineNumber+n)[0:2]!='.W' and
linecache.getline(filetoOpen,lineNumber+n)[0:2]!='.N' and
linecache.getline(filetoOpen,lineNumber+n)[0:2]!='.T' and
linecache.getline(filetoOpen,lineNumber+n)[0:2]!='.B' and
linecache.getline(filetoOpen,lineNumber+n)[0:2]!='.I' and
linecache.getline(filetoOpen,lineNumber+n)[0:2]!='.X' and
linecache.getline(filetoOpen,lineNumber+n)[0:2]!='.C' and
linecache.getline(filetoOpen,lineNumber+n)[0:2]!='.K'):
docAuthors[docID].append(linecache.getline(filetoOpen,lineNumber+n).strip("\n"))
n=n+1
#process through the document and populate the arrays
for i in readLines:
lineNumber=lineNumber+1
i=i.strip('\n')
if(lineNumber>1):
if(docInfo[docID[len(docID)-1]]<=0):
docInfo[docID]=""
if(docK[docID[len(docID)-1]]<=0):
docK[docID]=""
keys=docC.keys()
if docid not in keys:
docC[docid]=""
if(i[0:2]=='.I'):
docid=int(i[3:])
print "Processing doc: ",docid
docID.insert(lineNumber-1,(int(i[3:])))
if(i[0:2]=='.T'):
#line=linecache.getline(filetoOpen,lineNumber+1).strip('\n')
#docTitle[docid]=line
line1=linecache.getline(filetoOpen,lineNumber+1).replace('\n',' ')
next1=2
while(line1[0:2]!='.X' and line1[0:2]!='.N' and line1[0:2]!='.B'
and line1[0:2]!='.I' and line1[0:2]!='.C' and line1[0:2]!='.K'
and line1[0:2]!='.W'):
stringtoAdd2+=line1
line1=linecache.getline(filetoOpen,lineNumber+next1)
line1=line1.replace("\n"," ")
next1=next1+1
docTitle[docid].append(stringtoAdd2)
if(i[0:2]=='.B'):
line=linecache.getline(filetoOpen,lineNumber+1).strip('\n')
docBibilo[docid]=line
if(i[0:2]=='.A'):
processAuthor(docid, i, lineNumber)
if(i[0:2]=='.N'):
line=linecache.getline(filetoOpen,lineNumber+1).strip('\n')
nValues=string.split(line)
for i in nValues:
docN[docid].append(i)
if(i[0:2]=='.K'):
line=linecache.getline(filetoOpen,lineNumber+1).replace('\n',' ')
next=2
while(line[0:2]!='.X' and line[0:2]!='.N' and line[0:2]!='.B'
and line[0:2]!='.I' and line[0:2]!='.T' and line[0:2]!='.C' and line[0:2]!='.K'):
stringtoAddHere+=line
line=linecache.getline(filetoOpen,lineNumber+next).replace('\n'," ")
next=next+1
docK[docid].append(stringtoAddHere)
if(i[0:2]=='.C'):
line=linecache.getline(filetoOpen,lineNumber+1).strip('\n')
docC[docid]=line
if(i[0:2]=='.W'):
line=linecache.getline(filetoOpen,lineNumber+1).replace('\n',' ')
next=2
while(line[0:2]!='.X' and line[0:2]!='.N' and line[0:2]!='.B'
and line[0:2]!='.I' and line[0:2]!='.C' and line[0:2]!='.K'):
stringtoAdd+=line
line=linecache.getline(filetoOpen,lineNumber+next)
line=line.replace("\n"," ")
next=next+1
docInfo[docid].append(stringtoAdd)
stringtoAdd=""
stringtoAdd2=""
stringtoAddHere=""
linecache.clearcache()
#append all of the individual arrays to the whole dictionary
#This could be done while parsing but if one wants to index only on
#either author or text or.... then this helps
for i in docID:
mainDict[i].append(docTitle[i])
mainDict[i].append(docBibilo[i])
mainDict[i].append(docAuthors[i])
mainDict[i].append(docInfo[i])
mainDict[i].append(docN[i])
mainDict[i].append(docC[i])
mainDict[i].append(docK[i])
#lowe each word, and replace escape and other characters
def LowerPolish(fileT):
fileT=fileT.lower()
toRemove1 = [",","-","=","/","'",";","^","+","|",":","<",">","`","&","(",")"]
toRemove2 = [".",'"',"[","]","?","!","*","%","{","}"]
for i in toRemove1:
if i in fileT:
fileT=fileT.replace(i," ")
for i in toRemove2:
if i in fileT:
fileT=fileT.replace(i,'')
#print "returning fileT:" ,fileT
return fileT
#apply porter stemmer to the word and return the word
def applyStem(word):
p = PorterStemmer()
word = p.stem(word, 0,len(word)-1)
return word
#check the frequency of the word in the document id and return it
def checkforFrequency(docid, word):
freq=0
count=0
word=word[12:]
toCheck=[]
toApplyStem=[]
for i in mainDict[docid]:
if isinstance(i,ListType):
for listTerm in i:
if isinstance(listTerm, ListType):
for term in lisTerm:
toCheck.insert(count,term)
else:
toCheck.insert(count,listTerm)
else:
toCheck.insert(count, i)
count=count+1
for toCheckItem in toCheck:
toCheckItem=string.split(toCheckItem)
for items in toCheckItem:
items=LowerPolish(items)
items=items.strip()
if (' ' in items):
toApplyStem=string.split(items)
else:
toApplyStem.append(items)
for item in toApplyStem:
item=applyStem(item)
if item.strip()==word.strip():
freq=freq+1
del toApplyStem[:]
del toCheck[:]
return freq
#check if the word is already written
def checkifWritten(fileT, key):
f=open(fileT,'r')
toReturn=[]
words_new=[]
existing=f.readlines()
for words in existing:
words=string.split(words)
words_new.append(str(words[1]))
if str(key) in words_new:
return False
else:
return True
#print docs helper
def printDocsHelper(fileT,k):
if fileT not in stopwords:
#stemmed words
p = PorterStemmer()
fileT = p.stem(fileT, 0,len(fileT)-1) + " "
fileT=re.sub(r'\s', '', fileT)
if (len(fileT)>1) and (fileT not in stopwords):
fileT="./wordFiles/" + fileT
FILE=open(fileT,'a')
initFreq=checkforFrequency(k,fileT)
if checkifWritten(fileT,k):
FILE.write(str(fileT[12:])+ " " +str(k)+ " " +str(initFreq))
FILE.write("\n")
return 1
return 0
#write the word as a file to the specified directory
def printDocuments(mainDict):
filetowrite=[]
count=1
pos=0
for k,v in mainDict.iteritems():
dlen=0
print "Processing document: %s" %k
for value in v:
if isinstance(value, ListType):
for val in value:
fileTemp=string.split(val)
for filein in fileTemp:
filetowrite.insert(pos,filein)
pos=pos+1
else:
filetowrite=string.split(value)
for fileT in filetowrite:
fileRaw=fileT
fileT=LowerPolish(fileT)
if (' ' in fileT) == True or ('-' in fileT)==True:
filet=string.split(fileT)
for fileT in filet:
dlen=dlen+printDocsHelper(fileT,k)
else:
dlen=dlen+printDocsHelper(fileT,k)
docLen[k]=str(dlen)
del filetowrite[:]
printDocuments(mainDict)
#return all words in specific to the given arguments
def getAllWords(line,lineNumberr,wordCount):
countt=0
freq=0
toCheck=[]
line=line.strip()
line=line.rstrip('\n')
words=string.split(line)
toCheck.insert(countt,words[0])
countt=countt+1
toWrite=words[0]
countWrite=0
toPrint=toWrite
while toWrite == toCheck[0]:
freq=freq+int(words[2])
countWrite=countWrite+1
toPrint=toWrite
lineNumberr=lineNumberr+1
if lineNumberr < linesLength:
flagLine=lines[lineNumberr].rstrip('\n')
else:
break
words=string.split(flagLine)
toWrite=words[0]
FILEone.write(str(wordCount)+" " +str(toPrint)+" " +str(lineNumberr-1)+" " +str(countWrite)+" "+str(freq))
FILEone.write("\n")
return countWrite
#go to the directory where the word files are written to
f=open("fileappend1","a")
for r,d,fi in os.walk("./wordFiles"):
for files in fi:
g=open(os.path.join(r,files))
shutil.copyfileobj(g,f)
g.close()
f.close()
FILEone=open("file1","a")
f=open("fileappend1","r")
lines=f.readlines()
#temp variable
lineNumberr=0
wordCount=1
linesLength= len(lines)
i=0
while i<=len(lines)-1 :
toAdd=getAllWords(lines[i],lineNumberr,wordCount)
lineNumberr=lineNumberr+toAdd
wordCount=wordCount+1
i=i+toAdd
terms={}
FILEone.close()
#a file that maps term names to term IDs and associated term information,
#such as inverted index offset and length values and corpus frequency statistics
FILERead=open("file1","r")
r=FILERead.readlines()
for rlines in r:
rlines=rlines.strip('\n')
rlines=rlines.strip()
rlines=string.split(rlines)
terms[rlines[1]]=rlines[0]
#the inverted index file that maps term IDs to document IDs and associated term frequencies
fileTwo=open("file2","a")
for lines in lines:
lines=string.split(lines)
toWrite=terms[lines[0]]
fileTwo.write(str(toWrite)+" " +str(lines[1])+" "+str(lines[2]))
fileTwo.write("\n")
fileTwo.close()
f.close()
FILERead.close()
#le that maps document IDs to document names and associated document information, such as document lengths
file3=open('file3',"a")
for k,v in docLen.iteritems():
file3.write(str(k)+" " +str(docLen[k]))
file3.write("\n")