# Matcher
A high-performance matcher designed to solve **LOGICAL** and **TEXT VARIATIONS** problems in word matching, implemented in Rust.
For detailed implementation, see the [Design Document](../DESIGN.md).
## Features
- **Multiple Matching Methods**:
- Simple Word Matching
- Regex-Based Matching
- Similarity-Based Matching
- **Text Transformation**:
- **Fanjian**: Simplify traditional Chinese characters to simplified ones.
Example: `่ฒ่ธ` -> `่ซ่น`
- **Delete**: Remove specific characters.
Example: `*Fu&*iii&^%%*&kkkk` -> `Fuiiikkkk`
- **Normalize**: Normalize special characters to identifiable characters.
Example: `๐ข๐ฐ๐๐ป๐ง ๐โแตฃโ๐!` -> `hello world!`
- **PinYin**: Convert Chinese characters to Pinyin for fuzzy matching.
Example: `่ฅฟๅฎ` -> ` xi an `, matches `ๆดๆ` -> ` xi an `, but not `ๅ
` -> ` xian `
- **PinYinChar**: Convert Chinese characters to Pinyin.
Example: `่ฅฟๅฎ` -> `xian`, matches `ๆดๆ` and `ๅ
` -> `xian`
- **AND OR NOT Word Matching**:
- Takes into account the number of repetitions of words.
- Example: `hello&world` matches `hello world` and `world,hello`
- Example: `ๆ &ๆณ&ๆ &ๅคฉ` matches `ๆ ๆ ๆณๅคฉ` (because `ๆ ` is repeated twice), but not `ๆ ๆณๅคฉ`
- Example: `hello~helloo~hhello` matches `hello` but not `helloo` and `hhello`
- **Customizable Exemption Lists**: Exclude specific words from matching.
- **Efficient Handling of Large Word Lists**: Optimized for performance.
## Usage
### Adding to Your Project
To use `matcher_rs` in your Rust project, run the following command:
```shell
cargo add matcher_rs
```
> [!IMPORTANT]
> **Git Dependency Limitation**: This crate currently depends on a git-based version of `aho-corasick-unsafe` (a fork of `aho-corasick`). As a result, projects depending on `matcher_rs` must also use a git dependency or the `matcher_rs` dependency will fail to resolve in some package registry environments.
### Explanation of the configuration
* `Matcher`'s configuration is defined by the `MatchTableMap = HashMap<u32, Vec<MatchTable>>` type, the key of `MatchTableMap` is called `match_id`, **for each `match_id`, the `table_id` inside is required to be unique**.
* `SimpleMatcher`'s configuration is defined by the `SimpleTable = HashMap<ProcessType, HashMap<u32, &str>>` type, the value `HashMap<u32, &str>`'s key is called `word_id`, **`word_id` is required to be globally unique**.
#### MatchTable
* `table_id`: The unique ID of the match table.
* `match_table_type`: The type of the match table.
* `word_list`: The word list of the match table.
* `exemption_process_type`: The type of the exemption simple match.
* `exemption_word_list`: The exemption word list of the match table.
For each match table, word matching is performed over the `word_list`, and exemption word matching is performed over the `exemption_word_list`. If the exemption word matching result is True, the word matching result will be False.
#### MatchTableType
* `Simple`: Supports simple multiple patterns matching with text normalization defined by `process_type`.
* It can handle combination patterns and repeated times sensitive matching, delimited by `&` and `~`, such as `hello&world&hello` will match `hellohelloworld` and `worldhellohello`, but not `helloworld` due to the repeated times of `hello`.
* `Regex`: Supports regex patterns matching.
* `SimilarChar`: Supports similar character matching using regex.
* `["hello,hallo,hollo,hi", "word,world,wrd,๐", "!,?,~"]` will match `helloworld!`, `hollowrd?`, `hi๐~` ยทยทยท any combinations of the words split by `,` in the list.
* `Acrostic`: Supports acrostic matching using regex **(currently only supports Chinese and simple English sentences)**.
* `["h,e,l,l,o", "ไฝ ,ๅฅฝ"]` will match `hope, endures, love, lasts, onward.` and `ไฝ ็็ฌๅฎนๆธฉๆ, ๅฅฝๅฟๆ
ๅธธไผดใ`.
* `Regex`: Supports regex matching.
* `["h[aeiou]llo", "w[aeiou]rd"]` will match `hello`, `world`, `hillo`, `wurld` ยทยทยท any text that matches the regex in the list.
* `Similar`: Supports similar text matching based on distance and threshold.
* `Levenshtein`: Supports similar text matching based on Levenshtein distance.
#### ProcessType
* `None`: No transformation.
* `Fanjian`: Traditional Chinese to simplified Chinese transformation. Based on [FANJIAN](./process_map/FANJIAN.txt).
* `ๅฆณๅฅฝ` -> `ไฝ ๅฅฝ`
* `็พโพ` -> `็ฐ่บซ`
* `Delete`: Delete all punctuation, special characters and white spaces. Based on [TEXT_DELETE](./process_map/TEXT-DELETE.txt) and `WHITE_SPACE`.
* `hello, world!` -> `helloworld`
* `ใไฝ โทๅฅฝใ` -> `ไฝ ๅฅฝ`
* `Normalize`: Normalize all English character variations and number variations to basic characters. Based on [NORM](./process_map/NORM.txt) and [NUM_NORM](./process_map/NUM-NORM.txt).
* `โะโใ ร` -> `he11o`
* `โฦงใ` -> `123`
* `PinYin`: Convert all unicode Chinese characters to pinyin with boundaries. Based on [PINYIN](./process_map/PINYIN.txt).
* `ไฝ ๅฅฝ` -> ` ni hao `
* `่ฅฟๅฎ` -> ` xi an `
* `PinYinChar`: Convert all unicode Chinese characters to pinyin without boundaries. Based on [PINYIN](./process_map/PINYIN.txt).
* `ไฝ ๅฅฝ` -> `nihao`
* `่ฅฟๅฎ` -> `xian`
You can combine these transformations as needed. Pre-defined combinations like `DeleteNormalize` and `FanjianDeleteNormalize` are provided for convenience.
Avoid combining `PinYin` and `PinYinChar` due to that `PinYin` is a more limited version of `PinYinChar`, in some cases like `xian`, can be treat as two words `xi` and `an`, or only one word `xian`.
### Basic Example
Hereโs a basic example of how to use the `Matcher` struct for text matching:
```rust
use matcher_rs::{text_process, reduce_text_process, ProcessType};
let result = text_process(ProcessType::Delete, "ไฝ ๅฅฝ๏ผไธ็๏ผ");
let result = reduce_text_process(ProcessType::FanjianDeleteNormalize, "ไฝ ๅฅฝ๏ผไธ็๏ผ");
```
```rust
use std::collections::HashMap;
use matcher_rs::{Matcher, MatchTableMap, MatchTable, MatchTableType, ProcessType};
let match_table_map: MatchTableMap = HashMap::from_iter(vec![
(1, vec![MatchTable {
table_id: 1,
match_table_type: MatchTableType::Simple { process_type: ProcessType::FanjianDeleteNormalize},
word_list: vec!["example", "test"],
exemption_process_type: ProcessType::None,
exemption_word_list: vec![],
}]),
]);
let matcher = Matcher::new(&match_table_map);
let text = "This is an example text.";
let results = matcher.word_match(text);
```
```rust
use std::collections::HashMap;
use matcher_rs::{ProcessType, SimpleMatcher};
let mut simple_table = HashMap::new();
let mut simple_word_map = HashMap::new();
simple_word_map.insert(1, "ไฝ ๅฅฝ");
simple_word_map.insert(2, "ไธ็");
simple_table.insert(ProcessType::Fanjian, simple_word_map);
let matcher = SimpleMatcher::new(&simple_table);
let text = "ไฝ ๅฅฝ๏ผไธ็๏ผ";
let results = matcher.process(text);
```
For more detailed usage examples, please refer to the [test.rs](./tests/test.rs) file.
## Feature Flags
* `runtime_build`: By enable runtime_build feature, we could build process matcher at runtime, but with build time increasing.
* `serde`: By enable serde feature, we could serialize and deserialize matcher and simple_matcher. With serde feature, AhoCorasick's prefilter is disabled, because I don't know how to serialize it correctly, which will lead to performance regression when the patterns size is small (say, less than 100).
* `dfa`: By enable dfa feature, we could use dfa to perform simple matching, but with significantly increasing memory consumption.
Default feature is `dfa`. If you want to make `Matcher` and `SimpleMatcher` serializable, you should enable `serde` feature.
## Benchmarks
Bench against pairs ([CN_WORD_LIST_100000](../data/word_list/cn/cn_words_100000.txt), [CN_HAYSTACK](../data/text/cn/่ฅฟๆธธ่ฎฐ.txt)) and ([EN_WORD_LIST_100000](../data/word_list/en/en_words_100000.txt), [EN_HAYSTACK](../data/text/en/sherlock.txt)). Word selection is totally random.
The `matcher_rs` library includes benchmarks to measure the performance of the matcher. You can find the benchmarks in the [bench.rs](./benches/bench.rs) file. To run the benchmarks, use the following command:
```shell
cargo bench
```
```
Current default simple match type: ProcessType(None)
Current default simple word map size: 10000
Current default combined times: 3
bench fastest โ slowest โ median โ mean โ samples โ iters
โโ build โ โ โ โ โ
โ โโ cn_by_combinations โ โ โ โ โ
โ โ โโ 1 15.56 ms โ 32.94 ms โ 15.93 ms โ 16.25 ms โ 100 โ 100
โ โ โโ 3 52.38 ms โ 79.45 ms โ 53.66 ms โ 54.41 ms โ 92 โ 92
โ โ โฐโ 5 91.64 ms โ 123.8 ms โ 95.42 ms โ 97.54 ms โ 52 โ 52
โ โโ cn_by_process_type โ โ โ โ โ
โ โ โโ "delete" 52.58 ms โ 57.62 ms โ 53.25 ms โ 53.59 ms โ 94 โ 94
โ โ โโ "fanjian" 52.66 ms โ 59.06 ms โ 53.55 ms โ 53.92 ms โ 93 โ 93
โ โ โโ "fanjian_delete_normalize" 52.7 ms โ 68.99 ms โ 53.63 ms โ 54.21 ms โ 93 โ 93
โ โ โฐโ "none" 53.08 ms โ 75.71 ms โ 54.14 ms โ 54.81 ms โ 92 โ 92
โ โโ cn_by_size โ โ โ โ โ
โ โ โโ 1000 5.003 ms โ 7.175 ms โ 5.135 ms โ 5.173 ms โ 100 โ 100
โ โ โโ 10000 52.79 ms โ 60.83 ms โ 53.37 ms โ 53.67 ms โ 94 โ 94
โ โ โฐโ 50000 219 ms โ 245.5 ms โ 221.3 ms โ 223.8 ms โ 23 โ 23
โ โโ en_by_combinations โ โ โ โ โ
โ โ โโ 1 59.81 ms โ 87.29 ms โ 60.75 ms โ 61.56 ms โ 82 โ 82
โ โ โโ 3 124.3 ms โ 143.9 ms โ 125.7 ms โ 127.5 ms โ 40 โ 40
โ โ โฐโ 5 213.1 ms โ 230.8 ms โ 215.2 ms โ 217 ms โ 24 โ 24
โ โโ en_by_process_type โ โ โ โ โ
โ โ โโ "delete" 124.1 ms โ 137.1 ms โ 125.3 ms โ 125.8 ms โ 40 โ 40
โ โ โโ "delete_normalize" 116.6 ms โ 128.9 ms โ 117.8 ms โ 118.2 ms โ 43 โ 43
โ โ โฐโ "none" 124.6 ms โ 134.8 ms โ 125.8 ms โ 126.6 ms โ 40 โ 40
โ โฐโ en_by_size โ โ โ โ โ
โ โโ 1000 12.46 ms โ 13.42 ms โ 12.64 ms โ 12.67 ms โ 100 โ 100
โ โโ 10000 125.1 ms โ 145.8 ms โ 127 ms โ 128.9 ms โ 39 โ 39
โ โฐโ 50000 605.6 ms โ 630 ms โ 608.4 ms โ 613 ms โ 9 โ 9
โโ search_match โ โ โ โ โ
โ โโ cn_by_process_type โ โ โ โ โ
โ โ โโ "delete" 33.46 ms โ 57.76 ms โ 34.41 ms โ 35.58 ms โ 100 โ 100
โ โ โโ "fanjian" 23.5 ms โ 33.59 ms โ 24.62 ms โ 24.88 ms โ 100 โ 100
โ โ โโ "fanjian_delete_normalize" 51.02 ms โ 63.77 ms โ 52.45 ms โ 52.86 ms โ 95 โ 95
โ โ โฐโ "none" 21.45 ms โ 30.05 ms โ 22.52 ms โ 22.72 ms โ 100 โ 100
โ โโ cn_by_size โ โ โ โ โ
โ โ โโ 1000 4.596 ms โ 5.097 ms โ 4.692 ms โ 4.691 ms โ 100 โ 100
โ โ โโ 10000 21.38 ms โ 25.9 ms โ 22.01 ms โ 22.15 ms โ 100 โ 100
โ โ โฐโ 50000 93.97 ms โ 120.3 ms โ 98.59 ms โ 101.1 ms โ 50 โ 50
โ โโ en_by_combinations โ โ โ โ โ
โ โ โโ 1 1.333 ms โ 1.487 ms โ 1.356 ms โ 1.362 ms โ 100 โ 100
โ โ โโ 3 2.155 ms โ 2.879 ms โ 2.186 ms โ 2.203 ms โ 100 โ 100
โ โ โฐโ 5 2.951 ms โ 4.338 ms โ 3.072 ms โ 3.103 ms โ 100 โ 100
โ โโ en_by_process_type โ โ โ โ โ
โ โ โโ "delete" 7.028 ms โ 10.57 ms โ 7.308 ms โ 7.42 ms โ 100 โ 100
โ โ โโ "delete_normalize" 9.231 ms โ 10.99 ms โ 9.406 ms โ 9.445 ms โ 100 โ 100
โ โ โฐโ "none" 2.152 ms โ 2.877 ms โ 2.191 ms โ 2.212 ms โ 100 โ 100
โ โฐโ en_by_size โ โ โ โ โ
โ โโ 1000 1.102 ms โ 1.306 ms โ 1.119 ms โ 1.124 ms โ 100 โ 100
โ โโ 10000 2.162 ms โ 3.023 ms โ 2.252 ms โ 2.254 ms โ 100 โ 100
โ โฐโ 50000 6.184 ms โ 8.324 ms โ 6.301 ms โ 6.42 ms โ 100 โ 100
โฐโ search_no_match โ โ โ โ โ
โโ cn_by_process_type โ โ โ โ โ
โ โโ "delete" 6.159 ms โ 6.512 ms โ 6.208 ms โ 6.228 ms โ 100 โ 100
โ โโ "fanjian" 1.319 ms โ 1.558 ms โ 1.342 ms โ 1.354 ms โ 100 โ 100
โ โโ "fanjian_delete_normalize" 10.21 ms โ 13.12 ms โ 10.26 ms โ 10.3 ms โ 100 โ 100
โ โฐโ "none" 372.9 ยตs โ 484.4 ยตs โ 380 ยตs โ 383.5 ยตs โ 100 โ 100
โโ cn_by_size โ โ โ โ โ
โ โโ 1000 370 ยตs โ 398.9 ยตs โ 377.8 ยตs โ 379 ยตs โ 100 โ 100
โ โโ 10000 375.1 ยตs โ 418.6 ยตs โ 388.3 ยตs โ 389.2 ยตs โ 100 โ 100
โ โฐโ 50000 369.4 ยตs โ 432.8 ยตs โ 378.2 ยตs โ 380.8 ยตs โ 100 โ 100
โโ en_by_combinations โ โ โ โ โ
โ โโ 1 929.7 ยตs โ 1.029 ms โ 942.4 ยตs โ 946.5 ยตs โ 100 โ 100
โ โโ 3 914.6 ยตs โ 991.2 ยตs โ 934.2 ยตs โ 938.2 ยตs โ 100 โ 100
โ โฐโ 5 938.6 ยตs โ 988.8 ยตs โ 947.4 ยตs โ 950.9 ยตs โ 100 โ 100
โโ en_by_process_type โ โ โ โ โ
โ โโ "delete" 4.603 ms โ 5.581 ms โ 4.674 ms โ 4.703 ms โ 100 โ 100
โ โโ "delete_normalize" 5.816 ms โ 6.26 ms โ 5.881 ms โ 5.894 ms โ 100 โ 100
โ โฐโ "none" 908.1 ยตs โ 961.4 ยตs โ 920.8 ยตs โ 926.2 ยตs โ 100 โ 100
โฐโ en_by_size โ โ โ โ โ
โโ 1000 910.4 ยตs โ 1.584 ms โ 940.5 ยตs โ 961.4 ยตs โ 100 โ 100
โโ 10000 920.8 ยตs โ 1.013 ms โ 934 ยตs โ 938.4 ยตs โ 100 โ 100
โฐโ 50000 912.4 ยตs โ 1.009 ms โ 926.3 ยตs โ 932.2 ยตs โ 100 โ 100
```
## Contributing
Contributions to `matcher_rs` are welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository. If you would like to contribute code, please fork the repository and submit a pull request.
## License
`matcher_rs` is licensed under the MIT OR Apache-2.0 license.
## More Information
For more details, visit the [GitHub repository](https://github.com/Lips7/Matcher).