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

Uploaded Python 3

search_string_overvaagning-0.3.3-cp311-cp311-musllinux_1_1_x86_64.whl (237.2 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (242.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.3.3-cp311-cp311-macosx_10_9_x86_64.whl (140.8 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

search_string_overvaagning-0.3.3-cp310-cp310-musllinux_1_1_x86_64.whl (238.0 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (243.4 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.3.3-cp310-cp310-macosx_10_9_x86_64.whl (142.3 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7adbe80e2a6aa8314332ec5072d814243acb65e904712ba8ec1e04d4e0486d98
MD5 144177fbf4ded5694529f36d311c4bbd
BLAKE2b-256 961391e3fb78eb9c90dda56f8de9694fd620247c6d27be2681160cdff3d4aeb7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.3-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 6ed7a2cd104f810228ada91cc74d1f636f53d716a3f3c6412607c9dc35d61e1f
MD5 aa0af62f6ac6cdfa39dd252b43ebc32a
BLAKE2b-256 4693d027562a72c84c59f4f8484e4f588aec467d9604f1ecfe7167956710d25f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2e691ee62150d1217d0894c0299d71a233548a6701ca2ad6e71289b5b6e18f63
MD5 c94241377941a31061cb3272ebe4567b
BLAKE2b-256 99132669826adbfa507131bd1251072f21014126db97791c2552d9178adb78d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.3-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e3a7305c71fde03e58f65898ea23c83030855133c57d70a3c7cf8f716b264c36
MD5 9ae13a6ac2ea594547bf68f9d3ebddb8
BLAKE2b-256 c6a716ec6d530b6d0f4b5f2ca42beed08c174d9490d9ef76f9ed675731c72726

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.3-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 5c548f9f6994c5ff4d486e3c7edf770a509c80ee5c9624137e4962f6d6bae85f
MD5 f7387734d1450c6970528d5b1246e4f3
BLAKE2b-256 a3d04dc6641ceb787e9b230960d8f0bef1847073aeaababe99beedc5b2da70ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 38e1714dbadcf7bccda50b3039f8d8e9fabf5c52c70b4d7c54b779f8d6ce1800
MD5 432543833dad5fc03b0949d7039b1b05
BLAKE2b-256 26153a32b52564b391f21f0e6f8fad3177ef4b8822be41946c4edc1db25e6a87

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 662e090a9e38fa2c27c07c50eb525cf24333375a74e11e3b047eb4ff397dede0
MD5 33f11d9fee15edd5371b8f9493aeba75
BLAKE2b-256 1e480e1228e2cb55c7220a63aae0f8e831ecb29039f45d9d7a895a34a3e48b97

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