AhoCorasickDoubleArrayTrie

repository·master·Indexed 21 days ago

https://github.com/hankcs/ahocorasickdoublearraytrie

A high-performance Java implementation of the Aho-Corasick algorithm using a Double Array Trie structure. It provides O(n) time complexity for text parsing and supports persistence via save and load methods, custom value mapping, and memory-efficient parsing using callbacks or lambda expressions.

Tokens
849
Snippets
3
Records
3
Agent score
27%

What's inside ahocorasickdoublearraytrie

  1. Install AhoCorasickDoubleArrayTrie via Maven or Gradle

    master

    To use this library, add the following dependency to your project configuration. Always check Maven Central for the latest version.

    Maven (pom.xml):

    <dependency>
      <groupId>com.hankcs</groupId>
      <artifactId>aho-corasick-double-array-trie</artifactId>
      <version>1.2.3</version>
    </dependency>

    Gradle (build.gradle.kts):

    implementation("com.hankcs:aho-corasick-double-array-trie:1.2.2")
    <dependency>
      <groupId>com.hankcs</groupId>
      <artifactId>aho-corasick-double-array-trie</artifactId>
      <version>1.2.3</version>
    </dependency>
  2. Basic usage of AhoCorasickDoubleArrayTrie

    master

    To perform keyword matching, follow these steps:

    1. Create a Map<String, V> containing your dictionary (where the key is the keyword and the value is an optional object associated with it).
    2. Instantiate AhoCorasickDoubleArrayTrie<V>.
    3. Call .build(map) to construct the Double Array Trie structure.
    4. Use .parseText(text) to find all occurrences in a given string.

    Note: The instance is thread-safe after the .build() method has been called.

    Additional capabilities:

    • Persistence: Use .save() to store the trie to disk and .load() to restore it.
    • Custom Values: You can map keywords to any object using Map<String, SomeObject> to retrieve metadata during parsing.
    // Collect test data set
    TreeMap<String, String> map = new TreeMap<String, String>();
    String[] keyArray = new String[] {"hers", "his", "she", "he"};
    for (String key : keyArray) {
        map.put(key, key);
    }
    
    // Build an AhoCorasickDoubleArrayTrie
    AhoCorasickDoubleArrayTrie<String> acdat = new AhoCorasickDoubleArrayTrie<String>();
    acdat.build(map);
    
    // Test it
    final String text = "uhers";
    List<AhoCorasickDoubleArrayTrie.Hit<String>> wordList = acdat.parseText(text);
  3. Parse text using a callback (IHit or Lambda)

    master

    If you do not need to collect all results into a list, you can use a callback mechanism to process hits immediately. This is more memory-efficient for large result sets.

    You can provide an implementation of the AhoCorasickDoubleArrayTrie.IHit<V> interface or use a lambda expression. The callback receives the begin index, end index, and the associated value.

    // Using an anonymous inner class (IHit)
    acdat.parseText(text, new AhoCorasickDoubleArrayTrie.IHit<String>()
    {
        @Override
        public void hit(int begin, int end, String value)
        {
            System.out.printf("[%d:%d]=%s\n", begin, end, value);
        }
    });
    
    // Using a lambda function
    acdat.parseText(text, (begin, end, value) -> {
        System.out.printf("[%d:%d]=%s\n", begin, end, value);
    });