https://github.com/lofcz/ahocorasick
https://github.com/lofcz/ahocorasick
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lofcz/ahocorasick
- Owner: lofcz
- License: mit
- Created: 2024-09-07T19:52:58.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-03T12:05:24.000Z (about 1 year ago)
- Last Synced: 2025-02-15T18:58:22.958Z (8 months ago)
- Language: C#
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AhoCorasick
## Install
```
dotnet add package AhoCorasickCore
```## Use
This implementation of [Aho-Corasick](https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm) can be used in scenarios where a string needs to be matched against several substrings, and each substring is assigned a certain meaning. For example, one could scan an e-mail against a few words known to be used by spammers and trigger some follow-up actions on each match. Instead of doing that linearly (e.g., by calling `Contains` on each needle), AhoCorasick and similar algorithms scan efficiently by reusing the already traversed space.
A minimal example:
```cs
enum WordCategory
{
Noun,
Verb,
Adjective,
Adverb
}Dictionary patterns = new Dictionary
{
{"he", WordCategory.Noun},
{"she", WordCategory.Noun},
{"his", WordCategory.Adjective},
{"hers", WordCategory.Adjective},
{"run", WordCategory.Verb},
{"quickly", WordCategory.Adverb}
};// cache the instance and reuse it, all public methods are thread-safe
AhoCorasick inst = new AhoCorasick(patterns);// use Search() for consuming hits via yield
List> results = inst.SearchAll("he runs")/* returns: [
{pattern: "he", value: (Noun), pos: 0},
{pattern: "run", value: (Verb), pos: 3}
] */
```