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

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

Uploaded Python 3

search_string_overvaagning-0.5.2-cp313-cp313-musllinux_1_2_x86_64.whl (243.6 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

search_string_overvaagning-0.5.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (241.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.5.2-cp313-cp313-macosx_11_0_arm64.whl (122.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

search_string_overvaagning-0.5.2-cp313-cp313-macosx_10_13_x86_64.whl (132.0 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

search_string_overvaagning-0.5.2-cp313-cp313-macosx_10_13_universal2.whl (236.2 kB view details)

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

search_string_overvaagning-0.5.2-cp312-cp312-musllinux_1_2_x86_64.whl (244.2 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

search_string_overvaagning-0.5.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (242.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.5.2-cp312-cp312-macosx_11_0_arm64.whl (122.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

search_string_overvaagning-0.5.2-cp312-cp312-macosx_10_13_x86_64.whl (132.0 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

search_string_overvaagning-0.5.2-cp312-cp312-macosx_10_13_universal2.whl (236.2 kB view details)

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

search_string_overvaagning-0.5.2-cp311-cp311-musllinux_1_2_x86_64.whl (236.7 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

search_string_overvaagning-0.5.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (235.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.5.2-cp311-cp311-macosx_11_0_arm64.whl (122.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

search_string_overvaagning-0.5.2-cp311-cp311-macosx_10_9_x86_64.whl (129.6 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

search_string_overvaagning-0.5.2-cp311-cp311-macosx_10_9_universal2.whl (233.6 kB view details)

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

search_string_overvaagning-0.5.2-cp310-cp310-musllinux_1_2_x86_64.whl (237.4 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

search_string_overvaagning-0.5.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (236.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.5.2-cp310-cp310-macosx_11_0_arm64.whl (123.5 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

search_string_overvaagning-0.5.2-cp310-cp310-macosx_10_9_x86_64.whl (131.0 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

search_string_overvaagning-0.5.2-cp310-cp310-macosx_10_9_universal2.whl (236.2 kB view details)

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

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 24390de733628e5308606ab8ca614e2c813cc2839a37a7e4ea59671b9f3279f3
MD5 7c11288228b34c35ac385be5baccca98
BLAKE2b-256 ea23819df73d2e9f4ce24bd0ffb43e19a8aa5eaed6db887868039efb240a6fbf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f0625b49607213b33edbde0e4164ac8204c1000d88a2f4bbcb6eda611b8008f1
MD5 45ee9e549cf21db30531dc483798cf2d
BLAKE2b-256 8352044134e6fadfa76c2e2be254513a845d913217cf813ae2a7eb0f8adf2f05

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9b5974eb1a3a54188c6e4199a515b1963da51a55fb24cb35f9ee3259ee755131
MD5 98734a5200724dae813b3acb161de05f
BLAKE2b-256 754df0b1e61eb2352d82a484b66b16fcc1108b03f9fab0f2542629b1dec00e26

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 60e2b5a7f5628a8f2b4588220c055d45f4160bd78b59464fc1d5c8fea5fe318a
MD5 588ed302e2ce77e168f55e4f6add96e2
BLAKE2b-256 6fd8ed9abf17afbb78bbd70c47736d0a7c5f0aa9c22a0bb32735c45586922d7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 185e45564dd3191c3b9b4aa7a0f9842b90d27fdb7d9af88564957316cd1f56ab
MD5 0f91d534347ae7d62e35b586910e2ba6
BLAKE2b-256 c6f661c64267b840e49df9f1062abe98823755e15364783af31998e5013ed43d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 f1239f57971604e60863250c9261cd063bd535cb31d582b846c2e49e56431e00
MD5 3326aadb5ec39e0f00bd2f46a69ea112
BLAKE2b-256 ebb658fd61ef9fd183ab755ab71cd034bd84069e925dbcca29c721df8605fe63

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9c69a04a3e4c89ff83f59397278aaa24e4e515213c126f62eb3c9d9d7dd1abc3
MD5 32a7f7572e58ca03c1a0b4e5673b4974
BLAKE2b-256 f43016d36b097d3ab5f56cf2467a3a8b85d378f66fb68d13276606d20c1625ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 74bea1dd0c3f41a3543bb9f82c1a6773f173fe6eafa0e1b316fd7f4da29f4bfc
MD5 37090274aa5a0ff6810cf538605f359f
BLAKE2b-256 bb7df7fe471973e5bcd1bef5887e66aacf43f5a0ba29060bd3a3593aa6f6680e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b160cdb0e33aba97a4e5bc9747007d878da358ecf330769c5c74cb104edb870f
MD5 b70e54f8cfe8b4acadf9426c04591396
BLAKE2b-256 4e11ec8569a335961ef03d731eea1dc3230856496d07ae0493f2b177a24c2399

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 ec0c5eb531c64253851d4911d0d28c45761a44c582b38cd0c2ca34d40df32516
MD5 cb5e655aa20d57a6bf6b8ceab5a38d62
BLAKE2b-256 62da5406a16619ce7df0fb36152014846849b078ac4e65e8e5e5c77da05c6cb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 fd86e5fc0a8ce4d84069493fb2733f45c0dbb6eb292ecb41e907b3d3f186be83
MD5 b3c58a3c02eb6a061861b026a1e9219b
BLAKE2b-256 5bdc67aae53651ce0b4e08ea3703e0f3ccd4c8eb5080dce5c268dd1a263604c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c2760f55e296e71a96a1bca6e127b9751b349a268cc3a761f98252b767745837
MD5 6cd76731768fae125cda2b2e1232b4fa
BLAKE2b-256 94ef98a35b127db2bb02fd019d6824c2de5600d4ed0609ee61acfbff1f84331c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 94f4900980f472ffc10997fb153facc2be428db94cdcd570a26a64440092cca1
MD5 0ec376e431124ac5a8ee2be0eade10d2
BLAKE2b-256 6f7dd72e27e1b8895cdcab282c3a1a94b9b5a3d72243f1f0f5dc249fbdcfc009

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e8dbb01b1217c572cd32fdebc1e93249849b40c4fd36d8bac1a8217e9322700b
MD5 e3e900b368c0b2bf581f86db2665b474
BLAKE2b-256 2ccf03c3cb61216b1e86d85d7e31f5c3865977f4d012106b4d2336de47e393e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6065dc46f74988684ec105316803a53deff9b51a04df5158900a03743d3226d7
MD5 2932063b3b06ef92727623ddaf306ba9
BLAKE2b-256 58d39e278821272e5dd5c0d3d9bc25fabc57b66055fbd900b59c587c1e3a18c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 a38a69036ae9a20c70c791ec48fc357a048a964582266270f20645c0d7304aad
MD5 cac3ed2a472ce1995e49ab73d6f38e59
BLAKE2b-256 0f73034a7696161c9335ffa5d806bf9f7f52ca45ef1d94b251749d30f152ee7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0fa039d5e9a3105eec5bb9117785bcffebb6704dc8256bf83c898b8dc92d88a0
MD5 60766883aa925c84f7687b13932d3e3b
BLAKE2b-256 6e443f11df33ca7810b80d35148b9f638d51eef2762171e352f00e87b7be7bc9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d8b3a6c11bec0e718c1f29699b48721096e8544dc2efcd4fcf1cebf5a78b1724
MD5 504c49bfdec56f5c9ba2637256199b0c
BLAKE2b-256 623cde2ed5e6afb80dff8cd5f8287b931e30c46dbd8fb3209b6a38c9f0245fd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0bf39db6ba2a3584448d9813305109121895391d3e3829c56423f1438a3535e5
MD5 9da037f39bf64318a26278317492191e
BLAKE2b-256 f9b89de14f31dd26a2be2c8dd8014a1ce00d65343ddf5da0a51aac0e54974243

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f9862e47525215ce8c03cb236a3b1e39859950a73df7cd9920d58d743c2b9ad4
MD5 a3653c2cca091afdfcdd5f9fdb334303
BLAKE2b-256 32b13c13c0b1068dd08be163239dba47e0286e7b3fde4e248e00fa847df32b1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.5.2-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 ba7400092174a2a2c66fcf025b3245f1ca41f33a4d407ae30658d565e77ae8d1
MD5 97c64f8d0af556fdb98a5faaf7ce2088
BLAKE2b-256 6c9e44e5acd1be83529b78ea8fd5f31fb14ea948f394b5cd171be375dc2ee958

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