-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolean_model.py
More file actions
135 lines (112 loc) · 4.4 KB
/
boolean_model.py
File metadata and controls
135 lines (112 loc) · 4.4 KB
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
import os
import re
from collections import defaultdict
from glob import glob
import numpy as np
from nltk.corpus import stopwords
from nltk.stem.snowball import SnowballStemmer
from nltk.tokenize import word_tokenize
from query_tools import get_type_of_token, infix_to_postfix
from sympy import to_dnf, Symbol
class BooleanModel():
'''Boolean Model class for information retrieval'''
def __init__(self, documents, vocabulary_dict: dict[str, dict[int, int]]) -> None:
# self.trie = trie
self.documents = documents
self.vocabulary_dict = vocabulary_dict
def query(self, tokenized_query):
''' query the corpus documents using a boolean model
:param query: valid boolean expression to search for in the documents
:returns: a list of all marching documents
'''
# preprocess query
# print(tokenized_query)
processed_query = self.proccess_query(tokenized_query)
# print(tokenized_query)
# eval query and return relevant documents
return self.__eval_query(processed_query)
def proccess_query(self, tokenized_query):
n_tokenized_query = [tokenized_query[0]]
for i in range(1,len(tokenized_query)):
if get_type_of_token(tokenized_query[i-1]) == 4:
if get_type_of_token(tokenized_query[i]) == 4:
n_tokenized_query.append("&")
n_tokenized_query.append(tokenized_query[i])
else:
n_tokenized_query.append(tokenized_query[i])
continue
n_tokenized_query.append(tokenized_query[i])
query = " ".join(n_tokenized_query)
if query.find("|") != -1:
try:
dnf_q = str(to_dnf(query))
query = dnf_q
except:
print("error converting to dnf")
query = query.split()
return query
def __eval_query(self, tokenized_query):
''' Evaluates the query with the preprovcessed corpus
:param tokenized_query: list of tokens in the query (postfix form)
:returns: list of relevant document names
'''
tokenized_query = infix_to_postfix(tokenized_query)
operands = []
for token in tokenized_query:
# print( token)
if get_type_of_token(token) == 3:
right_op = operands.pop()
left_op = operands.pop()
result = self.__eval_operation(left_op, right_op, token)
operands.append(result)
else:
operands.append(self.__relevants(token))
if len(operands) != 1:
print("Malformed query or postfix expression")
return list()
# Find out documents corresponding to set bits in the vector
return operands.pop()
def __eval_operation(self, left, right, op):
"""Performs specified operation on the vectors
:param left: left operand
:param right: right operand
:param op: operation to perform
:returns: result of the operation
"""
if op == "&":
res = []
for i in left:
if i in right:
res.append(i)
return res
# return list(set(left).intersection(set(right)))
elif op == "|":
return left+right
# return list(set(left).union(set(right)))
else:
return []
def __relevants(self,word):
''' make a bitvector from the word'''
negate = False
# print('original word', word)
# If word is "~good"
if word[0] == "~":
negate = True
word = word[1:]
# node = self.trie.search(word)
relevant_docs = []
# print('word', word)
# print('negate', negate)
if word in self.vocabulary_dict:
if negate:
# print('mira')
relevant_docs = [ i for i in self.documents if not i in self.vocabulary_dict[word]]
# print(relevant_docs)
else:
relevant_docs = [ i for i in self.vocabulary_dict[word]]
else:
relevant_docs = [i for i in self.documents]
return relevant_docs
def remove_duplicates(self, text):
''' removes duplicates from text'''
return list(set(text))