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

Uploaded Python 3

search_string_overvaagning-0.3.8-cp311-cp311-musllinux_1_1_x86_64.whl (214.8 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.3.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (222.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.3.8-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.3.8-cp310-cp310-musllinux_1_1_x86_64.whl (215.7 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

search_string_overvaagning-0.3.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (222.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

search_string_overvaagning-0.3.8-cp310-cp310-macosx_10_9_x86_64.whl (130.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.8-py3-none-any.whl
Algorithm Hash digest
SHA256 970af0fe0f2b496ccaee133e17124b89f2beb92ac85cab527edfee21c6030ac2
MD5 43ae3d1c1e499b5f36481b9be9b85e45
BLAKE2b-256 b3ba6bea05b58bfd7bf767a4ae7a0df71234596d59b823d3fd683ef5ccb5d00f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.8-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 93792a0e3fa24ab75ed9b4754885ed2b27ca8ae5f3e072ff33204456d425296c
MD5 401874c9a3658e4f99fe1c004d8e5129
BLAKE2b-256 f679122a9c14e8a73a4f272bd0fc4f1f5bb35272b4d4a48234a53c05ac32a41c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d7e6b541f81c9912bb0ca0dae4528650cc2387cae3eefb6cb3e5c8f349b159f1
MD5 2357f1b36458597f1630d0215ade03da
BLAKE2b-256 27f28a969ec786be631ae1d195ddc1e8bbc83430b9b84749cd025ab50e6af216

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.8-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d6c4ff631923a39e6363b7295b3c1b0d1f5e3d3f822342b01d998c3d71e05868
MD5 9064da08491b1b3eefd6a1e0b67abf8f
BLAKE2b-256 b825774e0f1d239f9c65d4a5f8fa0b80cf6b253b3712a5be151f5d95b371da03

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.8-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 2340a4aca4bd0602744a5500ede5a181fff9395e17789dcd127da41582023324
MD5 1b69b0c9d6cff4705c3d51730760980c
BLAKE2b-256 7e4535d6ae8e8d0c76e64edc238c0f52702d264d58242bd1f674df86eb0d78e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 54021ce424d74fb2dbceb61a6ef12df90a98d65e55e83328214081f170560129
MD5 89af00e5bb1d999742982e130f94e08c
BLAKE2b-256 3c51c587ace3c452de65f470f11d3e5e0d19dbdc820ef3a2ae9f4de55e8fcb9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.8-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c478ee6319b5a2db6ff3f2653d168e5947c8748d177355b23a835c723a5f7917
MD5 2a49ff450854162134e6111c9e63696f
BLAKE2b-256 6eb494a42ac66a2e3dce03751f944b7c700ea2921559db682c2db9d101156090

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