Skip to main content

Structures for doing substring searches.

Project description

substring_structures

https://pypi.org/project/substring-structures/

Python library for substring search structures. Includes implementations of Ukkonen's algorithm, the Aho-Corasick algorithm, and the Knuth-Morris-Pratt algorithm.

Ukkonen's Suffix Tree

>>> from substring_structures import SuffixTree

>>> hello = SuffixTree("hello")
>>> "lo" in hello
True
>>> "he" in hello
True
>>> "hi" in hello
False

Suffix tree implementation based on descriptions from https://www.charliemistrata.com/posts/ukkonens-algorithm.

This suffix tree can be precomputed for a string S in O(N) time using Ukkonen's algorithm. It can then be used to check if a string W of length M is a substring of S in O(M) time.

Aho-Corasick String Finite State Machine

>>> from substring_structures import ACStringFSM

>>> greetings = ACStringFSM({"hello", "hi", "hey"})
>>> greetings.find_substrings_in_superstring(superstring="hi, hello")
{"hello", "hi"}

Structure used to preprocess a set of substrings to efficiently do substring checks.

Based on the Aho-Corasick algorithm, this structure is an FSM (finite state machine) that can be computed in O(N) time for a set of strings strings of total character count N. It can then be used to efficiently check which strings in strings are in an arbitrary larger string superstring using the find_substrings_in_superstring(superstring) method.

Knuth-Morris-Pratt Prefix Fallback

>>> from substring_structures import KMPPrefixFallback

>>> KMPPrefixFallback("bababooie")
KMPPrefixFallback(string='bababooie', fallback_length_by_prefix_length=[-1, 0, 0, 1, 2, 3, 0, 0, 0])
>>> baba = KMPPrefixFallback("baba")
>>> baba.contained_by("ababab")
True
>>> baba.contained_by("keke")
False

Structure used to preprocess a string W of length M in O(M) time so that you can check if its a substring of another string S of length N in O(N) time using KMPPrefixFallback(w).contained_by(s).

It does this by generating a structure of string W that allows you to "fallback" from a longer prefix of W to a shorter prefix of W on a character mismatch when searching S. The code here is based on descriptions at https://www.charliemistrata.com/posts/knuth-morris-pratt.

This class is mainly to illustrate the Knuth-Morris-Pratt algorithm as python has a fast built-in algorithm for substring search.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

substring_structures-0.0.2.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

substring_structures-0.0.2-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file substring_structures-0.0.2.tar.gz.

File metadata

  • Download URL: substring_structures-0.0.2.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for substring_structures-0.0.2.tar.gz
Algorithm Hash digest
SHA256 88d054203b9e49f15b1b10dff73ca5e97b5bd3b20fcde8014573fb0838afd170
MD5 0371663e1265636e77e11e98de6995f7
BLAKE2b-256 15402e74e1f239a2d523bf22b4eaa6344b4b0f1f966d60cbc35f6fad0e23eda5

See more details on using hashes here.

File details

Details for the file substring_structures-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for substring_structures-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a6c1690d9d7ca56a1fb4c248ea0be5414f9cfc89433865654ba51bc5e6495ae6
MD5 0182161ce78677409a00b886c3224cdd
BLAKE2b-256 d216778d5ae78bb769def722d202bf91fb2f5e8d2d2f93c0cde6b865d4cd805b

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