-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTask1_InvertedIndexer.py
162 lines (131 loc) · 5.27 KB
/
Task1_InvertedIndexer.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
import re
import os
import glob
from collections import *
PlainTextFileFolder = os.getcwd() + '/PlainText/'
oneGram_tfTable = 'MyIndex\\OneGram_TfTable.txt'
oneGram_dfTable = 'MyIndex\\OneGram_DfTable.txt'
twoGram_tfTable = 'MyIndex\\TwoGram_TfTable.txt'
twoGram_dfTable = 'MyIndex\\TwoGram_DfTable.txt'
threeGram_tfTable = 'MyIndex\\ThreeGram_TfTable.txt'
threeGram_dfTable = 'MyIndex\\ThreeGram_DfTable.txt'
oneGram_Stoplist = 'MyIndex\\Task3_Stoplist_OneGram.txt'
invertedIndex = {}
class TermFrequency:
def __init__(self, docNumber, docId, freq):
self.docNumber = docNumber
self.docId = docId
self.frequency = freq
def setDocId(self, docNum):
self.docNumber = docNum
def setDocId(self, docId):
self.docId = docId
def increaseFrequence(self, freq):
self.frequency = freq
class Document:
def __init__(self, tf):
self.tf = tf
def appendTf(self, tf):
self.tf.append(tf)
def getGramFrequency(text):
wordFreq = {}
for unword in text.split(' '):
word = unword.strip("'")
if word in wordFreq:
wordFreq[word] += 1
else:
wordFreq[word] = 1
return wordFreq
def prepareInvertedIndex(gramSequence):
docCounter = 1
for root, dirs, files in os.walk(PlainTextFileFolder):
for document in files:
#print docCounter
with open (PlainTextFileFolder + document, 'r') as textFile:
nGramFrequencies = getGramFrequency(textByGram(textFile.read().
replace('\n',' ').replace(' ',' ').replace(' ',' ').replace(' ', ' ')
, gramSequence))
for word in nGramFrequencies:
if (word.count('2_2') == gramSequence - 1 and word.split('2_2')[0] != '' and word.split('2_2')[gramSequence - 1] != ''):
if word not in invertedIndex and word != ' ' and word != '':
invertedIndex[word] = Document([TermFrequency(docCounter,
document,
nGramFrequencies[word])])
else:
invertedIndex[word].appendTf(TermFrequency(docCounter,
document,
nGramFrequencies[word]))
docCounter += 1
def textByGram(text, n):
if n == 1:
return text
nGramText = ''
newText = ''
wordCount = 0
textBySpace = text.split(' ')
textLength = len(textBySpace)
inWordCount = 1
while wordCount < len(textBySpace) - n:
newText += textBySpace[wordCount]
inWordCount = 1
while (inWordCount % n) != 0:
if textBySpace[wordCount + inWordCount] == ' ':
wordCount += 1
continue
newText += '2_2' + textBySpace[wordCount + inWordCount]
inWordCount += 1
newText += ' '
wordCount += 1
newText += textBySpace[textLength - n]
inWordCount = 1
while (inWordCount % n) != 0:
newText += '2_2' + textBySpace[textLength - (n - inWordCount)]
inWordCount += 1
return newText
def getGramInvertedList():
tfTable = {}
for word in invertedIndex:
freq = 0
for doc in invertedIndex[word].tf:
freq += doc.frequency
tfTable[word.replace('2_2',' ')] = freq
return tfTable
def createTfTable(tfTable, gramFile):
totWords = 0
with open (os.getcwd() + '/' + gramFile, 'a+') as tfFile:
tfFile.write('term, tf')
for key, value in (OrderedDict(sorted(tfTable.items(), key=lambda x: x[1], reverse = True))).items():
tfFile.write('\n%s, %d' % (key, value))
totWords += float(value)
print 'Total number of tokens = %f' % totWords
def createGramDocumentFrequency(gramFile):
with open (os.getcwd() + '/' + gramFile, 'a+') as dfFile:
dfFile.write('term docID df')
for word, value in (OrderedDict(sorted(invertedIndex.items(), key=lambda x: x[0]))).items():
line = '\n' + word.replace('2_2',' ') + ' '
for doc in invertedIndex[word].tf:
line += '%d,' % doc.docNumber
line = line[:-1] + ' %d' % len(invertedIndex[word].tf)
dfFile.write(line)
def generateStoplist(stoplist_file):
with open (os.getcwd() + '/' + stoplist_file, 'a+') as stopFile:
for word in invertedIndex:
if len(invertedIndex[word].tf) >= 500:
stopFile.write(word + '\n')
print ('Processing 1 grams....')
prepareInvertedIndex(1)
generateStoplist(oneGram_Stoplist)
createTfTable(getGramInvertedList(), oneGram_tfTable)
createGramDocumentFrequency(oneGram_dfTable)
#invertedIndex = {}
##print ('Processing 2 grams....')
##prepareInvertedIndex(2)
##createTfTable(getGramInvertedList(), twoGram_tfTable)
##createGramDocumentFrequency(twoGram_dfTable)
##
##invertedIndex = {}
##
##print ('Processing 3 grams....')
##prepareInvertedIndex(3)
##createTfTable(getGramInvertedList(), threeGram_tfTable)
##createGramDocumentFrequency(threeGram_dfTable)