The following is a keyword matching algorithm based on KWIC (implemented in pipeline + filter mode)
The key part of the pipeline + filter software system implementation, in many of the keyword search platform using this cycle SHIFT + sort output keyword matching algorithm:
The specific requirements are as follows:
1, using the pipe-filter style:
Each filter processes the data and then sends the result to the next filter. The filter begins to work when data is passed in. Data sharing between filters is strictly limited to pipeline transmission
Four filters:
Inputs (Input filter):
Reads the input file from the data source, parses the format, writes the rows to the output pipeline
Shift (Circularshifter filter): Cyclic shift
Sort (Alphabetizer filter):
Outputs (output filter)
Pipeline:
In_cs Pipe
Cs_al Pipe
Al_ou Pile
For example:
The code is as follows:
Using system;using system.collections.generic;using system.linq;using system.text;using System.IO;namespace KWIC{// <summary>//////</summary> public class Pipe {list<string> word; Public list<string> Read () {return word; public void Write (list<string> word) {This.word = Word;} }///<summary>///filter interface between////</summary> public abstract class Filter {Publi C virtual void Transform () {}}//<summary>///inherit and implement pipeline interface///</summary> public CLA SS Inputfilter:filter {public Pipe outpipe; public list<string> Word; Public Inputfilter (list<string> Word, Pipe outpipe) {This.word = Word; This.outpipe = Outpipe; } public void Transform () {outpipe.write (word); }}///<summary>///Inherit and implement filter connectionPort///</summary> public class Circleshiftfilter:filter {public Pipe inputpipe; Public Pipe Outpipe; Public Circleshiftfilter (pipe inputpipe, pipe outpipe) {this.inputpipe = Inputpipe; This.outpipe = Outpipe; }///<summary>///Key cyclic shift function////</summary> public virtual void Transform () {List<string> word = inputpipe.read (); Supplemental code that shifts the string in the word array to the loop/////////////////////////////////////////// list<string> turned_words = new list<string> (); Get each line of String data foreach (string in Word) {//split a sentence string[] words = line. Split ('); Gets the number of words ulong Word_number = (ULONG) words. Longlength; Temporary storage Intermediate sorted string list<string> tmp_words = new List<strIng> (); Tmp_words. Clear (); Tmp_words. ADD (line); String tmp_line = ""; for (ULONG i = 0; i < word_number-1; i++) {//Get previous string tmp_line = Tmp_words[tmp_words. COUNT-1]; Gets the last word of the previous line string Last_word = Tmp_line. Split (') [word_number-1]; Gets all the words except the last word in the previous line string left_words = Tmp_line. Substring (0, (tmp_line). Length-last_word. LENGTH-1)); Tmp_words. ADD (Last_word + "" + left_words); }//Remove the original string tmp_words. RemoveAt (0); Adds a shift string to the temporary list collection turned_words. AddRange (tmp_words); }//Add all the shifted strings to the original list collection word. AddRange (turned_words); Outpipe.write (word); }}///<summary&Gt Sort filter class implemented///</summary> public class Alphafilter:filter {public Pipe inputpipe; Public Pipe Outpipe; Public Alphafilter (pipe inputpipe, pipe outpipe) {this.inputpipe = Inputpipe; This.outpipe = Outpipe; }//<summary>///Sort output function///</summary> public void Transform () { List<string> Word = Inputpipe.read (); Supplemental code to sort words in Word array output/////////////////////////////////////////////////wor D.sort (); Outpipe.write (word); }}///<summary>//Implement output Filter interface class///</summary> public class Outputfilter:filter { Public Pipe Inputpipe; Public Pipe Outpipe; Public Outputfilter (pipe inputpipe, pipe outpipe) {this.inputpipe = inputpipe; this.outpipe = Outpipe; } public void TransfORM () {list<string> word = inputpipe.read (); Outpipe.write (word); }}///<summary>///program's overall operating framework///</summary> public class Kwic_system {Pipe in_cs; Create three objects of pipe pipe cs_al; And one object of type Pipe Al_ou; FileInputStream Pipe ou_ui; FileInputStream Inputfilter Inputfilter; Circleshiftfilter Shifter; Alphafilter Alpha; Outputfilter output; Output to screens public Kwic_system () {in_cs = new pipe ();//create three objects of pipe Cs_al = new Pipe (); And one object of type Al_ou = new Pipe (); FileInputStream ou_ui = new Pipe (); FileInputStream list<string> Word = new list<string> (); Word. ADD (Regex.Replace ("I Love You"). Trim (), @ "\s+", "")); The regular gets all types of spaces (such as tabs, new lines, and so on), and then replaces them with a space word. ADD (Regex.Replace ("Me Too"). Trim (), @ "\s+", "")); Word. ADD (Regex.Replace ("Do Know"). Trim (), @ "\s+", "")); Inputfilter = new Inputfilter (Word, in_cs); Shifter = new Circleshiftfilter (In_cs, Cs_al); Alpha = new Alphafilter (Cs_al, Al_ou); Output = new Outputfilter (AL_OU,OU_UI); Output to screens} public list<string > GetResult () {inputfilter.transform (); Shifter. Transform (); Alpha. Transform (); Output. Transform (); return Ou_ui.read (); } }}
(Note: If you want to change lines here to line the output, you need to end the output of each line at the end of the "\ r \ n")
In a wide range of search technologies, this keyword matching algorithm has a wide range of applications, such as our common Baidu and Google's search keyword prompt function.