Click here to Skip to main content
15,860,943 members
Articles / Programming Languages / C#
Article

Aho-Corasick string matching in C#

Rate me:
Please Sign up or sign in to vote.
4.88/5 (39 votes)
3 Dec 2005CPOL5 min read 257.1K   7.8K   122   33
A C# implementation of the very efficient Aho-Corasick keyword matching algorithm with multiple keywords support.

Introduction

In this article, I will describe the implementation of an efficient Aho-Corasick algorithm for pattern matching. In simple words, this algorithm can be used for searching a text for specified keywords. The following code is useful when you have a set of keywords and you want to find all occurrences of a keywords in the text or check if any of the keywords is present in the text. You should use this algorithm especially if you have a large number of keywords that don't change often, because in this case, it is much more efficient than other algorithms that can be simply implemented using the .NET class library.

Aho-Corasick algorithm

In this section, I'll try to describe the concept of this algorithm. For more information and for a more exact explanation, please take a look at the links at the end of this article. The algorithm consists of two parts. The first part is the building of the tree from keywords you want to search for, and the second part is searching the text for the keywords using the previously built tree (state machine). Searching for a keyword is very efficient, because it only moves through the states in the state machine. If a character is matching, it follows goto function otherwise it follows fail function.

Tree building

In the first phase of the tree building, keywords are added to the tree. In my implementation, I use the class StringSearch.TreeNode, which represents one letter. The root node is used only as a place holder and contains links to other letters. Links created in this first step represents the goto function, which returns the next state when a character is matching.

During the second phase, the fail and output functions are found. The fail function is used when a character is not matching and the output function returns the found keywords for each reached state. For example, in the text "SHIS", the failure function is used to exit from the "SHE" branch to "HIS" branch after the first two characters (because the third character is not matching). During the second phase, the BFS (breadth first search) algorithm is used for traversing through all the nodes. Functions are calculated in this order, because the fail function of the specified node is calculated using the fail function of the parent node.

Building of the keyword tree (figure 1 - after the first step, figure 2 - tree with the fail function)

Searching

As I already mentioned, searching only means traversing the previously built keyword tree (state machine). To demonstrate how this algorithm works, let's look at the commented method which returns all the matches of the specified keywords:

C#
// Searches passed text and returns all occurrences of any keyword
// Returns array containing positions of found keywords
public StringSearchResult[] FindAll(string text)
{
  ArrayList ret=new ArrayList(); // List containing results
  TreeNode ptr=_root;            // Current node (state)
  int index=0;                   // Index in text

  // Loop through characters
  while(index<text.Length)
  {
    // Find next state (if no transition exists, fail function is used)
    // walks through tree until transition is found or root is reached
    TreeNode trans=null;
    while(trans==null)
    {
      trans=ptr.GetTransition(text[index]);
      if (ptr==_root) break;
      if (trans==null) ptr=ptr.Failure;
    }
    if (trans!=null) ptr=trans;

    // Add results from node to output array and move to next character
    foreach(string found in ptr.Results)
      ret.Add(new StringSearchResult(index-found.Length+1,found));
    index++;
  }
  
  // Convert results to array
  return (StringSearchResult[])ret.ToArray(typeof(StringSearchResult));
}

Algorithm complexity

Complexity of the first part is not so important, because it is executed only once. Complexity of the second part is O(m+z) where m is the length of the text and z is the number of found keywords (in simple words, it is very fast and it's speed doesn't drop quickly for longer texts or many keywords).

Performance comparison

To show how efficient this algorithm is, I created a test application which compares this algorithm with two other simple methods that can be used for this purpose. The first algorithm uses the String.IndexOf method to search the text for all the keywords, and the second algorithm uses regular expressions - for example, for keywords he, she, and his, it creates a regular expression (he|she|his). The following graphs show the results of tests for two texts of different sizes. The number of used keywords is displayed on the X axis and the time of search is displayed on the Y axis.

The interesting thing is that for less than 70 keywords, it is better to use a simple method using String.IndexOf. Regular expressions are almost always slower than other algorithms. I also tried compiling the test under both .NET 1.1 and .NET 2.0 to see the difference. Although my measuring method may not be very precise, it looks like .NET 2.0 is a bit faster (about 5-10%), and the method with regular expressions gives much better results (about 60% faster).

Two charts comparing the speed of the three described algorithms - Aho-Corasick (green), IndexOf (blue), and Regex (yellow)

How to use the code

I decided to implement this algorithm when I had to ban some words in a community web page (vulgarisms etc.). This is a typical use case because searching should be really fast, but blocked keywords don't change often (and the creation of the keyword tree can be slower).

The search algorithm is implemented in a file StringSearch.cs. I created the interface that represents any search algorithm (so it is easy to replace it with another implementation). This interface is called IStringSearchAlgorithm, and it contains a property Keywords (gets or sets keywords to search for) and methods for searching. The method FindAll returns all the keywords in the passed text, and FindFirst returns the first match. Matches are represented by the StringSearchResult structure that contains the found keyword and its position in the text. The last method is ContainsAny, which returns true when the passed text contains a keyword. The class that implements the Aho-Corasick algorithm is called StringSearch.

Initialization

The following example shows how to load keywords from a database and create a SearchAlgorithm instance:

C#
// Initialize DB connection
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT BlockedWord" + 
                                " FROM BlockedWords",conn);
conn.Open();

// Read list of banned words
ArrayList listWords = new ArrayList();
using(SqlDataReader reader = 
  cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
  while(reader.Read()) 
    listWords.Add(myReader.GetString(0));
}
string[] arrayWords = (string[])listWords.ToArray(typeof(string));

// Create search algorithm instance
IStringSearchAlgorithm searchAlg = new StringSearch();
searchAlg.Keywords = arrayWords;

You can also use the StringSearch constructor which takes an array of keywords as parameter.

Searching

Searching the passed text for keywords is even easier. The following sample shows how to write all the matches to the console output:

C#
// Find all matching keywords  
StringSearchResult[] results=searchAlg.FindAll(textToSearch);

// Write all results  
foreach(StringSearchResult r in results)
{
  Console.WriteLine("Keyword='{0}', Index={1}", r.Keyword, r.Index);
}

Conclusion

This implementation of the Aho-Corasick search algorithm is very efficient if you want to find a large number of keywords in a text of any length, but if you want to search only for a few keywords, it is better to use a simple method like String.IndexOf. The code can be compiled in both .NET 1.1 and .NET 2.0 without any modifications. If you want to learn more about this algorithm, take a look at the link in the next section, it was very useful for me during the implementation of the algorithm and explains the theory behind this algorithm.

Links and references

Future work and history

  • 12/03/2005 - First version of this article published at CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Czech Republic Czech Republic
I live in Prague, the capital city of Czech republic (most of the time Smile | :) ). I've been very interested in functional programming recently and I have a passion for the new Microsoft F# language. I'm writing a book about Functional Programming in the Real World that shows the ideas using examples in C# 3.0 and F#.

I've been Microsoft MVP (for C#) since 2004 and I'm one of the most active members of the F# community. I'm a computer science student at Charles University of Prague. My hobbies include photography, fractals and of course many things related to computers (except fixing them). My favorite book writers are Terry Pratchett and Philip K Dick and I like paintings by M. C. Escher.

PS: My favorite codeproject icon is Sheep | [baah] .

Comments and Discussions

 
Questionplease help Pin
Member 1143886118-Feb-15 0:22
Member 1143886118-Feb-15 0:22 
QuestionNeed help in string matching Pin
vijayct0317-Aug-14 22:06
vijayct0317-Aug-14 22:06 
QuestionUnable to run it. Pin
The Enigmatic Coder29-May-14 23:20
The Enigmatic Coder29-May-14 23:20 
QuestionVery Nice Pin
Member 1035655623-Oct-13 21:25
Member 1035655623-Oct-13 21:25 
GeneralMy vote of 5 Pin
hbgh4-Sep-12 21:19
hbgh4-Sep-12 21:19 
GeneralMy vote of 5 Pin
lyhuy6-Jun-11 22:55
lyhuy6-Jun-11 22:55 
Generalfailure functions Pin
User 4306469-Jun-10 5:51
User 4306469-Jun-10 5:51 
GeneralC++ version Pin
johns42011-Sep-09 7:31
johns42011-Sep-09 7:31 
Generalskip multiple occurences of char (to be search) from string. Pin
Santosh Ingawale10-Jun-09 22:52
Santosh Ingawale10-Jun-09 22:52 
Generaldifferent keys supported with this algorithm Pin
chuckdawit19-Nov-08 18:42
chuckdawit19-Nov-08 18:42 
GeneralGenericList Pin
iridescence12-Sep-08 5:29
iridescence12-Sep-08 5:29 
Generalwith Tokens instead of char Pin
vj2111-May-08 11:57
vj2111-May-08 11:57 
GeneralGreate Article Pin
ashu_himt15-Apr-08 3:00
ashu_himt15-Apr-08 3:00 
GeneralCPU utilization on a multi-core machine Pin
ItemsThatNeedAttention17-Mar-08 5:12
ItemsThatNeedAttention17-Mar-08 5:12 
GeneralIndeed ver nice job ... Pin
WilenX10-Sep-07 7:42
WilenX10-Sep-07 7:42 
GeneralRe: Indeed ver nice job ... Pin
Tomas Petricek10-Sep-07 7:55
Tomas Petricek10-Sep-07 7:55 
QuestionI need some help Pin
stefan stoian12-Sep-06 5:02
stefan stoian12-Sep-06 5:02 
GeneralSpecialize to "Find if all in string" Pin
l_d_allan28-Feb-06 4:43
l_d_allan28-Feb-06 4:43 
GeneralRe: Specialize to "Find if all in string" Pin
Lior Kaduri14-Jun-06 2:06
Lior Kaduri14-Jun-06 2:06 
GeneralVery strange tests... Pin
Ivan A. Gusev12-Jan-06 22:46
Ivan A. Gusev12-Jan-06 22:46 
GeneralGreat! some improvements Pin
Lior Kaduri3-Jan-06 23:26
Lior Kaduri3-Jan-06 23:26 
GeneralRe: Great! some improvements Pin
rmircea14-Jun-06 0:14
rmircea14-Jun-06 0:14 
GeneralRe: Great! some improvements Pin
lutzeslife13-Aug-14 23:41
lutzeslife13-Aug-14 23:41 
C#
void InnerFindAll(string text, int maxKeywords, int maxUniqueKeywords, out StringSearchResult[] allMatches, out StringSearchResult[] uniqueMatches)
        {
            var allMatchesList = new ArrayList();
            var uniqueMatchesList = new HybridDictionary(maxUniqueKeywords);

            var ptr = _keywordTree;
            var index = 0;

            while (index == maxUniqueKeywords || allMatchesList.Count >= maxKeywords)
            {
                break;
                index++;
            }

            // set the output variables
            allMatches = (StringSearchResult[])allMatchesList.ToArray(typeof(StringSearchResult));
            uniqueMatches = new StringSearchResult[uniqueMatchesList.Count];
            uniqueMatchesList.Values.CopyTo(uniqueMatches, 0);
        }

I try to use your improvments but the InnerFindAll Function is incomplempete. Can you publish the completed function?
Best regards
Daniel

QuestionLicense Pin
leppie30-Dec-05 23:13
leppie30-Dec-05 23:13 
GeneralPlease remove source control Pin
Gary Thom5-Dec-05 8:30
Gary Thom5-Dec-05 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.