matcher_rs 0.8.0

A high-performance matcher designed to solve LOGICAL and TEXT VARIATIONS problems in word matching, implemented in Rust.
Documentation
# 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
```



### Explanation of the configuration

* `Matcher`'s configuration is built using `MatcherBuilder` and `MatchTableBuilder`.
* `SimpleMatcher`'s configuration is built using `SimpleMatcherBuilder`. For each `SimpleMatcher`, the added `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 matcher_rs::{MatcherBuilder, MatchTableBuilder, MatchTableType, ProcessType};

let table = MatchTableBuilder::new(1, MatchTableType::Simple { process_type: ProcessType::FanjianDeleteNormalize })
    .add_words(["example", "test"])
    .build();

let matcher = MatcherBuilder::new()
    .add_table(1, table)
    .build();

let text = "This is an example text.";
let results = matcher.word_match(text);
```

```rust
use matcher_rs::{ProcessType, SimpleMatcherBuilder};

let matcher = SimpleMatcherBuilder::new()
    .add_word(ProcessType::Fanjian, 1, "ไฝ ๅฅฝ")
    .add_word(ProcessType::Fanjian, 2, "ไธ–็•Œ")
    .build();

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.
* `dfa`: By enable dfa feature, we could use dfa to perform simple matching, but with significantly increasing memory consumption.

Default feature is `dfa`.

## 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).