A simple template matcher
Project description
OpTrie
一个简单的字符串模板匹配工具
安装
pip
pip install optrie
- 二进制兼容性manylinux2.17
- windows和macos暂未测试
源码安装
git clone https://github.com/qjf42/optrie
cd optrie
python setup.py install
Usage
1. 准备词典
- 每一个[D:xxx]表示词典key
- 后续的每一行是其可以匹配的字符串
- 不同key的值集合可以有重合
[D:hi]
你好
hello
# 注释,#必须为首字符
[D:location]
上海
北京
[D:price]
房价
价格
2. 准备模板
每一行是一个模板,用tab分隔,可以有2~4项,分别为:
- 模板项
[D:xx]必须包含在词典中[W:1]用于模糊匹配,表示0-1个字符,[W:2-3]表示2-3个字符- 也可以直接用明文
- 置信分
- ps:目前匹配采用贪心策略,当匹配多个模板时,只取第一个,不保证分数最大
- 模板关联信息(可选)
- json, schema: {string => string|numeric}
- 信息抽取(可选)
- json, schema: {输出字段 => 抽取的词典项或模糊匹配项}
[D:hi][W:1] 1
[W:2-3][D:location][D:price] 0.9 {"catg": "housing"} {"loc": "[D:location]"}
[W:1-2]月新番 1 {"month":"[W:1-2]"}
- 匹配
from optrie import OpTrie
# 加载模板和词典,可以有多个
m = OpTrie().load(['sample.tpl'], ['sample.dic'])
# 打印词典树
m.show()
res = m.match('你好')
res.matched # True
res.template # [D:hi][W:1]
res.score # 1
res = m.match('查询上海房价')
res.matched # True
res.template # "[W:2-3][D:location][D:price]"
res.score # 0.9
res.extra_info # {"catg": "housing"}
res.groups # {"loc": "上海"}
res = m.match('深圳房价')
res.matched # False
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
optrie-0.1.0.tar.gz
(147.7 kB
view details)
File details
Details for the file optrie-0.1.0.tar.gz.
File metadata
- Download URL: optrie-0.1.0.tar.gz
- Upload date:
- Size: 147.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12b5ab921714bb33c43a1b163506bfda29882c011353a700131db2438c39a471
|
|
| MD5 |
d21215cb6971f676122a4e9b93d528f3
|
|
| BLAKE2b-256 |
c0f1babe4a3092575389c4cd42998207b83a91ae0beccb140c8ccf1cc328efaf
|