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

About

This package implements the search string object that is used across overvaagning.app 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.2-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

search_string_overvaagning-0.3.2-cp311-cp311-musllinux_1_1_x86_64.whl (173.5 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (179.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.3.2-cp311-cp311-macosx_10_9_x86_64.whl (102.7 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

search_string_overvaagning-0.3.2-cp310-cp310-musllinux_1_1_x86_64.whl (173.4 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (179.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.3.2-cp310-cp310-macosx_10_9_x86_64.whl (103.3 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 14876656ca67fb2d9e7b2a565eac3a0a98e5d2e4951c737a49b817c21a4d7270
MD5 026b8c7f967dc1cdf2f51dcaccaa7b0c
BLAKE2b-256 45c33b631ebb5b4f4e8ba0676ad8adc9e120e063ec6bd48a92c88d07bacc1dce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.2-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 1583e8797142a4dc50b26467743d7447ac70d9814e668f094590c45ab54a082e
MD5 10a597f0e13cf5d4d6822be57d7b7a8c
BLAKE2b-256 a886e49a27c17e5ff2c0750efba587fbe71fdc09c138bab77fc36b7afa328ff2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 679f8f40d02e2f4df76f0886bced88c46942737b85f812db804df3394d45de7f
MD5 c35db957a4cb7934beb81e352da59850
BLAKE2b-256 9c35a5bf475dfcb2da953b8f0db81a8a34fc943bc7d78f5d3e1c0299bb01f6fe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5c83a66625a471a8d821e3520a9d999dd048c87d93098dd4a834af1df3abb381
MD5 653363a234e2b8b96af3512a2879af8a
BLAKE2b-256 26ec4a0806771dc538a8490477409c7c15244076b7d1bb3dfcd16ae7fa201f7b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.2-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 e83be591e449dc29df62de36f580cfa8dbddba817911696ec144493ec4896f0a
MD5 bca7784f622a6e4a036d783b9ad67d2e
BLAKE2b-256 5b491bd4d4ebe6f05220b1426ffde9600a8ad4b10f8d5c85b119e3de8b7b57fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c50d2ee6f24f4aa0997539126331bb874b6acf39acddfd8b644458b78eb761a6
MD5 27a42ea84a83579283eae27c100e7280
BLAKE2b-256 dc9f12c33f8c19060846353b1972f1dd35cedc20275f85eeeacef06ddcda6409

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5a8f801241a2dc2f83a316d840f5bdfe1824e0a1e5b87bb094bee817fa279abd
MD5 9fe5afaed4d6794ba65a8d2aead07b6c
BLAKE2b-256 eb68ee5d2d8901719d8bf1228d2e072c74a3d811ab4ebf2a6f4f031f38d7ef16

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