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.9+. 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.'

Creating search strings that match everything

If you construct a completely empty search string, it will match everything (even the empty string), but the matched text will always be the empty string. This is to allow for "catch-all" search strings:

>>> ss = SearchString('', '', '', data=1)
>>> ss.match('')
True
>>> ss.match('Some random text')
True
>>> ss.matched_text
''

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

Uploaded Python 3

search_string_overvaagning-0.5.3-cp313-cp313-musllinux_1_2_x86_64.whl (252.2 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

search_string_overvaagning-0.5.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (249.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.5.3-cp313-cp313-macosx_11_0_arm64.whl (126.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

search_string_overvaagning-0.5.3-cp313-cp313-macosx_10_13_x86_64.whl (137.2 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

search_string_overvaagning-0.5.3-cp313-cp313-macosx_10_13_universal2.whl (244.9 kB view details)

Uploaded CPython 3.13macOS 10.13+ universal2 (ARM64, x86-64)

search_string_overvaagning-0.5.3-cp312-cp312-musllinux_1_2_x86_64.whl (253.6 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

search_string_overvaagning-0.5.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (250.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.5.3-cp312-cp312-macosx_11_0_arm64.whl (126.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

search_string_overvaagning-0.5.3-cp312-cp312-macosx_10_13_x86_64.whl (137.5 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

search_string_overvaagning-0.5.3-cp312-cp312-macosx_10_13_universal2.whl (245.2 kB view details)

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

search_string_overvaagning-0.5.3-cp311-cp311-musllinux_1_2_x86_64.whl (243.6 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

search_string_overvaagning-0.5.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (241.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.5.3-cp311-cp311-macosx_11_0_arm64.whl (125.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

search_string_overvaagning-0.5.3-cp311-cp311-macosx_10_9_x86_64.whl (133.7 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

search_string_overvaagning-0.5.3-cp311-cp311-macosx_10_9_universal2.whl (240.4 kB view details)

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

search_string_overvaagning-0.5.3-cp310-cp310-musllinux_1_2_x86_64.whl (244.7 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

search_string_overvaagning-0.5.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (242.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.5.3-cp310-cp310-macosx_11_0_arm64.whl (126.5 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

search_string_overvaagning-0.5.3-cp310-cp310-macosx_10_9_x86_64.whl (135.2 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

search_string_overvaagning-0.5.3-cp310-cp310-macosx_10_9_universal2.whl (243.1 kB view details)

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

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c5f101dbaffb5b9029dd6f30212af73e5195f8029c27b52c846a12a8d15d3bb8
MD5 3a25d09b97d3c73a96e84b6335f3db2f
BLAKE2b-256 ec42b9751960477d79bb534eb1aaf5e7a272fef0ae7cd8c23c0e00ee7471e288

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.5.3-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a7d23235c4c430e71b8187a4812424dbe8a9820273ad9856477e0aef94ff8658
MD5 cecc71b0adb84ac6c42f8824f69826b8
BLAKE2b-256 439924e8529075ce59d53a614c07b073c967b05d48bab61bf76bafb8a001af4a

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.5.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 663e312d320cf2b9eb394951a619b43291a962ebffc2583f0e934b9ac3b44c18
MD5 1aee55dc7e781054874269e36df8df75
BLAKE2b-256 bdc071011684098d06d3b8f80b6be6184d33d7bf83eb3262841807210c3b1643

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.5.3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 28ee9d186bbbdbf5f7bde36be7063c391361933f69d0cb83af07cf370c0786e4
MD5 ddb48e86735f290ef0c8428b7c8a2c2f
BLAKE2b-256 0cb281ea77b32264aae5284c072c685e60c12812c5ba32e8b638e8debf29c7cd

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.5.3-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 9c78b3124f3c82e86ff309c3f320b023029943b212023cdc938e05bac00b75f0
MD5 0cbb1813bc2ea322619f6a03f9cd0ab8
BLAKE2b-256 04e0ac0c96317294f943bcd31713b0f6ce75b4eed014d5eb9324b95d7771cda0

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.5.3-cp313-cp313-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 f0c63f1ba44e223bdaebe0ce8f42640f612d7fcdbb2491bdb3cdb1f37d4766ed
MD5 f6398ebaa912ea474a218f6b020a090b
BLAKE2b-256 6c1abaa7aa3e463b087194fae71fd5b7ea2a88f2885560af6a72f770de9d63aa

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.5.3-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2af9e837ce6f00ddab311de88aa0de5b5432a9d16cf5b0ba8fa7480d2cbd3c30
MD5 ab2656eab76cf82d2b5c93e1dfb3765a
BLAKE2b-256 72971eb31275c70aed03ba38b4e617eb9d657c5d9a248b49dab115eae9a66944

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5806687166f57be7aa3f1c0735c925e7bac640921a9a7e82fd98c47781b9ffe9
MD5 e00f4707f240a5c27d683e2d9291cb41
BLAKE2b-256 953a85902b4a568899f23fc3e7ce5c2eaceff0498e15b772f345280c249fed9f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7eac2ce7d8701fb72c2f61325d4341546c9b7f53857cc1efe204ee707169dc85
MD5 3be918b940ae28d6509ea3b80f174c7d
BLAKE2b-256 3094af7fbff37f49afa48f690d57caf6174b27ceddfdd1758007547e232030b4

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.5.3-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 eaabefacffdf5f71742f4e69e5b212b2e4f3ac1b6125c063a2d15085ee95e3a2
MD5 771136fe4b0dcf42113159c7b0deb8dc
BLAKE2b-256 910c532017080d809026196be968b7d2bd0a7fa12bc5dbca7018a1a1e0f69583

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.5.3-cp312-cp312-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 f2e6d714e0f44ff951a6c30cce4be8496a8f11012a8e50425c6867c1a59ac5c4
MD5 481c8e33e72ad35154da96ed28dafc1e
BLAKE2b-256 20b97de44607771b390ca2c56130e56028855b8d72246b2f9882b146d29c79d3

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.5.3-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5df8ea3103b9b5942d7e0278ec2beed226495e96d493e073656abfbdde230ad6
MD5 6c08ca427a516a5b2f2320739496c05a
BLAKE2b-256 11b3ee3f857d4669ea9a46d1dc2e84fc6be1fa0ae6a9b06f864ea922c0ea213c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 66e85efe811431d37264b74f87949fc1de1b7b39f1e3bff84e6c34f47d76e865
MD5 1056d1bd98a23056369358958fb30be9
BLAKE2b-256 45553db87f7267f63aac1718029ae33407d2e8f2ad7e91d9ce6246d479d97077

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 adab722cc971e44bbeb92113e1294eebc7fef23d2331203918a67b5d0e4f0030
MD5 9adbd3ae6120d0ed4f8f06643b508eec
BLAKE2b-256 293177c728df784adce1d15cddab6c99faa398e1e4a33e137dac4520780bf066

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 539c65bcb10d61ed4760e37f6cae9d980be38f5c0db201aefaa33e98e6e33bac
MD5 54e634529dfae6f16ca1d50e57cd5df4
BLAKE2b-256 80770783f26b3a97703d41d5331cb22c32a2bdaf321043998f684a6e30cb2eb5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 dd3dc476f61013968e2c0c4f6f62a7e8fc0e62caa89ebdf3189ca9db3dc51823
MD5 27b37a962936360d8f101b93c6c7b56a
BLAKE2b-256 5544405cec0d6049a1f40f96ddb9384a56c18ff06ac56d331dc4695248044e94

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.5.3-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ac1f1ae116563b96a3f2d0c98d571f31124d5af14e3c488319878838203beb8e
MD5 3cfd6fd93a8b39729bb5ceda866b902e
BLAKE2b-256 40a8359b735f4e87d82fb5b168ed02a05065eea07339377c65fd90c6e2a8206d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0a27fdcc890450d0be1228f7dcf361a2db2fa427744d55f1e78dc8d17e9ea1d1
MD5 dcbc769af29ffbe5042cea264bb97af7
BLAKE2b-256 8db4cfca5f17c281f98e60e5697a6ab4145a900b31eaebaa9aa840696a44a58e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c0a8b5e0daf8ade7fb893d0246dec5590693a57f779fcd2d32099d62420e57d9
MD5 f984a55e424ec57fa110c04592eae42f
BLAKE2b-256 3fbc95e3d6b9859850050cb8132edf02ac77561e3b9357d92d22e1c6dde85492

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b73b44428af8ad13467a538dd282ec774e46b721cf4ee528110dccf77cf8414c
MD5 fe4a03a173a3c98b0fd7001c746ee439
BLAKE2b-256 5ace466706edb1f701e06866385401f1a77b620a91e2a3ad3633c0e4bc33de23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.3-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 89ce0c30ae09063af85d16e56117b899168eb0508111ac7ec654e8a13a899c98
MD5 ac9d36d514bde8b601f9eddf47a9a9ea
BLAKE2b-256 12ba30ae281ffd2e53bb3113173c598455796eb8ca29ff71e60248541f2bc1a6

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