JavaScript Tens of thousands of keyword instant match--with the help of hash table fast matching algorithm

Source: Internet
Author: User

Source:

Http://www.cnblogs.com/index-html/archive/2013/04/17/js_keyword_match.html

Http://www.etherdream.com/funnyscript/Keyword/Keyword.html

For multi-keyword, large text matching, if the keyword only one, it is only the simplest string matching (match one), does not show the role.

varTreesearch ={maketree:function(strkeys) {"Use Strict"; varTblcur ={}, Tblroot, Key, Str_key, Length, J, I        ; Tblroot=tblcur;  for(j = strkeys.length-1; J >= 0; J-= 1) {Str_key=Strkeys[j]; Length=str_key.length;  for(i = 0; i < Length; i + = 1) {Key=Str_key.charat (i); if(Tblcur.hasownproperty (key)) {//Generating child nodesTblcur =Tblcur[key]; } Else{tblcur= Tblcur[key] = {}; }} tblcur.end=true;//Last keyword does not have a separatorTblcur =Tblroot; }        returnTblroot; }, Search:function(content, tblroot) {"Use Strict"; varTblcur, P_star= 0, N=Content.length, P_end, match,//If a match is foundMatch_key, Match_str, Arrmatch= [],//Store ResultsArrlength = 0//length index of the Arrmatch            ;  while(P_star <N) {tblcur= Tblroot;//backtrack to the rootP_end =P_star; Match_str= ""; Match=false;  Do{Match_key=Content.charat (p_end); if(! (tblcur = Tblcur[match_key])) {//this match endsP_star + = 1;  Break; }Else{match_str+=Match_key; } p_end+ = 1; if(Tblcur.end = = =true)//match to tail//Find match keyword{Match=true; }            }  while(true); if(Match = = =true) {//Maximum MatchArrmatch[arrlength] = {//Enhanced ReadabilityKey:match_str, Begin:p_star-1, end:p_end}; Arrlength+ = 1; P_star=P_end; }        }        returnArrmatch; }};
View Code
functionTest (strcontent, Strkeys) {varArrmatch, Tblroot=Treesearch.maketree (strkeys), T=NewDate (); Arrmatch=Treesearch.search (strcontent, tblroot); Console.log ("Time is:" + (NewDate ()-T) + "MM"); Console.log (Arrmatch);}vars = (function() {    varThings = [' ', ' \ n ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' n ', ' O ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' Q ', ' R ', ' s ', ' t ', ' u ' , ' V ', ' w ', ' x ', ' y ', ' z ']; vars = "";  for(vari = 1000000; I >= 0; i--) {s+ = Things[parseint (Math.random () * things.length)%Things.length]}; returns;}) () test (s, ["ABC", "Efge", "Fun", "tree"]);
View Code

JavaScript Tens of thousands of keyword instant match--with the help of hash table fast matching algorithm

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.