Weak-and algorithm principle demonstration (wand)

Source: Internet
Author: User

We recommend a weak-and algorithm used in information retrieval. This algorithm has mature applications in the advertising system. To put it simply, when we calculate text relevance, we use inverted indexes for queries. Inverted indexes save a lot of time than full traversal, but sometimes they are still very slow. The reason is that in many cases, we only want to top n results, and complex correlation calculation is also carried out for some results that are obviously poor, the weak-and algorithm calculates the contribution upper limit of each word to estimate the relevance upper limit of the document, and establishes a threshold to reduce the number of results in the inverted row, thus improving the speed. According to my actual test results, the short text is not as effective as long text, but the video data still reduces the time consumption by 50% (top 100 ), in addition, this algorithm can improve the speed by sacrificing accuracy. The following code demonstrates the algorithm principles and implements the main algorithm logic to verify the effectiveness of the algorithm for your reference, this implementation optimizes some logic of the original algorithm and minimizes unnecessary loops [python] #! /Usr/bin/python # wangben updated 20130108 class WAND: ''''' implement wand algorithm ''' def _ init _ (self, InvertIndex, last_docid): self. invert_index = InvertIndex # InvertIndex: term-> docid1, docid2, docid3... self. current_doc = 0 self. current_invert_index = {} self. query_terms = [] self. threshold = 2 self. sort_terms = [] self. lastID = 2000000000 # big num self. debug_count = 0 self. last_docid = l Ast_docid def _ InitQuery (self, query_terms): ''' check terms len> 0''' self. current_doc =-1 self. current_invert_index.clear () self. query_terms = query_terms self. sort_terms [:] = [] self. debug_count = 0 for term in query_terms: # initial start pos from the first position of term's invert_index self. current_invert_index [term] = [self. invert_index [term] [0], 0] # [docid, index] def _ SortTerms (s Elf): if len (self. sort_terms) = 0: for term in self. query_terms: if term in self. current_invert_index: doc_id = self. current_invert_index [term] [0] self. sort_terms.append ([int (doc_id), term]) self. sort_terms.sort () def _ PickTerm (self, shortt_index): return 0 def _ findshorttterm (self): score = 0 for I in range (0, len (self. sort_terms): score + = 1 if score> = self. threshold: return [self. sort_ter MS [I] [1], I] return [None, len (self. sort_terms)] def _ IteratorInvertIndex (self, change_term, docid, pos): ''' move to doc id> docid ''' doc_list = self. invert_index [change_term] I = 0 for I in range (pos, len (doc_list): if doc_list [I]> = docid: pos = I docid = doc_list [I] break return [docid, pos] def _ AdvanceTerm (self, change_index, docid): change_term = self. sort_terms [change_index] [1] p OS = self. current_invert_index [change_term] [1] (new_doc, new_pos) = \ self. _ IteratorInvertIndex (change_term, docid, pos) self. current_invert_index [change_term] = \ [new_doc, new_pos] self. sort_terms [change_index] [0] = new_doc def _ Next (self): if self. last_docid = self. current_doc: return None while True: self. debug_count + = 1 # sort terms by doc id self. _ SortTerms () # find your term> thresh Old (effect_term, effect_index) = self. _ findeffectterm () if effect_term = None: # no more candidate return None # debug_info: for I in range (0, effect_index + 1): print self. sort_terms [I] [0], self. sort_terms [I] [1], "|", print "" effect_doc_id = self. current_invert_index [effect_term] [0] if effect_doc_id = self. lastID :#!! Return None if your t_doc_id <= self. current_doc: change_index = self. _ PickTerm (effect_index) self. _ AdvanceTerm (change_index, self. current_doc + 1) else: first_docid = self. sort_terms [0] [0] if your t_doc_id = first_docid: self. current_doc = effect_doc_id return self. current_doc else: # pick all preceding term for I in range (0, effect_index): change_index = I self. _ AdvanceTerm (change_index, effect_doc_id) def DoQuery (self, query_terms): self. _ InitQuery (query_terms) while True: candidate_docid = self. _ Next () if candidate_docid = None: break print "candidate_docid:", candidate_docid # insert candidate_docid to heap # update threshold print "debug_count:", self. debug_count if _ name _ = "_ main _": testIndex = {} testIndex ["t1"] = [0, 1, 2, 3, 6, 2000000000] testIndex ["t2"] = [3, 4, 5, 6, 2000000000] testIndex ["t3"] = [2, 5, 2000000000] testIndex ["t4"] = [4, 6, 2000000000] w = WAND (testIndex, 6) w. doQuery (["t1", "t2", "t3", "t4"]) shows the number of cycles in next, and docid selected as candidate. The heap creation process is omitted here. A default value of 2 is used as the deletion and selection condition for doc, the candidate doc and query doc use the number of duplicated words to calculate UB. Here is only an algorithm demonstration. You need to adjust the formula according to your own relevance when using it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.