Match
Match class#
Contains methods for comparing and matching strings.
Initialise the Match class with the given parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scorer |
Type[BaseScorer]
|
The scoring algorithm to use, by default LevenshteinScorer Available scorers: LevenshteinScorer, JaroScorer, JaroWinklerScorer. |
LevenshteinScorer
|
latinise |
bool
|
If special unicode characters should be removed from the strings, by default False. |
False
|
ignore_case |
bool
|
If the strings should be compared ignoring case, by default True. |
True
|
remove_punctuation |
bool
|
If punctuation should be removed from the strings, by default False. |
False
|
alphanumeric |
bool
|
If the strings should only be compared by their latin letters, by default False. |
False
|
include_partial |
bool
|
If partial substring matches should be included, by default False. |
False
|
Returns:
Type | Description |
---|---|
Match
|
The Match class. |
Examples:
Source code in stringmatch/match.py
match(string1: str, string2: str, *, score: int = 70) -> bool
#
Matches two strings, returns True if they are similar enough.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string1 |
str
|
The first string to compare. |
required |
string2 |
str
|
The second string to compare. |
required |
score |
int
|
The cutoff for the score, by default 70. |
70
|
Returns:
Type | Description |
---|---|
bool
|
If the strings are similar enough. |
Examples:
Source code in stringmatch/match.py
match_with_ratio(string1: str, string2: str, *, score: int = 70) -> Tuple[bool, int]
#
Same as match, but returns the boolean in a tuple, together with the score.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string1 |
str
|
The first string to compare. |
required |
string2 |
str
|
The second string to compare. |
required |
score |
int
|
The cutoff for the score, by default 70. |
70
|
Returns:
Type | Description |
---|---|
Tuple[bool, int]
|
If the strings are similar and their score. |
Examples:
>>> match_with_ratio("stringmatch", "strmatch")
(True, 84)
>>> match_with_ratio("stringmatch", "something different")
(False, 40)
Source code in stringmatch/match.py
get_best_match(string: str, string_list: List[str], *, score: int = 70) -> Optional[str]
#
Returns the best match from a list of strings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string |
str
|
The string to compare. |
required |
string_list |
List[str]
|
The List of strings to compare to. |
required |
score |
int
|
The cutoff for the score, by default 70. |
70
|
Returns:
Type | Description |
---|---|
Optional[str]
|
The best string found, or None if no good match was found. |
Examples:
Source code in stringmatch/match.py
get_best_match_with_ratio(string: str, string_list: List[str], *, score: int = 70) -> Optional[Tuple[str, int]]
#
Same as get_best_match, but returns a tuple with the best match and its score.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string |
str
|
The string to compare. |
required |
string_list |
List[str]
|
The List of strings to compare to. |
required |
score |
int
|
The cutoff for the score, by default 70. |
70
|
Returns:
Type | Description |
---|---|
Optional[tuple[str, int]]
|
The best string and its score found, or None if no good match was found. |
Examples:
>>> get_best_match_with_ratio("stringmatch", ["strmatch", "test", "something else"])
('strmatch', 84)
Source code in stringmatch/match.py
get_best_matches(string: str, string_list: List[str], *, score: int = 70, limit: Optional[int] = 5) -> List[str]
#
Matches a string to a list of strings, returns the strings found that are similar.
If there are more than limit
matches,
only the limit
best matches are returned, sorted by score.
If no matches are found, returns an empty list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string |
str
|
The string to compare. |
required |
string_list |
List[str]
|
The List of strings to compare to. |
required |
score |
int
|
The cutoff for the score, by default 70. |
70
|
limit |
int
|
The number of matches to return, by default 5. If you want to return every match, set this to 0 (or less than 0) or None. |
5
|
Returns:
Type | Description |
---|---|
List[str]
|
All of the matches found. |
Examples:
>>> get_best_matches("stringmatch", ["strmatch", "stringmatch", "test", "something else"])
['stringmatch', 'strmatch']
Source code in stringmatch/match.py
get_best_matches_with_ratio(string: str, string_list: List[str], *, score: int = 70, limit: Optional[int] = 5) -> List[Tuple[str, int]]
#
Same as get_best_matches, but returns a list of tuples with the best matches and their score.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string |
str
|
The string to compare. |
required |
string_list |
List[str]
|
The List of strings to compare to. |
required |
score |
int
|
The cutoff for the score, by default 70. |
70
|
limit |
int
|
The number of matches to return, by default 5. If you want to return every match, set this to 0 (or less than 0) or None. |
5
|
Returns:
Type | Description |
---|---|
List[tuple[str, int]]
|
All of the matches found. |
Examples:
>>> get_best_matches_with_ratio("stringmatch", ["strmatch", "stringmatch", "test", "something else"])
[('stringmatch', 100), ('strmatch', 84)]