Skip to main content

SearchString is a custom implementation for searching strings for overvaagning.app.

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

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)
... ]
>>> 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.3.9-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

search_string_overvaagning-0.3.9-cp312-cp312-musllinux_1_1_x86_64.whl (220.3 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.3.9-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (227.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.3.9-cp312-cp312-macosx_10_9_x86_64.whl (130.7 kB view details)

Uploaded CPython 3.12macOS 10.9+ x86-64

search_string_overvaagning-0.3.9-cp311-cp311-musllinux_1_1_x86_64.whl (212.5 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.3.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (218.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.3.9-cp311-cp311-macosx_10_9_x86_64.whl (128.6 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

search_string_overvaagning-0.3.9-cp310-cp310-musllinux_1_1_x86_64.whl (213.3 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.3.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (219.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.3.9-cp310-cp310-macosx_10_9_x86_64.whl (130.0 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.9-py3-none-any.whl
Algorithm Hash digest
SHA256 f0d15b048ee5abc0f1846e0754d62a33145e33accae91a59d7fa25558ef918ca
MD5 0f2ba1cb0e056f7278aadcf3144cbe27
BLAKE2b-256 f4c0d81e0b1a836ac5b330db9f7c8e01b2b8bc3c65158bb00629e540df780df6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.9-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 45b4c07300641b1a758443abfcbc1ef602f33d9f6f5b0221561619210d301b94
MD5 d27eeedf4478d900a5c3247a7ae19a5a
BLAKE2b-256 4ae577bdac52e257c7f9b021b992a083753669ec43cb1866b250c0932a9c903a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.9-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 978c147297ecb688348490cbd7906440625c3da63aaa44caf93580082059c86c
MD5 9187d72dfdd57ef3fb9c6fbd16191047
BLAKE2b-256 2ccf6e8a51e41e1ba3d490148e91cdd118d429b4393d30cb5951f75d4350d5a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.9-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d3cebc55e12772b574558bba991fa88ecf0f45f270cf33922b0674e994bd9302
MD5 f710356b37d6edd633e6dbb55e51106e
BLAKE2b-256 896c7689ff05c0d39a33291eb73d4451ed42bcbdf9023217b35a6e1d66474786

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.9-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 3880e31a123ce8aad438419771d2b2782f9c64dac8a7fc8b6ade8785e49c88d9
MD5 d9c127991cedb2797df28e1960c4f548
BLAKE2b-256 6a591f8e04cc8dbffcded8701541fa6c327b6041ecdfb9a56f0950fa62f3c3d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e9769a07bc0cead1a2fe36ad8069ddda7b547085d89f0e61d5378618e447de73
MD5 b7c609bead19d38a6b56182357625dd4
BLAKE2b-256 736a7de228ca465d172f911f8e491c02048846918b68fca32b6b470fbb4319e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.9-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 26a4d65274a32e5c8f7e3b5bd5c424f16ec9e448f44ac75ccfb30bd84973c155
MD5 8fe14688436ef0a7fe72833692bef6d0
BLAKE2b-256 7a365580f07e83527d127e80281acdd650da962e9306fb1977b3bfb736d76341

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.9-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 d17a555f93d45ee705f49467ba16c7d40dabfa63ede5adf56f0848eeaf6b5c1d
MD5 dceb58e38aa504960857477a2249bb6f
BLAKE2b-256 ce467c7bc7f32c9e9abb63abb8594ebbf9cb164061e0a3e5caf912c1920e7531

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3201852ffe0b7110e515516bcb090ce4a41d601ff6f8b41aa6860fa6e44290d3
MD5 cfd69492c03b29feec9cad02c75a719d
BLAKE2b-256 6f57d358152b0f5bea2edbd83bc11a815ff35e701dc29c0d9c573865698f3375

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.9-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 11b7d1a28d4dfad2dbce2e72667cb77ed3d5b44d256731aa8abaab2106c1289b
MD5 bc354c333bd26d1768fc65aeae791ac0
BLAKE2b-256 406c8c795f7bee8ddee070eeeb2d2be5efc011cfddd809954fbcfa1e2880d610

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