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

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

Uploaded Python 3

search_string_overvaagning-0.3.6-cp311-cp311-musllinux_1_1_x86_64.whl (214.4 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.3.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (221.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.3.6-cp311-cp311-macosx_10_9_x86_64.whl (129.2 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

search_string_overvaagning-0.3.6-cp310-cp310-musllinux_1_1_x86_64.whl (215.1 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.3.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (222.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.3.6-cp310-cp310-macosx_10_9_x86_64.whl (130.5 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d0d0af489cc5625c908d7bd26040272838ccbf3282c356401523607879163863
MD5 55c640115fc2ac058ba82e871790fc68
BLAKE2b-256 81d721a665825879ea1bc0e9d048dec15182fa9e0c2dd3755c281ba7de5e79ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.6-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 114ef4879103d62098dd45ea0ce83632eeae02f6db74bac63769dd5eb9c65e62
MD5 160036fdbfe4d4305b4b2926afcd023f
BLAKE2b-256 c3a0701b1307b88d2380403e34fc70eb6ab43ae1356d9e24fc31ff5f487accba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b7843d94840a0709bf857bd313c6af1a16325b2a4462ba1ecdf8b8a4edcf2433
MD5 41d5158d0f0ba1837cdf05f67151bb89
BLAKE2b-256 d2b9f403c62036c2eb789d937989b1304ed4b59a5c2c7849a82cb669f0ab4bf3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.6-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 752351e2fcc08b946a8e1803db426823aa787381c7c522a37c477960e0f37fff
MD5 1ef63668947137e84f6f633d1579148b
BLAKE2b-256 408a06b9b6614f97f2b5eef46cf32903e4d4b004ac8c9377a3bcafba73ad413c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.6-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 a2476ae4a4f9e4f697d21600a1870866f6c926182ff545e820268c63d1dd4f75
MD5 0c629b48f4f81aea6940ae1a8832f534
BLAKE2b-256 70630f43a6781965c901af73952021676b695fae67e4567ad92051190ea6a36a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 24c5f2daefe25535971f2adecb71374bb699af559f2544ec00cf0888c97d44c5
MD5 05aeb5815fe2880f394352645f861682
BLAKE2b-256 b3776ea3449c7888af0a220e32b825216a95fcf06be02ad0187010dea2e0b891

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.6-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 cdfe6efd2fbb7ea785639da7761e71590e1aa1c305c434b481d2247a9f40bff3
MD5 4758b3f70257e30ab64c858ceda43ffd
BLAKE2b-256 8956e6f6d93d53f59e8a89537d37fa9e699e243457311cddf2a053407ba50bc9

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