Skip to content

API Reference

This page contains the complete API documentation for PrefixTrie, automatically generated from the source code docstrings.

PrefixTrie(entries, allow_indels=False, immutable=True, shared_memory_name=None)

Thin wrapper around the cPrefixTrie class to provide a Python interface.

Initialize the PrefixTrie with the given arguments.

Parameters:

Name Type Description Default
entries list[str]

List of strings to be added to the trie.

required
allow_indels bool

If True, allows insertions and deletions in the trie

False
immutable bool

If True, the trie cannot be modified after creation

True
shared_memory_name str

If provided, load from existing shared memory block

None

Functions

create_shared_memory(name=None)

Create a shared memory block containing this trie's data. Returns the name of the shared memory block. Note: Shared memory requires the trie to be immutable.

Parameters:

Name Type Description Default
name str

Optional name for the shared memory block

None

Returns:

Type Description
str

Name of the created shared memory block

cleanup_shared_memory()

Clean up shared memory if this instance owns it

search(item, correction_budget=0)

Search for an item in the trie with optional corrections.

Parameters:

Name Type Description Default
item str

The string to search for in the trie.

required
correction_budget int

Maximum number of corrections allowed (default is 0).

0

Returns:

Type Description
tuple[str | None, int]

A tuple containing the found item and the number of corrections, or (None, -1) if not found.

search_substring(target_string, correction_budget=0)

Search for fuzzy substring matches of trie entries within a target string.

This method finds any entry from the trie that appears as a fuzzy substring within the target string, allowing for insertions, deletions, and substitutions.

Parameters:

Name Type Description Default
target_string str

The string to search within for trie entries

required
correction_budget int

Maximum number of edits allowed (default is 0)

0

Returns:

Type Description
tuple[str | None, int, int, int]

Tuple of (found_string, corrections, start_pos, end_pos) or (None, -1, -1, -1) where start_pos and end_pos indicate the location of the match in target_string

longest_prefix_match(target, min_match_length, correction_budget=0)

Find the longest prefix match in the trie for the given target string.

Parameters:

Name Type Description Default
target str

The target string to find the longest prefix match for.

required
min_match_length int

Minimum length of the match to be considered valid.

required
correction_budget int

Maximum number of corrections allowed (default is 0 for exact matching).

0

Returns:

Type Description
tuple[str | None, int, int]

A tuple containing the longest matching prefix, the target start index, and the match length.

search_count(query, correction_budget=0)

Count the number of entries that fit a query string within a correction budget.

This method is optimized for efficiently counting all possible matches without returning the actual strings.

Parameters:

Name Type Description Default
query str

The query string to match against the trie entries.

required
correction_budget int

Maximum number of corrections allowed (default is 0).

0

Returns:

Type Description
int

The count of matching entries within the correction budget.

add(entry)

Add a new entry to the trie (only if mutable).

Parameters:

Name Type Description Default
entry str

The string to add

required

Returns:

Type Description
bool

True if added successfully, False if already exists or trie is immutable

remove(entry)

Remove an entry from the trie (only if mutable).

Parameters:

Name Type Description Default
entry str

The string to remove

required

Returns:

Type Description
bool

True if removed successfully, False if not found or trie is immutable

is_immutable()

Check if the trie is immutable.

Returns:

Type Description
bool

True if immutable, False if mutable