Fast multi-keyword search engine for text strings
Project description
What is Acora?
Acora is ‘fgrep’ for Python, a fast multi-keyword text search engine.
Based on a set of keywords and the Aho-Corasick algorithm, it generates a search automaton and runs it over string input, either unicode or bytes.
Acora comes with both a pure Python implementation and a fast binary module written in Cython. However, note that the current construction algorithm is not suitable for really large sets of keywords (i.e. more than a couple of thousand).
You can find the latest source code on github.
To report a bug or request new features, use the github bug tracker. Please try to provide a short test case that reproduces the problem without requiring too much experimentation or large amounts of data. The easier it is to reproduce the problem, the easier it is to solve it.
Features
works with unicode strings and byte strings
about 2-3x as fast as Python’s regular expression engine for most input
finds overlapping matches, i.e. all matches of all keywords
support for case insensitive search (~10x as fast as ‘re’)
frees the GIL while searching
additional (slow but short) pure Python implementation
support for Python 2.5+ and 3.x
support for searching in files
permissive BSD license
How do I use it?
Import the package:
>>> from acora import AcoraBuilder
Collect some keywords:
>>> builder = AcoraBuilder('ab', 'bc', 'de') >>> builder.add('a', 'b')
Or:
>>> builder.update(['a', 'b']) # new in version 2.0
Generate the Acora search engine for the current keyword set:
>>> ac = builder.build()
Search a string for all occurrences:
>>> ac.findall('abc') [('a', 0), ('ab', 0), ('b', 1), ('bc', 1)] >>> ac.findall('abde') [('a', 0), ('ab', 0), ('b', 1), ('de', 2)]
Iterate over the search results as they come in:
>>> for kw, pos in ac.finditer('abde'): ... print("%2s[%d]" % (kw, pos)) a[0] ab[0] b[1] de[2]
Acora also has direct support for parsing files (in binary mode):
>>> keywords = ['Import', 'FAQ', 'Acora', 'NotHere'.upper()] >>> builder = AcoraBuilder([s.encode('ascii') for s in keywords]) >>> ac = builder.build() >>> found = set(kw for kw, pos in ac.filefind('README.rst')) >>> len(found) 3 >>> sorted(str(s.decode('ascii')) for s in found) ['Acora', 'FAQ', 'Import']
FAQs and recipes
How do I run a greedy search for the longest matching keywords?
>>> builder = AcoraBuilder('a', 'ab', 'abc') >>> ac = builder.build() >>> for kw, pos in ac.finditer('abbabc'): ... print(kw) a ab a ab abc >>> from itertools import groupby >>> from operator import itemgetter >>> def longest_match(matches): ... for pos, match_set in groupby(matches, itemgetter(1)): ... yield max(match_set) >>> for kw, pos in longest_match(ac.finditer('abbabc')): ... print(kw) ab abc
Note that this recipe assumes search terms that do not have inner overlaps apart from their prefix.
How do I parse line-by-line with arbitrary line endings?
>>> def group_by_lines(s, *keywords): ... builder = AcoraBuilder('\r', '\n', *keywords) ... ac = builder.build() ... ... current_line_matches = [] ... last_ending = None ... ... for kw, pos in ac.finditer(s): ... if kw in '\r\n': ... if last_ending == '\r' and kw == '\n': ... continue # combined CRLF ... yield tuple(current_line_matches) ... del current_line_matches[:] ... last_ending = kw ... else: ... last_ending = None ... current_line_matches.append(kw) ... yield tuple(current_line_matches)
>>> kwds = ['ab', 'bc', 'de'] >>> for matches in group_by_lines('a\r\r\nbc\r\ndede\n\nab', *kwds): ... print(matches) () () ('bc',) ('de', 'de') () ('ab',)
How do I find whole lines that contain keywords, as fgrep does?
>>> def match_lines(s, *keywords): ... builder = AcoraBuilder('\r', '\n', *keywords) ... ac = builder.build() ... ... line_start = 0 ... matches = False ... for kw, pos in ac.finditer(s): ... if kw in '\r\n': ... if matches: ... yield s[line_start:pos] ... matches = False ... line_start = pos + 1 ... else: ... matches = True ... if matches: ... yield s[line_start:]
>>> kwds = ['x', 'de', '\nstart'] >>> text = 'a line with\r\r\nsome text\r\ndede\n\nab\n start 1\nstart\n' >>> for line in match_lines(text, *kwds): ... print(line) some text dede start
Changelog
2.2 [2018-08-16]
Update to work with CPython 3.7 by building with Cython 0.29.
2.1 [2017-12-15]
fix handling of empty engines (Github issue #18)
2.0 [2016-03-17]
rewrite of the construction algorithm to speed it up and save memory
1.9 [2015-10-10]
recompiled with Cython 0.23.4 for better compatibility with recent Python versions.
1.8 [2014-02-12]
pickle support for the pre-built search engines
performance optimisations in builder
Unicode parsing is optimised for Python 3.3 and later
no longer recompiles sources when Cython is installed, unless --with-cython option is passed to setup.py (requires Cython 0.20+)
build failed with recent Cython versions
built using Cython 0.20.1
1.7 [2011-08-24]
searching binary strings for byte values > 127 was broken
built using Cython 0.15+
1.6 [2011-07-24]
substantially faster automaton building
no longer includes .hg repo in source distribution
built using Cython 0.15 (rc0)
1.5 [2011-01-24]
Cython compiled NFA-2-DFA construction runs substantially faster
always build extension modules even if Cython is not installed
--no-compile switch in setup.py to prevent extension module building
built using Cython 0.14.1 (rc2)
1.4 [2009-02-10]
minor speed-up in inner search engine loop
some code cleanup
built using Cython 0.12.1 (final)
1.3 [2009-01-30]
major fix for file search
built using Cython 0.12.1 (beta0)
1.2 [2009-01-30]
deep-copy support for AcoraBuilder class
doc/test fixes
include .hg repo in source distribution
built using Cython 0.12.1 (beta0)
1.1 [2009-01-29]
doc updates
some cleanup
built using Cython 0.12.1 (beta0)
1.0 [2009-01-29]
initial release
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
Built Distributions
Hashes for acora-2.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c50161d8711789306e304d92f95e912224e2f50e41d9c7be53565ddde6af7542 |
|
MD5 | a5f79ecb15f32b4d0356393580d4fd6d |
|
BLAKE2b-256 | 7b254d77e1d5561c9365bbbc3ccbf804e074ae36598eda57b8ad40e765e36086 |
Hashes for acora-2.2-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1da09be7db8fdfe6a6c11c82d9aa9a9ef55ff14c490e4cc02ff3a696675db2c4 |
|
MD5 | 41459371bc445edc5ffba3f6580a1d50 |
|
BLAKE2b-256 | f608d7022e22ee5bbde9a39015347dc36d0e491d9824025ec573c57b2180dfc5 |
Hashes for acora-2.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc59e846d109f43c3ae340c5dada9bbca33df65beffb6b42e93a504b342c789d |
|
MD5 | b5bb7e9378cc25fdbc89e1708e1809e6 |
|
BLAKE2b-256 | f697de75ac2ff9cb984eb3b041762dd5e72ef008143a904050c1bc870ee7556b |
Hashes for acora-2.2-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 886bb0c37496572233fbaf723f13a9fec64372f93b1cfee6fdfd62b89a8bb893 |
|
MD5 | 366b62f886062f89c9ab37f33ccf7182 |
|
BLAKE2b-256 | 77c17ff4c263d9c04293004c9052a05d69fa05a075877922434216f521d7a1e0 |
Hashes for acora-2.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f90ea6a36156aca12c330c40178662e9f2def23343b393109bd0be98ef944f02 |
|
MD5 | c2547e47bebfee7c0b7a7d4f09c9a197 |
|
BLAKE2b-256 | bebdbbc0d46627c4eaec2230a29384ed5874957f2b4531489b99f6d7b227f1bc |
Hashes for acora-2.2-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e76a1c353be19fae8724d3921e734ab7ca55ffb2dcffc4349275dccf5dc42319 |
|
MD5 | 44fd2a503992fc94de688ae3c045f591 |
|
BLAKE2b-256 | 74acb91ac1a9b31b1dab23a39f4f64604c2aefb9658bb2463f336ea369751b7e |
Hashes for acora-2.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5d671946eb3fa7b882a97eefeb4047c6d47ebacbd438fb779d69d276b9a56fb |
|
MD5 | ef335f83dd117265c86f42cc5655feaa |
|
BLAKE2b-256 | 9003b132828c4db0583675ba0cd9f6f872c94b21c2d30886aeaed78d483c90cb |
Hashes for acora-2.2-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3e39ba95e9378025ce0405e332cd8cdb7e0036011e9e524a2b3f93493feab1e |
|
MD5 | 8e3b10bc664783d529e9a7cbe7c04a36 |
|
BLAKE2b-256 | 006eeeeb2e1bb26e871571ff008173c2091f7352dc351fb1a3a547716d43c6fc |
Hashes for acora-2.2-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b564fdab642ce6d1d263dd31ae94e832d420d4b099f01dc432df1aa451e9f41 |
|
MD5 | e3894e43c87d56953583b09291a2eaa3 |
|
BLAKE2b-256 | b05e091e93dfcbd0c551681db595d6afb7394814e09f07ecdb751b26de3f800c |
Hashes for acora-2.2-cp27-cp27mu-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3ed53992b3a43b182eb367a730e7d2b3d13fb01986084dc1c0a26ba21a14ead |
|
MD5 | 6f4570e955ad4d68eab3cace4dd9029f |
|
BLAKE2b-256 | 953e99794e7ea8c17de225e1c209619c6e9448b754b83883f2d5269273ddb08f |
Hashes for acora-2.2-cp27-cp27m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7942b94a22681f055e7700d14d5c7d0af28c914319dc84a26ee9f635ba9266f |
|
MD5 | 5e5ec14dbab6236ccaef502286f45f64 |
|
BLAKE2b-256 | 293d5b9c1bb82ce970db9b87bfb1bfa3f7b23ba1f38b047e8bfbc424205501e3 |
Hashes for acora-2.2-cp27-cp27m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b982ee509ca8b76bc22519f12c359622644b4ee9acc58568fa0650ae50f5f0a5 |
|
MD5 | 26c695c64af84ed9c5b254a04aa506f5 |
|
BLAKE2b-256 | a8b6d240b777ecda1cf66ccdb23951a37ee099197cb335330bfdd855322f27c2 |