-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorSpaceModel.py
179 lines (148 loc) · 5.87 KB
/
vectorSpaceModel.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
##########################################
##-----------Anurag Banerjee------------##
##---------------CS 7030----------------##
##----------- Assignment 4 -----------##
##########################################
from __future__ import division
from operator import itemgetter
import indexer
import string
import re
import numpy as np
'''
In this program we will show the working of the VSM Model for search
0. create query vector
1. From Document Term Matrix - create doc vectors
2. tw = (1+ln(tf))*(ln(N/df)) # tf from docterm matrix, N = number of docs in which term present, df from inverse
3. normalize all sqrt(sum(sqr(w_i))) <--- skipped this
4. calculate cosine similarity between query and each doc
5. high value of cos similarity=> high rank
6. show top fifteen docs
'''
# The global variables
rows = 0 # number of documents in collection
cols = 0 # number of unique terms in collection
docList = [] # List of all documents in collection to provide integer count to name mapping
termList = []
termDfList = []
def getQuery():
"""
This functions asks user to input a query and cleans/stems it.
:return:
"""
queryString = raw_input("Enter your query: ")
queryString = queryString.translate(None, string.punctuation) # removing all punctuation except parenthesis
temp = ""
for char in queryString:
if char.isspace() or char.isalnum():
temp += char
else:
temp += ' '+char+' '
queryString = temp.strip()
queryString = re.sub(ur' +', ' ', queryString.lower()) # remove extra spaces
return indexer.stemmer(queryString) # now performing stemming and returning list of terms in query
def getDocIndex():
"""
In this dummy function we will simply call upon the functions we built in the indexer.py file
:return:
"""
docTermDict = indexer.buildDocTerm() # the doc term matrix in a python dictionary
termDocDict = indexer.buildInvIndx(docTermDict) # the inverted, term-doc matrix in a python dictionary
return (docTermDict, termDocDict)
def createQueryVector(qList):
"""
This functions returns a vector for the query.
:return:
"""
global termList, termDfList, docList
qVec = np.zeros((1, cols))
for term in qList:
index = termList.index(term)
if termDfList[index]:
qVec[0][termList.index(term)] = np.log(len(docList)/termDfList[index])
else:
qVec[0][termList.index(term)] = np.log(len(docList))
return qVec
def createDocVectors(qTermList, docTermDict, termDocDict):
"""
We will create the document vectors here
:return: the inverted term-document index
"""
global rows, cols, docList, termList, termDfList
# modifying term-doc matrix for query terms not in collection
for term in qTermList:
if term not in termDocDict:
termDocDict[term] = [0, {}]
rows = len(docTermDict.keys())
cols = len(termDocDict.keys())
docList = [doc for doc in sorted(docTermDict.keys())] # list of docs
termList = [term for term in sorted(termDocDict.keys())] # list of terms
termDfList = [termDocDict[term][0] for term in sorted(termDocDict.keys())]
print("Building the Document Vectors...")
docVecs = np.zeros((rows, cols))
for (docIndx, docId) in enumerate(docList):
for term in docTermDict[docId].keys():
docVecs[docIndx][termList.index(term)] = (1 + np.log(docTermDict[docId][term]))*(np.log(len(docList)/termDfList[termList.index(term)]))
# print docVecs
return docVecs
def rankDocAndShow(docScore, count):
"""
This function will essentially sort my documents according to the score and display top fifteen
:param docScore:
:return:
"""
global docList, rows
'''sort_permutation = np.argsort(docScore)
sorted_docList = docList[sort_permutation]'''
# docScore (rows, 1), docList(rows,)
print "Ranking the results..."
docListnumpy = np.array(docList)
joined = np.column_stack((docScore, docListnumpy.reshape(rows, 1)))
# sorted_docList = np.sort(joined, axis=-1)
sorted_docList = np.array(sorted(joined, key=itemgetter(0), reverse=True))
print "Top fifteen documents for your query are:\n"
for i in range(count):
print "Rank."+str(i+1)+": "+sorted_docList[i][1]+"\t(match score: "+sorted_docList[i][0]+")"
def vsmsearch(qList, docTermDict, termDocDict):
"""
In this function we will call functions to build the vectors, calculate similarity, and rank docs
:param qList:
:param docTermDict:
:param termDocDict:
:return:
"""
global rows, cols
docVec = createDocVectors(qList, docTermDict, termDocDict)
qVec = createQueryVector(qList)
# --------------------------------------------------------
print "Looking for documents matching your query..."
docScore = np.zeros((rows, 1))
for i in range(rows):
docScore[i] = np.asscalar(np.dot(qVec, docVec[i].reshape(cols, 1))) / (np.linalg.norm(docVec[i].reshape(1, cols)) * np.linalg.norm(qVec))
rankDocAndShow(docScore, 15)
# print "Done!"
def test():
'''qList = getQuery()
(docTermDict, termDocDict) = getDocIndex()
docVec = createDocVectors(qList, docTermDict, termDocDict)
qVec = createQueryVector(qList)
print qVec'''
pass
def main():
"""
:return:
"""
(docTermDict, termDocDict) = getDocIndex()
ch = 'x'
while ch != '2':
ch = raw_input("\nEnter 1 to fire query\n\t\t2 to exit:")
if ch == '1':
qList = getQuery()
vsmsearch(qList, docTermDict, termDocDict)
elif ch not in ('1', '2'):
print "\nWrong input! Try again."
ch = 'x'
if __name__ == '__main__':
# test()
main()
# EOF