Skip to main content

Structures for doing substring searches.

Project description

substring_structures.py

Structures for doing substring searches. 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("bababooie")
>>> "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.1.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.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: substring_structures-0.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 08eaaed0589c0fd217fcbcdfc8a78e78da488d2dbfe9775d067b456dd9ab64ba
MD5 e48cdf0bf04324fab9011a659b67d03a
BLAKE2b-256 f09978314408c59b77faa228ce40901452529b4501f3987b413a7f721483d796

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for substring_structures-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a2b274387e39fef21694f4809a565b214faebfe0f134d3f0d65a3b5666d16a74
MD5 61d83efc265f361ac803f6be065f596a
BLAKE2b-256 25146d3bdf7a963c2c92019d939fe4549dc9cbfde92c748869fd17bddf09358d

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