-
Notifications
You must be signed in to change notification settings - Fork 2
/
read_index.py
216 lines (189 loc) · 6.09 KB
/
read_index.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
# -*- coding: utf-8 -*-
from nltk.stem import PorterStemmer
import sys
termids = {}
docids = {}
term_info = {}
forward_index = {}
def load_term_ids():
f = open('termids.txt')
data = f.readlines()
for row in data:
element=row.split('\t')
termids[element[1].strip('\n')]=element[0]
f.close()
return termids
def load_doc_ids():
f=open('docids.txt')
data = f.readlines()
for row in data:
element=row.split('\t')
docids[element[1].strip('\n')]=element[0]
f.close()
return docids
def load_term_info():
f = open('term_info.txt')
data = f.readlines()
for row in data:
element=row.split('\t')
element[-1]=element[-1].strip('\n')
term_info[element[0]]=element[1:]
f.close()
return term_info
def get_inverted(posting, doc_id):
i = 2
sum_id = 0
doc_id_base = ''
first_id = False
while posting[i] != ':':
doc_id_base += posting[i]
i += 1
index_found = False
nid = ''
if int(doc_id_base) == int(doc_id):
sum_id = int(doc_id_base)
first_id = True
index_found = True
# find index
doc_check = False
while posting[i]!='\n' and index_found == False:
if doc_check:
doc_check = False
nid = ''
# getting complete relative id
while posting[i] != ':':
nid += posting[i]
i += 1
sum_id += int(nid)
if sum_id == int(doc_id):
index_found = True
break
elif posting[i] == '\t':
doc_check = True
i += 1
positions = []
rsum = 0
if index_found:
doc_check = False
position_flag = True
i += 1
while posting[i] != '\n':
if position_flag:
position = ''
while posting[i] != '\t':
position += posting[i]
i+= 1
positions.append(int(position))
position_flag = False
doc_check = True
elif doc_check:
rid = ''
while posting[i] != ':':
rid += posting[i]
i += 1
rsum += int(rid)
if first_id == False and rsum != sum_id:
break
elif first_id == True and rid != '0':
break
position_flag = True
doc_check = False
i += 1
i=0
current = 0
last = 0
while i < len(positions):
current = positions[i]
positions[i] = last + current
last = positions[i]
i += 1
return positions
def load_doc_info():
f = open('doc_index.txt')
data = f.readlines()
for row in data:
element=row.split('\t')
element[-1]=element[-1].strip('\n')
element=map(int,element)
if forward_index.has_key((element[0],element[1]))==False:
forward_index[(element[0],element[1])]=element[2:]
else:
print "duplicate found"
f.close()
def show_term_info(term_id):
info=term_info[term_id]
print "TERMID: " + term_id
print "Number of documents containing term: "+ info[2]
print "Term frequency in corpus: "+info[1]
print "Inverted list offset: "+info[0]
return
def show_doc_info(doc_id):
print "DOCID: "+ doc_id
distinct_terms=0
total_terms=0
i=0
for docid, termid in forward_index.keys():
if doc_id == str(docid):
distinct_terms+=1
total_terms+=len(forward_index[(docid,termid)])
i+=1
print "Distinct terms: "+str(distinct_terms)
print "Total terms: "+str(total_terms)
return total_terms
def get_doc_info(doc_id):
distinct_terms=0
total_terms=0
i=0
for docid, termid in forward_index.keys():
if doc_id == str(docid):
distinct_terms+=1
total_terms+=len(forward_index[(docid,termid)])
i+=1
return total_terms
load_term_ids()
load_doc_ids()
if (len(sys.argv)==3):
if(sys.argv[1]=='--term'):
term = sys.argv[2]
stemmer = PorterStemmer()
stemmed = stemmer.stem(term)
if termids.has_key(stemmed):
load_term_info()
term_id = termids[stemmed]
print'Listing for term: '+term
show_term_info(term_id)
else:
print "List for "+term+" not present."
elif(sys.argv[1]=='--doc'):
doc_name = sys.argv[2]
if docids.has_key(doc_name):
load_doc_info()
doc_id=docids[doc_name]
print'Listing for doc: '+doc_name
show_doc_info(doc_id)
else:
print "Document not present"
if len(sys.argv)==5:
if(sys.argv[1]=='--term' and sys.argv[3]=='--doc'):
term=sys.argv[2]
stemmer=PorterStemmer()
stemmed=stemmer.stem(term)
if termids.has_key(stemmed):
term_id=termids[stemmed]
doc_name=sys.argv[4]
if docids.has_key(doc_name):
doc_id=docids[doc_name]
load_term_info()
offset=(int)(term_info[term_id][0])
print term_info[term_id]
f=open('term_index.txt')
f.seek(offset)
line=f.readline()
f.close()
positions=get_inverted(line, doc_id)
print 'Inverted list for term: ' + term
print 'In document: ' + doc_name
print 'TERMID: ' + term_id
print 'DOCID: ' + doc_id
print 'Term frequency in document: ' + str(len(positions))
print 'Positions: ' + str(positions)