|
1 | | -# CThaiNLP |
| 1 | +# CThaiNLP |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +C implementation of Thai Natural Language Processing tools, ported from [PyThaiNLP](https://github.com/PyThaiNLP/pythainlp). |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **newmm**: Dictionary-based maximal matching word segmentation constrained by Thai Character Cluster (TCC) boundaries |
| 10 | +- Similar API to PyThaiNLP for easy migration from Python to C |
| 11 | +- UTF-8 support |
| 12 | +- Efficient Trie data structure for dictionary lookup |
| 13 | +- Handles mixed Thai/English/numeric content |
| 14 | + |
| 15 | +## Building |
| 16 | + |
| 17 | +### Prerequisites |
| 18 | + |
| 19 | +- GCC or compatible C compiler |
| 20 | +- Make |
| 21 | + |
| 22 | +### Compilation |
| 23 | + |
| 24 | +```bash |
| 25 | +make |
| 26 | +``` |
| 27 | + |
| 28 | +This will create: |
| 29 | +- Static library: `lib/libcthainlp.a` |
| 30 | +- Example program: `build/example_basic` |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +### Basic Example |
| 35 | + |
| 36 | +```c |
| 37 | +#include "newmm.h" |
| 38 | + |
| 39 | +int main() { |
| 40 | + const char* text = "ฉันไปโรงเรียน"; |
| 41 | + int token_count; |
| 42 | + |
| 43 | + // Segment text (with NULL for dict_path to use default dictionary) |
| 44 | + char** tokens = newmm_segment(text, NULL, &token_count); |
| 45 | + |
| 46 | + // Print tokens |
| 47 | + for (int i = 0; i < token_count; i++) { |
| 48 | + printf("%s\n", tokens[i]); |
| 49 | + } |
| 50 | + |
| 51 | + // Free memory |
| 52 | + newmm_free_result(tokens, token_count); |
| 53 | + |
| 54 | + return 0; |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Compile Your Program |
| 59 | + |
| 60 | +```bash |
| 61 | +gcc your_program.c -I./include -L./lib -lcthainlp -o your_program |
| 62 | +``` |
| 63 | + |
| 64 | +### Running Examples |
| 65 | + |
| 66 | +Basic example with default dictionary: |
| 67 | +```bash |
| 68 | +./build/example_basic "ฉันไปโรงเรียน" |
| 69 | +``` |
| 70 | + |
| 71 | +With custom dictionary: |
| 72 | +```bash |
| 73 | +./build/example_basic "ฉันไปโรงเรียน" data/thai_words.txt |
| 74 | +``` |
| 75 | + |
| 76 | +### Running Tests |
| 77 | + |
| 78 | +Run the test suite: |
| 79 | +```bash |
| 80 | +make test |
| 81 | +``` |
| 82 | + |
| 83 | +This will compile and run all unit tests to verify the tokenizer is working correctly. |
| 84 | + |
| 85 | +## API Reference |
| 86 | + |
| 87 | +### Functions |
| 88 | + |
| 89 | +#### `char** newmm_segment(const char* text, const char* dict_path, int* token_count)` |
| 90 | + |
| 91 | +Segment Thai text into words using the newmm algorithm. |
| 92 | + |
| 93 | +**Parameters:** |
| 94 | +- `text`: Input text to segment (UTF-8 encoded) |
| 95 | +- `dict_path`: Path to dictionary file (one word per line, UTF-8). Use `NULL` for default dictionary |
| 96 | +- `token_count`: Output parameter - receives the number of tokens found |
| 97 | + |
| 98 | +**Returns:** |
| 99 | +- Array of strings (tokens), or `NULL` on error |
| 100 | +- Caller must free the result using `newmm_free_result()` |
| 101 | + |
| 102 | +**Example:** |
| 103 | +```c |
| 104 | +int count; |
| 105 | +char** tokens = newmm_segment("ฉันไปโรงเรียน", "dict.txt", &count); |
| 106 | +``` |
| 107 | + |
| 108 | +#### `void newmm_free_result(char** tokens, int token_count)` |
| 109 | + |
| 110 | +Free memory allocated by `newmm_segment()`. |
| 111 | + |
| 112 | +**Parameters:** |
| 113 | +- `tokens`: Array of tokens returned by `newmm_segment()` |
| 114 | +- `token_count`: Number of tokens in the array |
| 115 | + |
| 116 | +**Example:** |
| 117 | +```c |
| 118 | +newmm_free_result(tokens, count); |
| 119 | +``` |
| 120 | +
|
| 121 | +## Dictionary Format |
| 122 | +
|
| 123 | +Dictionary files should contain one word per line in UTF-8 encoding: |
| 124 | +
|
| 125 | +``` |
| 126 | +ฉัน |
| 127 | +ไป |
| 128 | +โรงเรียน |
| 129 | +วันนี้ |
| 130 | +อากาศ |
| 131 | +ดี |
| 132 | +มาก |
| 133 | +``` |
| 134 | +
|
| 135 | +A sample dictionary is provided in `data/thai_words.txt`. |
| 136 | +
|
| 137 | +## Comparison with PyThaiNLP |
| 138 | +
|
| 139 | +The API is designed to be similar to PyThaiNLP's `segment()` function: |
| 140 | +
|
| 141 | +**PyThaiNLP (Python):** |
| 142 | +```python |
| 143 | +from pythainlp.tokenize import word_tokenize |
| 144 | +
|
| 145 | +text = "ฉันไปโรงเรียน" |
| 146 | +tokens = word_tokenize(text, engine="newmm") |
| 147 | +print(tokens) # ['ฉัน', 'ไป', 'โรงเรียน'] |
| 148 | +``` |
| 149 | + |
| 150 | +**CThaiNLP (C):** |
| 151 | +```c |
| 152 | +const char* text = "ฉันไปโรงเรียน"; |
| 153 | +int token_count; |
| 154 | +char** tokens = newmm_segment(text, NULL, &token_count); |
| 155 | +// tokens = ['ฉัน', 'ไป', 'โรงเรียน'] |
| 156 | +newmm_free_result(tokens, token_count); |
| 157 | +``` |
| 158 | +
|
| 159 | +## Algorithm |
| 160 | +
|
| 161 | +The newmm (New Maximum Matching) algorithm: |
| 162 | +
|
| 163 | +1. **Trie-based Dictionary Lookup**: Uses a trie data structure for efficient prefix matching |
| 164 | +2. **Thai Character Cluster (TCC) Boundaries**: Respects Thai character cluster rules for valid word boundaries |
| 165 | +3. **Maximal Matching**: Finds the longest dictionary word that matches at each position |
| 166 | +4. **Fallback Handling**: Handles non-dictionary words and non-Thai characters (Latin, digits, etc.) |
| 167 | +
|
| 168 | +## Project Structure |
| 169 | +
|
| 170 | +``` |
| 171 | +CThaiNLP/ |
| 172 | +├── include/ |
| 173 | +│ └── newmm.h # Public API header |
| 174 | +├── src/ |
| 175 | +│ ├── newmm.c # Main newmm implementation |
| 176 | +│ ├── trie.c # Trie data structure |
| 177 | +│ ├── trie.h # Trie header |
| 178 | +│ ├── tcc.c # Thai Character Cluster |
| 179 | +│ └── tcc.h # TCC header |
| 180 | +├── examples/ |
| 181 | +│ └── example_basic.c # Basic usage example |
| 182 | +├── tests/ |
| 183 | +│ └── test_newmm.c # Test suite |
| 184 | +├── data/ |
| 185 | +│ └── thai_words.txt # Sample dictionary |
| 186 | +├── Makefile # Build configuration |
| 187 | +└── README.md # This file |
| 188 | +``` |
| 189 | +
|
| 190 | +## Credits |
| 191 | +
|
| 192 | +- Original PyThaiNLP implementation: [PyThaiNLP Project](https://github.com/PyThaiNLP/pythainlp) |
| 193 | +- newmm algorithm: Based on work by Korakot Chaovavanich |
| 194 | +- TCC rules: Theeramunkong et al. 2000 |
| 195 | +
|
| 196 | +## License |
| 197 | +
|
| 198 | +Apache License 2.0 (following PyThaiNLP's license) |
| 199 | +
|
| 200 | +## Contributing |
| 201 | +
|
| 202 | +Contributions are welcome! Please feel free to submit issues or pull requests. |
| 203 | +
|
| 204 | +## Future Enhancements |
| 205 | +
|
| 206 | +- [ ] Add more tokenization engines (attacut, deepcut, etc.) |
| 207 | +- [ ] Improve performance with optimized data structures |
| 208 | +- [ ] Add part-of-speech tagging |
| 209 | +- [ ] Add named entity recognition |
| 210 | +- [ ] Provide Python bindings (PyPI package) |
0 commit comments