pyrefo: a fast regex for object
This project is based on refo and the paper Regular Expression Matching: the Virtual Machine Approach, it use cffi to extend python with c to speed accelerate processing performance.
This project has done the following work:
- full compatiable with refo api, support all patterns and match, search, finditer methods;
- fix c source bug included in the paper;
- use cffi to extend python with c;
- add new feature which supports partial match;
- add new
Phrasepattern which can realize'ab'match['a', 'b', 'c']list;
How to use it
"ab" is Literal("a")+Literal("b")
"a*" is Star(Literal("a"))
"aab?" is Literal("a")+Literal("a")+Question(Literal("b"), greedy=False)
a{3,4} is Repetition(Literal("a"), 3, 4, greedy=False)
"(ab)+|(bb*)?" is
a = Literal("a")
b = Literal("b")
regex = Plus(a + b) | Star(b + b, greedy=False)
You can also assign a group to any sub-match and later on retrieve the matched content, for instance:
regex = Group(Plus(a + b), "foobar") | Star(b + b, greedy=False)
m = match(regex, "abab")
print(m.span("foobar"))
pyrefo offers match, search, findall, finditer search functions:
- match: match pattern from first position
- search: search pattern from first position till find one
- findall: find all matched result
- finditer: return an iterator for all matched result
pyrefo offers the following predicates:
- Any
- Literal
- Star
- Plus
- Question
- Group
- Repetition
- Phrase
Performance test
prerequisites
import jieba
text = '为什么在本店买东西?因为物流迅速+品质保证。为什么我购买的每件商品评价都一样呢?因为我买的东西太多了,积累了很多未评价的订单,所以我统一用这段话作为评价内容。如果我用了这段话作为评价,那就说明这款产品非常赞,非常好!'
tokens = list(jieba.cut(text))
CPython
- pyrefo
from pyrefo import search, Group, Star, Any, Literal
%timeit search(Group(Literal('物流') + Star(Any()) + Literal('迅速'), 'a'), tokens)
95.9 µs ± 472 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
- refo
import refo
%timeit refo.search(refo.Group(refo.Literal('物流') + refo.Star(refo.Any()) + refo.Literal('迅速'), 'a'), tokens)
1.03 ms ± 7.27 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
- re
import re
%timeit re.search('(物流.*速度)', text)
989 ns ± 4.69 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
PyPy
- pyrefo
from pyrefo import search, Group, Star, Any, Literal
%timeit search(Group(Literal('物流') + Star(Any()) + Literal('迅速'), 'a'), tokens)
53.4 µs ± 28 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
- refo
import refo
%timeit refo.search(refo.Group(refo.Literal('物流') + refo.Star(refo.Any()) + refo.Literal('迅速'), 'a'), tokens)
78 µs ± 35.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
- re
import re
%timeit re.search('(物流.*速度)', text)
347 ns ± 3.26 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Metadata
Release files for pyrefo 0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyrefo-0.4.tar.gz | 24.7 kB | Without provenance | Details for pyrefo-0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyrefo-0.4-cp37-cp37m-macosx_10_13_x86_64.whl | CPython 3.7 | CPython 3.7 pymalloc | macOS 10.13+ x86-64 | Details for pyrefo-0.4-cp37-cp37m-macosx_10_13_x86_64.whl |
Total release size: 55.1 kB
Release files / pyrefo-0.4.tar.gz
| Download URL | pyrefo-0.4.tar.gz |
|---|---|
| Size | 24.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
baa99189e1aec8c392b863b3b2eacd34ca2225b44ddbf0c62136051bae689fba
|
|
BLAKE2b-256 checksum How to use checksums |
a3e6d854c13ba3836cbe17aa989626bddd78c9a3f9065b086ab7e84407088441
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1
|
Release files / pyrefo-0.4-cp37-cp37m-macosx_10_13_x86_64.whl
| Download URL | pyrefo-0.4-cp37-cp37m-macosx_10_13_x86_64.whl |
|---|---|
| Size | 30.3 kB |
| Tags | CPython 3.7 CPython 3.7 pymalloc macOS 10.13+ x86-64 |
|
SHA-256 checksum How to use checksums |
75a3ed38ab5a3435b1e3f72fedc662cbfdc8b5b97c62a6c666e8af288831ae91
|
|
BLAKE2b-256 checksum How to use checksums |
a3e6dae36bc33491a4837aea44e7b84861cf9d6faeeea6b04eacb5f01b1d9d7b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1
|
Security for pyrefo 0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.