def test_text_end_situation(self): kwtree = KeywordTree() kwtree.add('blaaaaaf') kwtree.add('a') kwtree.finalize() result = kwtree.search_one('bla') self.assertEqual(('a', 2), result)
def ahocorasick_any_match(text_info): kwtree_any = KeywordTree(case_insensitive=True) bool_name = False bool_lang = False bool_type = False bool_tags = False bool_categories = False """ CREATE TREE """ ## if name condition is empty, (name is a string) if not text_info[1][2]: bool_name = True else: kwtree_any.add( text_info[1][2]) ## add name condition into aho corasick tree ## if lang condition is empty, (lang is a string) if not text_info[1][3]: bool_lang = True else: kwtree_any.add( text_info[1][3]) ## add lang condition into aho corasick tree ## if type condition is empty, (type is a string) if not text_info[1][4]: bool_type = True else: kwtree_any.add( text_info[1][4]) ## add type condition into aho corasick tree ## if tags condition is empty, (tags is a list) if not text_info[1][5]: bool_name = True else: for tag in text_info[1][5]: kwtree_any.add(tag) ## add tag conditions into aho corasick tree ## if categories condition is empty, (categories is a list) if not text_info[1][6]: bool_name = True else: for categ in text_info[1][6]: kwtree_any.add( categ) ## add categories conditions into aho corasick tree kwtree_any.finalize() """ ANY MATCH """ ## name if kwtree_any.search_one(text_info[1][7]["name"]): bool_name = True ## lang if kwtree_any.search_one(text_info[1][7]["lang"]): bool_lang = True ## type if kwtree_any.search_one(text_info[1][7]["type"]): bool_type = True ## tags tags = helper_list_to_str(text_info[1][7]["tags"]) if kwtree_any.search_one(tags): bool_tags = True ## categories categs = helper_list_to_str(text_info[1][7]["categories"]) if kwtree_any.search_one(categs): bool_categories = True """ RESULT """ if bool_name and bool_lang and bool_type and bool_tags and bool_categories: return text_info else: return False