Skip to main content

SearchString is a custom implementation for searching strings for km24.dk.

Project description

Search String

GitHub Workflow Status (branch) PyPI - Version PyPI - Python Version

Installation

You can install search-string from PyPI:

$ pip install search-string-overvaagning

The package is supported on Python 3.8+. However, the package is only compiled using mypyc starting from Python 3.10 which makes it about twice as fast. As such, it is strongly advised to run it on Python 3.10+.

About

This package implements the search string object that is used across km24.dk for different types of surveillance.

It is used for searching a text. For something to be deemed a match, the text must match the first_str and if the second_str is not empty, the text must also match the second_str. If the not_str is not empty, the text must not match the not_str. A logical AND is used between the three conditions. The three strings can each be a collection of strings separated by semicolons wherein a match is deemed by logical OR. You can use '~' to make a word boundary. Finally, you can use !global at the end of a string to signal that that part should check globally.

Quick examples:

>>> ss = SearchString('example;hello', 'text', 'elephant', data=None)
>>> ss.match('This is an example text')
True
>>> ss.match('This text says hello')
True
>>> ss.match('This is just an example')
False

Usage

Creating Search Strings

Start by importing the SearchString class:

>>> from search_string import SearchString

Construct a new search string by supplying the first_str, second_str, not_str and any data that can be useful to refer back to later, such as an ID:

>>> ss = SearchString('first', '', '', data=2)

Optionally, you can also supply a third_str that works in the same was as first_str and not_str but has to be supplied as a keyword argument:

>>> ss = SearchString('first', '', '', data=2, third_str='third')

Matching text

If you just need to find out whether a given search string matches a text, you can use the method .match on a SearchString instance.

Often, what you want to do, is to match a collection of search strings across a list of text, e.g. sentences. You can do that the following way:

>>> from search_string import SearchString
>>> search_strings = [
...    SearchString('kan', '', 'ritzau', data=1),
...    SearchString('kan', '', 'ritzau!global', data=2)
... ]
>>> sentences = [
...    'Du kan skrive din tekst her.',
...    'Den kan bestå af flere sætninger.',
...    'Dig og Ritzau kan bestemme hvordan det skal være.',
...    'Nogle kan være lange, andre kan være korte.'
... ]
>>> res = SearchString.find_all(sentences, search_strings)
>>> res
[SearchString(kan, -, ritzau, data=1)]

For each of the matched search strings (in the above example, only one), you can extract the data and the matched text as follows:

>>> res[0].data
1
>>> res[0].matched_text
'Du kan skrive din tekst her. Den kan bestå af flere sætninger. (...) Nogle kan være lange, andre kan være korte.'
>>> res[0].matched_text_highligthed
'Du <b>kan</b> skrive din tekst her. Den <b>kan</b> bestå af flere sætninger. (...) Nogle <b>kan</b> være lange, andre <b>kan</b> være korte.'

SearchStringCollection - Matching when you have many search strings

If you have a problem where you repeatedly will be matching new texts against the same collection of search strings, it is highly advised to use the SearchStringCollection which behind the scenes uses a trie for efficient search when many search strings are present. There is some initial cost in building the trie. Thus, it is recommended that you initialize the collection once and then continue to use it.

The most important method on SearchStringCollection is find_all, which takes a sentence (str) or list of sentences (list[str]) and returns the matched search strings, very similar to the familiar SearchString.find_all.

>>> from search_string import SearchString, SearchStringCollection
>>> search_strings = [
...    SearchString('kan', '', 'ritzau', data=1),
...    SearchString('kan', '', 'ritzau!global', data=2)
... ]
>>> sentences = ...  # Same as before
>>> ss_collection = SearchStringCollection(search_strings)
>>> res = ss_collection.find_all()
>>> res
[SearchString(kan, -, ritzau, data=1)]

Importantly, SearchStringCollection relies on the data variable being set on the collection of search strings. If it is set to None or multiple search strings have the same value, the behavior is undefined.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

search_string_overvaagning-0.4.0-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

search_string_overvaagning-0.4.0-cp312-cp312-musllinux_1_1_x86_64.whl (223.0 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (229.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.4.0-cp312-cp312-macosx_11_0_arm64.whl (127.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

search_string_overvaagning-0.4.0-cp312-cp312-macosx_10_9_x86_64.whl (133.0 kB view details)

Uploaded CPython 3.12macOS 10.9+ x86-64

search_string_overvaagning-0.4.0-cp312-cp312-macosx_10_9_universal2.whl (243.8 kB view details)

Uploaded CPython 3.12macOS 10.9+ universal2 (ARM64, x86-64)

search_string_overvaagning-0.4.0-cp311-cp311-musllinux_1_1_x86_64.whl (215.2 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (222.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.4.0-cp311-cp311-macosx_11_0_arm64.whl (127.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

search_string_overvaagning-0.4.0-cp311-cp311-macosx_10_9_x86_64.whl (131.5 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

search_string_overvaagning-0.4.0-cp311-cp311-macosx_10_9_universal2.whl (242.0 kB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)

search_string_overvaagning-0.4.0-cp310-cp310-musllinux_1_1_x86_64.whl (216.1 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (223.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.4.0-cp310-cp310-macosx_11_0_arm64.whl (128.3 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

search_string_overvaagning-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl (132.7 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

search_string_overvaagning-0.4.0-cp310-cp310-macosx_10_9_universal2.whl (244.2 kB view details)

Uploaded CPython 3.10macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file search_string_overvaagning-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58db8d24098cc5336f3be4736ed7b0664b7f867de39eb26fe7e7b750b452d357
MD5 1063fa94ce989649fbb682453d2675cd
BLAKE2b-256 ba629708848dbe89288868edb43b3f3250aee312e5a61b02936c503691e07115

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp312-cp312-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 d0734812cf9d84b79d5888c7f55eab4a61d637cc64e99f1b6a50e6d0d95493d1
MD5 e3fe5c2f8cde8932ffe72dcca8d63039
BLAKE2b-256 e2a631eb0f950d819c14d21d60fb460dd25d60eae45ee85eb84c2d0134711f87

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c31b17148a85c1265fae53c6d2a437e70c8545c361447bf8d95141f500ebdcc5
MD5 f915e75ff8c058ce797851868df7f39a
BLAKE2b-256 b4030ef9bdbb4754e018614149a381268fda0ec819ea492b5930afe1ce41aa4e

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fb197663c9469c83747b58f5270aa4c4fcad830c08ebecbf4a0ed305fdfe6e1f
MD5 438ed46182abf1238012de53a2fe1da3
BLAKE2b-256 6a1fba147aed9f23fc0be1af836fd8f322cb70de711b528acecc08dfb1d775f5

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d8061bd7277250e363e63da77a1ecb7c77ee0ea77d68bf2d759e57551bca623a
MD5 a13b756676bade8ab15907c3f36d1915
BLAKE2b-256 1601196a14473b225384213d06572cb672d27ecdc23c1d3712977ee4074abca2

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp312-cp312-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 dad241721e0c80bebec43258de7b9e3a574fbcdddf7a48122e47b74a9ced39ef
MD5 d5b675de1d19159cb118fe2e5bd684a4
BLAKE2b-256 735797a8dc47d84b96484d8b0fb3ed59b1835739cb343313f184e1568e4e29b9

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 a07318def7e5bd3fa61defc7e59e1cca31ea8b0c2f21969513d85f7c5e1427d3
MD5 b27ca8e8b0d988d88b4aa12e0db5b25d
BLAKE2b-256 aedcfb84d3ed9368b9a9cc4141cd8de35154d54e24d0fc574719b7992a3f703a

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 612a797b53a65dd63ac948569b83d761ef8e27675e33268dc1a06c65535334e0
MD5 ce204e14fbb1a8d0479c4ebd7016b173
BLAKE2b-256 201bf9cfaba9950257fc5b3a671fe17d2837fa01ad46c67567f7fd0e2b03b163

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 561df5f6a92f19e0d1ec9b65488420f73a525aa151fa5d1770d58c39bab5abec
MD5 ed4ca503b2ead4701a88690bbf5478e5
BLAKE2b-256 c73da7b621b1a5b8f3d3ce15453f54f1927b3fffc6e09e2815f6dfe1395ae51c

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bf6d18d72960caf34805e8c8c96e90d71b75c8ad42836822fb486d6b5e83be5e
MD5 ca1432311a840472f6ac2aef59f573b2
BLAKE2b-256 36023901c4ddafba0fe7a80b4f915d8e92e58a2794cc3b33cf9b999234bfbe44

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 9b729d082eb075a67501210cefb46a487a51f1144415afd2be73b5c97a4ab96b
MD5 e36bc4338289356319c93a87536bab5c
BLAKE2b-256 cc020967da88fa4ffc207b4dbf3c635d02ac1b303ee60a5fe1ec59bb5270b418

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp310-cp310-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 7b68472153256bb1d6791ea216bda6cf686f0a6d299afa2276a6086dd321d8a7
MD5 a9471d9a018f359f99eae472c77ec732
BLAKE2b-256 368b3dd47bfdd916b459053199193fce82248742e965db0049b4d13dd404d2d7

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7055a02cf3e669479340b8add591ddce298850521a5c1608eaf4933dac782d77
MD5 1aac17f6d816f7a3b04af618531cef0e
BLAKE2b-256 31b655d64fc6970088a49febf175026993f3ee1b8d0461a387b171edc7aa94ca

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bec9690554126f11ce45d06fc86e0d24262c22b44d465abed8425586da5b3c43
MD5 b41ccf5d3df67fa60583bfcf33ed90a2
BLAKE2b-256 10c19ca1a58ce021740834c74a90509b39663aa5e24efc7b22989f7ef1416a1a

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2427f23e1ace6e7592dc0ebe5b9ca0ef38dedcf94ce5f4d3cee63403ade3f0ff
MD5 23322ba9c4c156922d6eae913a3a2c31
BLAKE2b-256 e500e5bc60ce9a549716bb5700acade332f7b3273c0f1831eff8f3996da8b2f6

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.4.0-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.4.0-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 ca87c57c7cd6edf3ea3d5fb465013fd877341aa58569ce81152332b7d7475773
MD5 ff951ac57a4b97f0f2953d774a3061ea
BLAKE2b-256 04a239542b82fea8a7e0368c1b50e7419d8cc984b130a6624a44077dddcfec83

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page