Dictionary

The Dictionary provides read access to a single keyvi file.

from keyvi.dictionary import Dictionary

d = Dictionary("data.kv")
class keyvi.dictionary.Dictionary

Keyvi dictionary, an immutable container storing key value pairs, optimized for size, lookup performance and special lookup use cases. A keyvi dictionary has to be created by a previous compile run.

Keyvi dictionaries allow multiple types of approximate and completion matches due to its internal FST based data structure.

complete_fuzzy_multiword(self: keyvi._core.dictionary.Dictionary, query: str, max_edit_distance: SupportsInt | SupportsIndex, minimum_exact_prefix: SupportsInt | SupportsIndex = 0, multiword_separator: SupportsInt | SupportsIndex = 27) → collections.abc.Iterator[keyvi._core.Match]
Complete the given key to full matches after whitespace tokenizing,

allowing up to max_edit_distance distance(Levenshtein). In case the used dictionary supports inner weights, the completer traverses the dictionary according to weights, otherwise byte-order.

complete_multiword(*args, **kwargs)

Overloaded function.

  1. complete_multiword(self: keyvi._core.dictionary.Dictionary, query: str, multiword_separator: typing.SupportsInt | typing.SupportsIndex = 27) -> collections.abc.Iterator[keyvi._core.Match]

Complete the given key to full matches after whitespace tokenizing

and return the top n completions. In case the used dictionary supports inner weights, the completer traverses the dictionary according to weights, otherwise byte-order.

Note, due to depth-first traversal the traverser immediately yields results when it visits them. The results are neither in order nor limited to n. It is up to the caller to resort and truncate the lists of results. Only the number of top completions is guaranteed.

  1. complete_multiword(self: keyvi._core.dictionary.Dictionary, query: str, top_n: typing.SupportsInt | typing.SupportsIndex, multiword_separator: typing.SupportsInt | typing.SupportsIndex = 27) -> collections.abc.Iterator[keyvi._core.Match]

Complete the given key to full matches after whitespace tokenizing

and return the top n completions. In case the used dictionary supports inner weights, the completer traverses the dictionary according to weights, otherwise byte-order.

Note, due to depth-first traversal the traverser immediately yields results when it visits them. The results are neither in order nor limited to n. It is up to the caller to resort and truncate the lists of results. Only the number of top completions is guaranteed.

complete_prefix(*args, **kwargs)

Overloaded function.

  1. complete_prefix(self: keyvi._core.dictionary.Dictionary, query: str) -> collections.abc.Iterator[keyvi._core.Match]

Complete the given key to full matches after whitespace tokenizing

and return the top n completions. In case the used dictionary supports inner weights, the completer traverses the dictionary according to weights, otherwise byte-order.

Note, due to depth-first traversal the traverser immediately yields results when it visits them. The results are neither in order nor limited to n. It is up to the caller to resort and truncate the lists of results. Only the number of top completions is guaranteed.

  1. complete_prefix(self: keyvi._core.dictionary.Dictionary, query: str, top_n: typing.SupportsInt | typing.SupportsIndex) -> collections.abc.Iterator[keyvi._core.Match]

Complete the given key to full matches after whitespace tokenizing

and return the top n completions. In case the used dictionary supports inner weights, the completer traverses the dictionary according to weights, otherwise byte-order.

Note, due to depth-first traversal the traverser immediately yields results when it visits them. The results are neither in order nor limited to n. It is up to the caller to resort and truncate the lists of results. Only the number of top completions is guaranteed.

get(self: keyvi._core.dictionary.Dictionary, arg0: str) → keyvi._core.Match

Get an entry from the dictionary.

items(self: keyvi._core.dictionary.Dictionary) → keyvi._core.dictionary.DictionaryItemsIterator

Return an iterator over all (key, value) tuples in the dictionary.

keys(self: keyvi._core.dictionary.Dictionary) → keyvi._core.dictionary.DictionaryKeysIterator

Return an iterator over all keys in the dictionary.

manifest(self: keyvi._core.dictionary.Dictionary) → str

Get the manifest of the dictionary.

match(self: keyvi._core.dictionary.Dictionary, key: str) → collections.abc.Iterator[keyvi._core.Match]

Exact match for a key.

match_fuzzy(self: keyvi._core.dictionary.Dictionary, key: str, max_edit_distance: SupportsInt | SupportsIndex, minimum_exact_prefix: SupportsInt | SupportsIndex = 2) → collections.abc.Iterator[keyvi._core.Match]

Fuzzy match for a key allowing up to max_edit_distance Levenshtein distance.

match_near(self: keyvi._core.dictionary.Dictionary, key: str, minimum_prefix_length: SupportsInt | SupportsIndex, greedy: bool = False) → collections.abc.Iterator[keyvi._core.Match]

Match a key near: match as much as possible exact given the minimum prefix length and then return everything below.

If greedy is True it matches everything below the minimum_prefix_length, but in the order of exact first.

search(self: keyvi._core.dictionary.Dictionary, key: str, offset: SupportsInt | SupportsIndex = 0) → keyvi::dictionary::util::iterator_pair<keyvi::dictionary::MatchIterator>

Search for a key using leftmost longest lookup.

search_tokenized(self: keyvi._core.dictionary.Dictionary, text: str) → collections.abc.Iterator[keyvi._core.Match]

Search a text by tokenizing on whitespace and performing leftmost longest lookup for each token.

statistics(self: keyvi._core.dictionary.Dictionary) → object

Get the statistics of the dictionary as a python dict.

values(self: keyvi._core.dictionary.Dictionary) → keyvi._core.dictionary.DictionaryValuesIterator

Return an iterator over all values in the dictionary.

SecondaryKeyDictionary

class keyvi.dictionary.SecondaryKeyDictionary

Secondary Key dictionary, a specialized dictionary type that takes additional key value pairs (meta) that are matched exact upfront, enabling use cases like multi-tenancy and personalization.

All lookup methods take a meta parameter, a dict of string to string, to filter entries by secondary keys before performing the actual lookup.

complete_fuzzy_multiword(self: keyvi._core.dictionary.SecondaryKeyDictionary, query: str, meta: collections.abc.Mapping[str, str], max_edit_distance: SupportsInt | SupportsIndex, minimum_exact_prefix: SupportsInt | SupportsIndex = 0, multiword_separator: SupportsInt | SupportsIndex = 27) → collections.abc.Iterator[keyvi._core.Match]

Complete the given key to full matches within the secondary key space after whitespace tokenizing, allowing up to max_edit_distance distance (Levenshtein). In case the used dictionary supports inner weights, the completer traverses the dictionary according to weights, otherwise byte-order.

complete_multiword(*args, **kwargs)

Overloaded function.

  1. complete_multiword(self: keyvi._core.dictionary.SecondaryKeyDictionary, query: str, meta: collections.abc.Mapping[str, str], multiword_separator: typing.SupportsInt | typing.SupportsIndex = 27) -> collections.abc.Iterator[keyvi._core.Match]

    Complete the given key to full matches within the secondary key space after whitespace tokenizing. In case the used dictionary supports inner weights, the completer traverses the dictionary according to weights, otherwise byte-order.

  2. complete_multiword(self: keyvi._core.dictionary.SecondaryKeyDictionary, query: str, meta: collections.abc.Mapping[str, str], top_n: typing.SupportsInt | typing.SupportsIndex, multiword_separator: typing.SupportsInt | typing.SupportsIndex = 27) -> collections.abc.Iterator[keyvi._core.Match]

    Complete the given key to full matches within the secondary key space after whitespace tokenizing and return the top n completions. In case the used dictionary supports inner weights, the completer traverses the dictionary according to weights, otherwise byte-order.

    Note, due to depth-first traversal the traverser immediately yields results when it visits them. The results are neither in order nor limited to n. It is up to the caller to resort and truncate the lists of results. Only the number of top completions is guaranteed.

complete_prefix(*args, **kwargs)

Overloaded function.

  1. complete_prefix(self: keyvi._core.dictionary.SecondaryKeyDictionary, query: str, meta: collections.abc.Mapping[str, str]) -> collections.abc.Iterator[keyvi._core.Match]

    Complete the given key to full matches within the secondary key space (prefix matching). In case the used dictionary supports inner weights, the completer traverses the dictionary according to weights, otherwise byte-order.

  2. complete_prefix(self: keyvi._core.dictionary.SecondaryKeyDictionary, query: str, meta: collections.abc.Mapping[str, str], top_n: typing.SupportsInt | typing.SupportsIndex) -> collections.abc.Iterator[keyvi._core.Match]

    Complete the given key to full matches within the secondary key space (prefix matching) and return the top n completions. In case the used dictionary supports inner weights, the completer traverses the dictionary according to weights, otherwise byte-order.

    Note, due to depth-first traversal the traverser immediately yields results when it visits them. The results are neither in order nor limited to n. It is up to the caller to resort and truncate the lists of results. Only the number of top completions is guaranteed.

contains(self: keyvi._core.dictionary.SecondaryKeyDictionary, key: str, meta: collections.abc.Mapping[str, str]) → bool

Check if a key with the given secondary keys is in the dictionary.

get(self: keyvi._core.dictionary.SecondaryKeyDictionary, key: str, meta: collections.abc.Mapping[str, str], default: object = None) → object

Get an entry matching the key and secondary keys (meta), returning default if not found.

items(self: keyvi._core.dictionary.SecondaryKeyDictionary, meta: collections.abc.Mapping[str, str]) → keyvi._core.dictionary.DictionaryItemsIterator

Return an iterator over all (key, value) tuples matching the secondary keys.

keys(self: keyvi._core.dictionary.SecondaryKeyDictionary, meta: collections.abc.Mapping[str, str]) → keyvi._core.dictionary.DictionaryKeysIterator

Return an iterator over all keys matching the secondary keys.

manifest(self: keyvi._core.dictionary.SecondaryKeyDictionary) → str

Get the manifest of the dictionary.

match(self: keyvi._core.dictionary.SecondaryKeyDictionary, key: str, meta: collections.abc.Mapping[str, str]) → collections.abc.Iterator[keyvi._core.Match]

Exact match for a key within the secondary key space.

match_fuzzy(self: keyvi._core.dictionary.SecondaryKeyDictionary, key: str, meta: collections.abc.Mapping[str, str], max_edit_distance: SupportsInt | SupportsIndex, minimum_exact_prefix: SupportsInt | SupportsIndex = 2) → collections.abc.Iterator[keyvi._core.Match]

Fuzzy match for a key within the secondary key space, allowing up to max_edit_distance Levenshtein distance.

match_near(self: keyvi._core.dictionary.SecondaryKeyDictionary, key: str, meta: collections.abc.Mapping[str, str], minimum_prefix_length: SupportsInt | SupportsIndex, greedy: bool = False) → collections.abc.Iterator[keyvi._core.Match]

Match a key near within the secondary key space: match as much as possible exact given the minimum prefix length and then return everything below.

If greedy is True it matches everything below the minimum_prefix_length, but in the order of exact first.

statistics(self: keyvi._core.dictionary.SecondaryKeyDictionary) → object

Get the statistics of the dictionary as a python dict.

values(self: keyvi._core.dictionary.SecondaryKeyDictionary, meta: collections.abc.Mapping[str, str]) → keyvi._core.dictionary.DictionaryValuesIterator

Return an iterator over all values matching the secondary keys.