forked from yoconana/Information-Retrieval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminWindow.py
169 lines (99 loc) · 3.66 KB
/
minWindow.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
### Author Xi Chen
### 1. read source files(after preprocess)
### 2. calculate minWindow for each file in the reading
### 3. return a list of minWindow values
### 4. normalize the value
import numpy as np
import copy
import os
import utils
from similarity import vectorlength
from similarity import simDistance
def readfiles(dicName):
AllFiles =[]
for filename in os.listdir(dicName):
#print filename
preprocessed_file = open(dicName+"/"+filename, 'r')
document = preprocessed_file.read().split()
#print type(document)
AllFiles.append(document)
preprocessed_file.close()
#utils.store_datastructure('preprocessed_docs_list.pkl',AllFiles)
return AllFiles
#readfiles("Removed")
#docs = utils.read_datastructure('preprocessed_docs_list.pkl')
def smallestWindow(query, document):
begin =0
size = 999999
start =0
Length = len(query)
#docLength = len(document)
mark1={};
for term in query:
if term not in mark1:
mark1[term] =1
else: mark1[term] +=1
mark2 = copy.deepcopy(mark1)
for n,i in enumerate(document):
if (document[n] in mark2) and (mark2[document[n]]!=0):
mark1[document[n]] -=1
if mark1[document[n]] >=0:
Length-=1
if Length ==0:
while Length ==0:
if (document[begin] in mark2) and (mark2[document[begin]]>0):
if mark1[document[begin]] >= 0:
break
else:
#print begin, mark1[document[begin]]
mark1[document[begin]] +=1
#print begin, mark1[document[begin]]
begin +=1
#updte minwindow
if size> n+1-begin:
size = n+1-begin
start = begin
end = start + size
minWindow = document[start:end]
minSize = len(minWindow)
return minSize
#print smallestWindow(["A","B"],["A","B","C","D"])
#input RankedList from similarity result and query list
#output a new ranked list by both similarity and term proximity results.
#output[0] is ranked docs indices. output[1] is ranked value
def newRankedList(docs, RankedDocList, query):
output =[]
newDocIndices = []
rankedIndice = RankedDocList[0]
rankedSimiValue = RankedDocList[1]
term_proximity=[]
for i in rankedIndice:
if len(docs[i]) == smallestWindow(query,docs[i]):
term_proximity.append(1000000)
else:
term_proximity.append(smallestWindow(query, docs[i]))
#normalization
#maxLength= float(max(term_proximity))
term_proximity = [1/float(x) for x in term_proximity]
## set a and b
a = 0.5
b = 0.5
newSim = np.array(rankedSimiValue)*a
newProximity = np.array(term_proximity)*b
simAndProxi =[x+y for x,y in zip(newSim, newProximity)]
#simAndProxi = newProximity.tolist()
rankedIndices = sorted(range(len(simAndProxi)), key = lambda k: simAndProxi[k])
for i in rankedIndices:
newDocIndices.append(rankedIndice[i])
newDocIndices.reverse()
simAndProxi.sort()
simAndProxi.reverse()
output.append(newDocIndices)
output.append(simAndProxi)
return output
""""
rlist =[[4,1,3],[0.4, 0.1, 0.05]]
q =['nation', 'park']
result = newRankedList(docs, rlist, q)
print result
"""