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

Uploaded Python 3

search_string_overvaagning-0.3.10-cp312-cp312-macosx_11_0_arm64.whl (125.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

search_string_overvaagning-0.3.10-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.10-cp312-cp312-macosx_10_9_universal2.whl (239.9 kB view details)

Uploaded CPython 3.12macOS 10.9+ universal2 (ARM64, x86-64)

search_string_overvaagning-0.3.10-cp311-cp311-macosx_11_0_arm64.whl (124.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

search_string_overvaagning-0.3.10-cp311-cp311-macosx_10_9_x86_64.whl (128.7 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

search_string_overvaagning-0.3.10-cp311-cp311-macosx_10_9_universal2.whl (236.9 kB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)

search_string_overvaagning-0.3.10-cp310-cp310-macosx_11_0_arm64.whl (125.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.9+ x86-64

search_string_overvaagning-0.3.10-cp310-cp310-macosx_10_9_universal2.whl (239.8 kB view details)

Uploaded CPython 3.10macOS 10.9+ universal2 (ARM64, x86-64)

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.10-py3-none-any.whl
Algorithm Hash digest
SHA256 1312f7cf2cad25f3d1ff525b062cc32ead3beea03b88395fd957dba8a3a4d9aa
MD5 43b8ece09658168258e618884aed7045
BLAKE2b-256 45161e14e6ad863419dcd3ea3c5cb797ba583de03a1e151541afa0c761827be1

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.3.10-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.10-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 066c24e8bb14e5033bc2c7dc76a7c09e07bd93a0e283b8d131b48d79834885ca
MD5 101d25fa71fde99b62bbc0e6f26e4c29
BLAKE2b-256 c1ff700f6c05156cdee4f74c82dcaa5657d4d4c75123ae62e3c75c7f5ff49cd7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.10-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 18f462472fa0e7d0cda9e22e10714aa7d957d6c3f8d61711a18739357a3a0fe1
MD5 2e4fe64f2b1666688b61899362f313fc
BLAKE2b-256 4fc3c9c30e47ef0ca15a5d785a3bc14a336e489cb34d6afe613151877679e1b1

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.3.10-cp312-cp312-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.10-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 d3d29e9180f2f5d701397fa1b68fab7c4a381b876ef2e0ea57813eccf9eeae01
MD5 d39b2ac14a31cdb32cf7f7d1bafb282c
BLAKE2b-256 0e8badf9fd2d6537b0c28b5a602dbe77675d733181cf13d57757d2d7d8b88491

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.3.10-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.10-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5aef3fe20c31cdd018af130cee369d8678730e203f859f33cab9d8fc7e273483
MD5 1f36be659c513729b6a0dc4ea9cfd344
BLAKE2b-256 fab14bc0ca1587e1cb43d308db40b09e2f76dbdbe8336818d55943057c88c570

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.10-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a9c816362f3ed9b2d26d6e30bb1c7397c94a6584d79848b71ac7873b58c2e045
MD5 1c6d0520fdae5e3514f4975e50da87ff
BLAKE2b-256 84bdef756a40df1697f0dca54f89e4d3e9cdf7ac835cae540753f30628e5a73f

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.3.10-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.10-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 8ced7ee67ce1c3d651ea33860f810b5cae65898d863293b55f5421c37c116bf5
MD5 b20354159ef7803414a5bb76cc564787
BLAKE2b-256 c36ae44dde6ed73f80336fb379814bc0881612a59b87df4d9faae671d5b6e911

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.3.10-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.10-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b88d1c92b238cf6da91e03900bf7b045626033b4f1faed9b096df98d6f197f8c
MD5 a4d8ac5f7fe884d1d2d581f8368b8f1e
BLAKE2b-256 f332aed851d21cab76cd6cc3ba91d8f660eb98dcc942279aad9893f20c7488ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.10-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0280327ccf83c53665beee7fa972a1be6073a5ffdb4451ab5f77e5c7296a2ff1
MD5 41857a7ba192bcfaa61a547b8084dd5b
BLAKE2b-256 d301f598733b8ecb5620ee179fd3ecdaf4f04d133fa3ca672e4bc4314d26a568

See more details on using hashes here.

File details

Details for the file search_string_overvaagning-0.3.10-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for search_string_overvaagning-0.3.10-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 1fc6f9269b25b08e1465a903bbd46b1919a4be2c84ad438ec80df9c29218a493
MD5 8e91274d42e22b1d670995d69f2b150a
BLAKE2b-256 bf8e362edacbfbaf6935fde300bbd000e27f368f3b99c264a22a2f1a6c467aa6

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