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.5.0-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

search_string_overvaagning-0.5.0-cp312-cp312-musllinux_1_1_x86_64.whl (224.9 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.5.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (231.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.5.0-cp312-cp312-macosx_11_0_arm64.whl (121.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

search_string_overvaagning-0.5.0-cp312-cp312-macosx_10_9_x86_64.whl (131.5 kB view details)

Uploaded CPython 3.12macOS 10.9+ x86-64

search_string_overvaagning-0.5.0-cp312-cp312-macosx_10_9_universal2.whl (236.0 kB view details)

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

search_string_overvaagning-0.5.0-cp311-cp311-musllinux_1_1_x86_64.whl (217.1 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (224.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.5.0-cp311-cp311-macosx_11_0_arm64.whl (120.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

search_string_overvaagning-0.5.0-cp311-cp311-macosx_10_9_x86_64.whl (129.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

search_string_overvaagning-0.5.0-cp311-cp311-macosx_10_9_universal2.whl (233.2 kB view details)

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

search_string_overvaagning-0.5.0-cp310-cp310-musllinux_1_1_x86_64.whl (217.8 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (225.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.5.0-cp310-cp310-macosx_11_0_arm64.whl (122.2 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

search_string_overvaagning-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl (130.7 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

search_string_overvaagning-0.5.0-cp310-cp310-macosx_10_9_universal2.whl (235.8 kB view details)

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

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04388284744deef178a81e12f6e9285c3307a9326f827c23b5a8eba216501e66
MD5 b44c302de456fadf571f8a5adcb6a1ca
BLAKE2b-256 2c58823c8ab91237312d787c2b686a0ee96aee44f43079dd649a17d32354384a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 4043b0eaff3f6911428d5e3351347db4b35fbe34c0d32c5283f0bf57c81cfc96
MD5 8bc3108686b427c3e3b7ae19f808b70e
BLAKE2b-256 2bac490a693a3be0b98f6799dd6dadabe6a7de638769291a286140c079db500e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6e8c41df330e57d5a11959c0430d406833d7d35b84e1527b575289e34242e30d
MD5 c0712fcbb762bb6f2c21f2a86089ebfa
BLAKE2b-256 98dc030ca9d0f0236fb89368f03e63718971bfef3b0e6ceada74be974719c5e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 75ccb9adddbef8c3453ca053bef7afd464b9b767d7e5c7bb4186c777f5c02113
MD5 247ecd7d804fc8668502245a8948e9d7
BLAKE2b-256 b342c3c9d09e646d463f397193f3475f14c47ec8dc050546b0cc67a0707c8e36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6c4f12f6dddbbf1b94d0ac2cd270780bec9dbe090662f88246514e8528dd0bde
MD5 da94fdd279285eed480cfb8148e5dfb5
BLAKE2b-256 bd4bbbcebf41d2121b4303b75550608e90d728a3b16ba840ae011334b46cfdcb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 6f187862d8c481dd9333fb2a06d077794a21c29453f6b618b1ae1a68048c85bd
MD5 bf1cea1cabc44460dd2c45cfcc4c4d48
BLAKE2b-256 a6d0125a809e98113cc8f9a8a881ef0517f3dd7d2bc23a42b6a6115d8eb12732

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 9f7ade92a858bd551ffe8ba65ad07dc5b00a972bb78bea332dfd563d12b70b6e
MD5 6edee633f4a3ff454b876c61b2869345
BLAKE2b-256 31375277437d50ca805347dca257b22ad87fefcb3e59c57a49a70f9e3410be9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 958b1fd83965c107e96abdf1624034b8ace61d208df4f62791b0c223223786f8
MD5 5f86d27e72488e93607604180116034c
BLAKE2b-256 5a18f261ab05ca8d8fa0b2e7eea5e9f493ab2b95d4dbb41f03f44c674c45f98a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5f29f7f2b0f1d78fa0bcc461f422bff3ad6dcf45a0263639a58c22c136feb219
MD5 a2c9aced6964629951105bb43c5da55e
BLAKE2b-256 c17e6afd345876e5978abdf6c0f42a77d5d208f6e4f886f0496daf09bae14efc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7bb3800132bf71dda694c318e0753c15d2424e9dd9c09cfccb30b7e08b37faba
MD5 05a2a2fb0dc9dbca3fb20a0a528b2fbc
BLAKE2b-256 ab75da897877cc425863beed7554165ee4035c58d7c06320bd1f433486c4bbce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 1bb823ebde72f5929b63c4a0ac24a4c94b09e76101175f538f5b2a5c7aea00aa
MD5 c4c8bd2fa34ea9fe3ea4435f95069de5
BLAKE2b-256 5a956b78266d3d0940c993187ea2cbf78e37947912157923c44e9e6ad24190ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 d7e33801a4c143b24c893f32fb42b2a899ab31a2901fcf9907f85c275c048bb3
MD5 9c26771da2b88e3838c09da5ba73bd64
BLAKE2b-256 4e61fde19bebf30f055df6a700ad8fd4f4aeae0078637b9793006d3612821f19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9b8aa360c8540c4c1343f0889b24ee9c8e487b8cc8310519cfa9b1d1be60cf3a
MD5 d650bcb5a2f19007ac2bcb911682bde6
BLAKE2b-256 347ad00b50ade58c482e4a48f951805810e1b30721b558832bf9f6849f93a8a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 244aee37472841c0d09ce6284f4eeff1f97da9468e3e1e1031cab28eb44d3a4e
MD5 de8dd37f2a68c594c6edba8198349d84
BLAKE2b-256 54122588ec6674896464c39d302df952442e9480fd94c5551bc276cccecce749

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 31b8f1767fc277f3a80282eb6acb4d8eb6a3ff8ffd31d49eecc581085d1892a1
MD5 05f89fb7cfaa378b5e9dcad04b478e6d
BLAKE2b-256 c952b75e4ea6d54a7c91eb31454e72fb6fcdd971e86580b4f17013970833ab68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.0-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 91fc3637db247cee55f1a2129a5a645e52d1b2e2c640b8b841ff2807f57cc31b
MD5 842435f5a643829ecd5eca7b0e01425d
BLAKE2b-256 ce39ec741989aa2cab4e8cc71ae721dcbfdfb2d7cf5ce2a71cd4a59b8cad610c

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