def test_for_non_operators(self): self.assertFalse(sy.isOperator("1")) self.assertFalse(sy.isOperator("(")) self.assertFalse(sy.isOperator(")")) self.assertFalse(sy.isOperator("[")) self.assertFalse(sy.isOperator("]")) self.assertFalse(sy.isOperator("a")) self.assertFalse(sy.isOperator("x")) self.assertFalse(sy.isOperator("d")) self.assertFalse(sy.isOperator("p")) self.assertFalse(sy.isOperator("m")) self.assertFalse(sy.isOperator("|")) self.assertFalse(sy.isOperator("~")) self.assertFalse(sy.isOperator("\\"))
def test_wrong_input_type(self): with self.assertRaises(TypeError): sy.isOperator(1)
def test_for_operators(self): self.assertTrue(sy.isOperator("+")) self.assertTrue(sy.isOperator("-")) self.assertTrue(sy.isOperator("*")) self.assertTrue(sy.isOperator("/"))
def test_empty_char(self): with self.assertRaises(ValueError): sy.isOperator('')
def test_a_string(self): with self.assertRaises(ValueError): sy.isOperator('21')
def test_non_operator(self): isOperator = sy.isOperator('0') self.assertFalse(isOperator)
def test_an_operator(self): isOperator = sy.isOperator('-') self.assertTrue(isOperator)
def search3(self,query): "search for many query with boolean expression" root = self.inverted_index.root query_list = query.split() expression = shunting_yard.infixToRPN(query_list) expression.reverse() stack = [] plist_num=0 plist_hashlist={} #[2012/08/22 added] to initial variables to find score slist_num=0 slist_hashlist={} # just find that term if doing only single word query. if len(expression) ==0: return [] elif len(expression) <= 1: word = expression.pop() answer_last= self.inverted_index.lookup(root,word) # get node to find tf-idf node_answer_last = self.inverted_index.lookupNode(root,word) if(node_answer_last==None): return [] tf_idf_list = node_answer_last.get_tf_idf_list(self.inverted_index.docCount,self.inverted_index.highest_tc) ranked_answer = Search_System.rank_answer(answer_last,tf_idf_list) return ranked_answer # in case of > 1 word query while len(expression)!=0: token = expression.pop() if shunting_yard.isOperator(token): word1 = stack.pop() if( not word1 in plist_hashlist ): answer_list1 = self.inverted_index.lookup(root,word1) # [2012/08/22 added] to get score of word node1 = self.inverted_index.lookupNode(root,word1) if(node1!=None): score_list1 = node1.get_tf_idf_list(self.inverted_index.docCount,self.inverted_index.highest_tc) else: score_list1 = [] else: answer_list1 = plist_hashlist[word1] # [2012/08/22 added] to get score of word score_list1 = slist_hashlist[word1] word2 = stack.pop() if( not word2 in plist_hashlist ): answer_list2 = self.inverted_index.lookup(root,word2) # [2012/08/22 added] to get score of word node2 = self.inverted_index.lookupNode(root,word2) if(node2!=None): score_list2 = node2.get_tf_idf_list(self.inverted_index.docCount,self.inverted_index.highest_tc) else: score_list2 = [] else: answer_list2 = plist_hashlist[word2] # [2012/08/22 added] to get score of word score_list2 = slist_hashlist[word2] if(token=="&&"): answer_last = self.intersect(answer_list1,answer_list2) # [2012/08/22 added] to get score of word score_list_last = self.intersect_score(answer_list1,answer_list2,score_list1,score_list2) elif(token=="||"): answer_last = self.union(answer_list1,answer_list2) # [2012/08/22 added] to get score of word score_list_last = self.union_score(answer_list1,answer_list2,score_list1,score_list2) else: print("Wrong Operator: program will exit") exit() stack.append("plist"+str(plist_num)) plist_hashlist["plist"+str(plist_num)] = answer_last plist_num+=1 #[2012/08/22 added] to store new score list in hash table slist_hashlist["plist"+str(slist_num)] = score_list_last slist_num+=1 else: stack.append(token) # get all the last answers #[2012/08/22 added] to store new score list in hash table last_word = stack.pop() answer = plist_hashlist[last_word]; #[2012/08/22 added] to get answer score list in hash table s_answer = slist_hashlist[last_word] print(answer) print(s_answer) ranked_answer = Search_System.rank_answer(answer,s_answer) return ranked_answer