almanac.utils

Implementation of the FuzzyMatcher class.

class FuzzResult(string, ratio)

Bases: tuple

ratio

Alias for field number 1

string

Alias for field number 0

class FuzzyMatcher(reference: str, candidates: Iterable[str], ratio_threshold: float = 0.6, num_max_matches: int = 3)[source]

Bases: object

A simple fuzzy string matcher.

Parameters
  • reference – The string against which candidate matches are compared.

  • candidates – The strings to compare against reference.

  • ratio_threshold – The float within the range 0 < r < 1 that valid matches must surpass when compared to reference.

  • num_max_matches – The maximum number of matches that this class will hold.

static fuzz(reference: str, comparison: str)almanac.utils.fuzzy_matcher.FuzzResult[source]

Return the fuzz ratio of two strings. Higher means more similar.

property matches

Only the strings of matching candidates.

property reference

The reference string against which candidates are matched.

property results

The FuzzResult result of each match.

Utilities for iteration.

pairwise(i: Iterable[T]) → Iterator[Tuple[T, T]][source]

Move over an iterable, two at a time.

>>> from almanac import pairwise
>>> for pair in pairwise([1, 2, 3, 4]):
...     print(pair)
(1, 2)
(2, 3)
(3, 4)
See:

https://docs.python.org/3/library/itertools.html#itertools-recipes

String utilities.

abbreviated(text: str, len: int = 88, placeholder: str = '...')str[source]

Abbreviate the text to the specified length.

capitalized(text: str)str[source]

Capitalize the first letter of the text.