Skip to main content

Adds fuzzy matching and additional regex matching support to spaCy.

Project description

Tests Codecov PyPI Read the Docs

spaczz: Fuzzy matching and more for spaCy

Spaczz provides fuzzy matching and multi-token regex matching functionality for spaCy. Spaczz's components have similar APIs to their spaCy counterparts and spaczz pipeline components can integrate into spaCy pipelines where they can be saved/loaded as models.

Fuzzy matching is currently performed with matchers from RapidFuzz's fuzz module and regex matching currently relies on the regex library. Spaczz certainly takes additional influence from other libraries and resources. For additional details see the references section.

Spaczz has been tested on Ubuntu 18.04, MacOS 10.15, and Windows Server 2019.

v0.3.1 Release Notes:

  • spaczz now includes an experimental SimilarityMatcher that attempts to match search terms based on vector similarity. It requires a a spaCy model with word vectors (e.x. spaCy's medium and large English models) to function properly. See the documentation below for usage details.

v0.3.0 Release Notes:

  • The FuzzyMatcher and RegexMatcher now return fuzzy ratio and fuzzy count details respectively. The behavior of these two matchers is still the same except they now return lists of tuples of length 4 (match id, start, end, fuzzy details).
    • This change could be breaking in instances where these tuples are unpacked in the traditional spaCy fashion (match id, start, end). Simply include the fuzzy details or a placeholder during unpacking to fix.
  • The SpaczzRuler now writes fuzzy ratio and fuzzy count details for fuzzy/regex matches respectively as custom Span attributes. These are spaczz_ent_ratio and spaczz_ent_counts respectively. They return None by default.
    • The spaczz_ent portion of these attributes is controlled by the attr parameter and can be changed if needed. However, the _ent_ratio and _ent_counts extensions are hard-coded.
    • If, in the rare case, the same match is made via a fuzzy pattern and regex pattern, the span will have both extensions set with their repsective values.
  • Fixed a bug where the attr parameter in the SpaczzRuler did not actually change the name of the custom span attribute.

v0.2.0 Release Notes:

  • Fuzzy matching is now performed with RapidFuzz instead of FuzzyWuzzy.
    • RapidFuzz is higher performance with a more liberal license.
  • The spaczz ruler now automatically sets a custom, boolean, Span attribute on all entities it adds.
    • This is set by the attr parameter during SpaczzRuler instantiation and defaults to: "spaczz_ent".
    • For example: an entity set by the spaczz ruler will have ent._.spaczz_ent set to True.
  • Spaczz ruler patterns now support optional "id" values like spaCy's entity ruler. See this spaCy documentation for usage details.
  • Automated Windows testing is now part of the build process.

Table of Contents

Installation

Spaczz can be installed using pip.

pip install spaczz

Basic Usage

Spaczz's primary features are fuzzy and regex matchers that function similarily to spaCy's phrase matcher, and the spaczz ruler which integrates the fuzzy/regex matcher into a spaCy pipeline component similar to spaCy's entity ruler.

Fuzzy Matcher

The basic usage of the fuzzy matcher is similar to spaCy's phrase matcher except it returns the fuzzy ratio along with match id, start and end information, so make sure to include a variable for the ratio when unpacking results.

import spacy
from spaczz.matcher import FuzzyMatcher

nlp = spacy.blank("en")
text = """Grint Anderson created spaczz in his home at 555 Fake St,
Apt 5 in Nashv1le, TN 55555-1234 in the USA.""" # Spelling errors intentional.
doc = nlp(text)

matcher = FuzzyMatcher(nlp.vocab)
matcher.add("NAME", [nlp("Grant Andersen")])
matcher.add("GPE", [nlp("Nashville")])
matches = matcher(doc)

for match_id, start, end, ratio in matches:
    print(match_id, doc[start:end], ratio)
NAME Grint Anderson 86
GPE Nashv1le 82

Unlike spaCy matchers, spaczz matchers are written in pure Python. While they are required to have a spaCy vocab passed to them during initialization, this is purely for consistency as the spaczz matchers do not use currently use the spaCy vocab. This is why the match_id is simply a string in the above example instead of an integer value like in spaCy matchers.

Spaczz matchers can also make use of on match rules via callback functions. These on match callbacks need to accept the matcher itself, the doc the matcher was called on, the match index and the matches produced by the matcher.

import spacy
from spacy.tokens import Span
from spaczz.matcher import FuzzyMatcher

nlp = spacy.blank("en")
text = """Grint Anderson created spaczz in his home at 555 Fake St,
Apt 5 in Nashv1le, TN 55555-1234 in the USA.""" # Spelling errors intentional.
doc = nlp(text)

def add_name_ent(
    matcher, doc, i, matches
):
    """Callback on match function. Adds "NAME" entities to doc."""
    # Get the current match and create tuple of entity label, start and end.
    # Append entity to the doc's entity. (Don't overwrite doc.ents!)
    _match_id, start, end, _ratio = matches[i]
    entity = Span(doc, start, end, label="NAME")
    doc.ents += (entity,)

matcher = FuzzyMatcher(nlp.vocab)
matcher.add("NAME", [nlp("Grant Andersen")], on_match=add_name_ent)
matches = matcher(doc)

for ent in doc.ents:
    print((ent.text, ent.start, ent.end, ent.label_))
('Grint Anderson', 0, 2, 'NAME')

Like spaCy's EntityRuler, a very similar entity updating logic has been implemented in the SpaczzRuler. The SpaczzRuler also takes care of handling overlapping matches. It is discussed in a later section.

Unlike spaCy's matchers, rules added to spaczz matchers have optional keyword arguments that can modify the matching behavior. Take the below fuzzy matching example:

import spacy
from spaczz.matcher import FuzzyMatcher

nlp = spacy.blank("en")
# Let's modify the order of the name in the text.
text = """Anderson, Grint created spaczz in his home at 555 Fake St,
Apt 5 in Nashv1le, TN 55555-1234 in the USA.""" # Spelling errors intentional.
doc = nlp(text)

matcher = FuzzyMatcher(nlp.vocab)
matcher.add("NAME", [nlp("Grant Andersen")])
matches = matcher(doc)

# The default fuzzy matching settings will not find a match.
for match_id, start, end, ratio in matches:
    print(match_id, doc[start:end], ratio)

Next we change the fuzzy matching behavior for the "NAME" rule.

import spacy
from spaczz.matcher import FuzzyMatcher

nlp = spacy.blank("en")
# Let's modify the order of the name in the text.
text = """Anderson, Grint created spaczz in his home at 555 Fake St,
Apt 5 in Nashv1le, TN 55555-1234 in the USA.""" # Spelling errors intentional.
doc = nlp(text)

matcher = FuzzyMatcher(nlp.vocab)
matcher.add("NAME", [nlp("Grant Andersen")], kwargs=[{"fuzzy_func": "token_sort"}])
matches = matcher(doc)

# The default fuzzy matching settings will not find a match.
for match_id, start, end, ratio in matches:
    print(match_id, doc[start:end], ratio)
NAME Anderson, Grint 86

The full list of keyword arguments available for fuzzy matching rules includes:

  • fuzzy_func: Key name of fuzzy matching function to use. All rapidfuzz matching functions with default settings are available. Default is "simple". The included fuzzy matchers are:
    • "simple" = fuzz.ratio
    • "partial" = fuzz.partial_ratio
    • "token_set" = fuzz.token_set_ratio
    • "token_sort" = fuzz.token_sort_ratio
    • "partial_token_set" = fuzz.partial_token_set_ratio
    • "partial_token_sort" = fuzz.partial_token_sort_ratio
    • "quick" = fuzz.QRatio
    • "weighted" = fuzz.WRatio
    • "quick_lev" = fuzz.quick_lev_ratio
  • ignore_case: If strings should be lower-cased before fuzzy matching or not. Default is True.
  • min_r1: Minimum fuzzy match ratio required for selection during the intial search over doc. This should be lower than min_r2 and "low" in general because match span boundaries are not flexed initially. 0 means all spans of query length in doc will have their boundaries flexed and will be re-compared during match optimization. Lower min_r1 will result in more fine-grained matching but will run slower. Default is 25.
  • min_r2: Minimum fuzzy match ratio required for selection during match optimization. Should be higher than min_r1 and "high" in general to ensure only quality matches are returned. Default is 75.
  • flex: Number of tokens to move match span boundaries left and right during match optimization. Default is "default".

Regex Matcher

The basic usage of the regex matcher is also fairly similar to spaCy's phrase matcher. It accepts regex patterns as strings so flags must be inline. Regexes are compiled with the regex package so approximate fuzzy matching is supported. Due to the supported fuzzy matching the matcher returns the fuzzy count values along with match id, start and end information, so make sure to include a variable for the counts when unpacking results.

import spacy
from spaczz.matcher import RegexMatcher

nlp = spacy.blank("en")
text = """Anderson, Grint created spaczz in his home at 555 Fake St,
Apt 5 in Nashv1le, TN 55555-1234 in the USA.""" # Spelling errors intentional.
doc = nlp(text)

matcher = RegexMatcher(nlp.vocab)
# Use inline flags for regex strings as needed
matcher.add("APT", [r"""(?ix)((?:apartment|apt|building|bldg|floor|fl|suite|ste|unit
|room|rm|department|dept|row|rw)\.?\s?)#?\d{1,4}[a-z]?"""]) # Not the most robust regex.
matcher.add("GPE", [r"(?i)[U](nited|\.?) ?[S](tates|\.?)"])
matches = matcher(doc)

for match_id, start, end, counts in matches:
    print(match_id, doc[start:end], counts)
APT Apt 5 (0, 0, 0)
GPE USA (0, 0, 0)

Spaczz matchers can also make use of on match rules via callback functions. These on match callbacks need to accept the matcher itself, the doc the matcher was called on, the match index and the matches produced by the matcher. See the fuzzy matcher usage example for details.

Like the fuzzy matcher, the regex matcher has optional keyword arguments that can modify matching behavior. Take the below regex matching example.

import spacy
from spaczz.matcher import RegexMatcher

nlp = spacy.blank("en")
text = """Anderson, Grint created spaczz in his home at 555 Fake St,
Apt 5 in Nashv1le, TN 55555-1234 in the USA.""" # Spelling errors intentional.
doc = nlp(text)

matcher = RegexMatcher(nlp.vocab)
# Use inline flags for regex strings as needed
matcher.add("STREET", ["street_addresses"], kwargs=[{"predef": True}]) # Use predefined regex by key name.
# Below will not expand partial matches to span boundaries.
matcher.add("GPE", [r"(?i)[U](nited|\.?) ?[S](tates|\.?)"], kwargs=[{"partial": False}])
matches = matcher(doc)

for match_id, start, end, counts in matches:
    print(match_id, doc[start:end], counts)
STREET 555 Fake St, (0, 0, 0)

The full list of keyword arguments available for regex matching rules includes:

  • partial: Whether partial matches should be extended to existing span boundaries in doc or not, i.e. the regex only matches part of a token or span. Default is True.
  • predef: Whether the regex string should be interpreted as a key to a predefined regex pattern or not. Default is False. The included regexes are:
    • "dates"
    • "times"
    • "phones"
    • "phones_with_exts"
    • "links"
    • "emails"
    • "ips"
    • "ipv6s"
    • "prices"
    • "hex_colors"
    • "credit_cards"
    • "btc_addresses"
    • "street_addresses"
    • "zip_codes"
    • "po_boxes"
    • "ssn_number"

The above patterns are the same that the commonregex package provides.

SimilarityMatcher

The basic usage of the similarity matcher is similar to spaCy's phrase matcher except it returns the vector similarity ratio along with match id, start and end information, so make sure to include a variable for the ratio when unpacking results.

In order to produce meaningful results from the similarity matcher, a spaCy model with word vectors (e.x. medium or large English models) must be used to initialize the matcher, process the target doc, and process any patterns added.

import spacy
from spaczz.matcher import SimilarityMatcher

nlp = spacy.load("en_core_web_md")
text = "I like apples, grapes and bananas."
doc = nlp(text)

#lowering min_r2 from default of 75 to produce matches in this example
matcher = SimilarityMatcher(nlp.vocab, min_r2=65)
matcher.add("FRUIT", [nlp("fruit")])
matches = matcher(doc)

for match_id, start, end, ratio in matches:
    print(match_id, doc[start:end], ratio)
FRUIT apples 72
FRUIT grapes 72
FRUIT bananas 68

Please note that even for the mostly pure-Python spaczz, this process is currently extremely slow so be mindful of the scope in which it is applied. Enabling GPU support in spaCy (see here) should improve the speed somewhat, but I believe the process will still be bottlenecked in the pure-Python search algorithm until I develop a better search algorithm and/or drop the search to lower-level code (e.x. C).

Also as a somewhat experimental feature, the SimilarityMatcher is not currently part of the SpaczzRuler nor does it have a separate ruler. If you need to add similarity matches to a doc's entities you will need to use an on-match callback for the time being. Please see the on-match callback example in the FuzzyMatcher documentation for ideas.

The full list of keyword arguments available for similarity matching rules includes:

  • min_r1: Minimum similarity match ratio required for selection during the intial search over doc. This should be lower than min_r2 and "low" in general because match span boundaries are not flexed initially. 0 means all spans of query length in doc will have their boundaries flexed and will be re-compared during match optimization. Lower min_r1 will result in more fine-grained matching but will run slower. Default is 50.
  • min_r2: Minimum similarity match ratio required for selection during match optimization. Should be higher than min_r1 and "high" in general to ensure only quality matches are returned. Default is 75.
  • flex: Number of tokens to move match span boundaries left and right during match optimization. Default is "default".

SpaczzRuler

The spaczz ruler combines the fuzzy matcher and regex matcher into one pipeline component that can update a docs entities similar to spaCy's entity ruler.

Patterns must be added as an iterable of dictionaries in the format of {label (str), pattern(str), type(str), optional kwargs (dict), and optional id (str)}.

For example:

{"label": "ORG", "pattern": "Apple", "type": "fuzzy", "kwargs": {"ignore_case": False}, "id": "TECH"}

The spaczz ruler also writes custom Span attributes to matches it adds.

When instantiated, the spaczz ruler adds three custom span attributes: spaczz_ent, spaczz_ent_ratio, spaczz_ent_counts, which all default to None. Any span set by the spaczz ruler will have the spaczz_ent set to True. If it was a fuzzy match it's spaczz_ent_ratio value will be set and if it was a regex match it's spaczz_ent_counts value will be set. In the rare case that the same match is made via a fuzzy pattern and regex pattern, the span will have both extensions set with their repsective values.

The spaczz_ent portion of these attributes is controlled by the spaczz ruler's attr parameter and can be changed if needed. However, the _ent_ratio and _ent_counts extensions are hard-coded.

import spacy
from spaczz.pipeline import SpaczzRuler

nlp = spacy.blank("en")
text = """Anderson, Grint created spaczz in his home at 555 Fake St,
Apt 5 in Nashv1le, TN 55555-1234 in the USA.""" # Spelling errors intentional.
doc = nlp(text)

patterns = [
    {"label": "NAME", "pattern": "Grant Andersen", "type": "fuzzy", "kwargs": {"fuzzy_func": "token_sort"}},
    {"label": "STREET", "pattern": "street_addresses", "type": "regex", "kwargs": {"predef": True}},
    {"label": "GPE", "pattern": "Nashville", "type": "fuzzy"},
    {"label": "ZIP", "pattern": r"\b(?:55554){s<=1}(?:(?:[-\s])?\d{4}\b)", "type": "regex"}, # fuzzy regex
    {"label": "GPE", "pattern": "(?i)[U](nited|\.?) ?[S](tates|\.?)", "type": "regex"}
]

ruler = SpaczzRuler(nlp)
ruler.add_patterns(patterns)
doc = ruler(doc)

print("Fuzzy Matches:")
for ent in doc.ents:
    if ent._.spaczz_ent_ratio:
        print((ent.text, ent.start, ent.end, ent.label_, ent._.spaczz_ent_ratio))

print("\n", "Regex Matches:", sep="")
for ent in doc.ents:
    if ent._.spaczz_ent_counts:
        print((ent.text, ent.start, ent.end, ent.label_, ent._.spaczz_ent_counts))
Fuzzy Matches:
('Anderson, Grint', 0, 3, 'NAME', 86)
('Nashv1le', 17, 18, 'GPE', 82)

Regex Matches:
('555 Fake St,', 9, 13, 'STREET', (0, 0, 0))
('55555-1234', 20, 23, 'ZIP', (1, 0, 0))
('USA', 25, 26, 'GPE', (0, 0, 0))

Saving/Loading

The SpaczzRuler has it's own to/from disk/bytes methods and will accept cfg parameters passed to spacy.load(). It also has it's own spaCy factory entry point so spaCy is aware of the SpaczzRuler. Below is an example of saving and loading a spacy pipeline with the small English model, the EntityRuler, and the SpaczzRuler.

import spacy
from spaczz.pipeline import SpaczzRuler

nlp = spacy.load("en_core_web_sm")
text = """Anderson, Grint created spaczz in his home at 555 Fake St,
Apt 5 in Nashv1le, TN 55555-1234 in the USA.""" # Spelling errors intentional.
doc = nlp(text)

for ent in doc.ents:
    print((ent.text, ent.start, ent.end, ent.label_))
('Anderson', 0, 1, 'ORG')
('Grint', 2, 3, 'ORG')
('spaczz', 4, 5, 'GPE')
('555', 9, 10, 'CARDINAL')
('Fake St', 10, 12, 'PERSON')
('5', 15, 16, 'CARDINAL')
('TN', 19, 20, 'ORG')
('55555-1234', 20, 23, 'DATE')
('USA', 25, 26, 'GPE')

While spaCy does a decent job of identifying that named entities are present in this example, we can definitely improve the matches - particularly with the types of labels applied.

Let's add an entity ruler for some rules-based matches.

from spacy.pipeline import EntityRuler

entity_ruler = EntityRuler(nlp)
entity_ruler.add_patterns([
    {"label": "GPE", "pattern": "Nashville"},
    {"label": "GPE", "pattern": "TN"}
])

nlp.add_pipe(entity_ruler, before="ner")
doc = nlp(text)

for ent in doc.ents:
    print((ent.text, ent.start, ent.end, ent.label_))
('Anderson', 0, 1, 'ORG')
('Grint', 2, 3, 'ORG')
('spaczz', 4, 5, 'GPE')
('555', 9, 10, 'CARDINAL')
('Fake St', 10, 12, 'PERSON')
('5', 15, 16, 'CARDINAL')
('TN', 19, 20, 'GPE')
('55555-1234', 20, 23, 'DATE')
('USA', 25, 26, 'GPE')

We're making progress, but Nashville is spelled wrong in the text so the entity ruler does not find it, and we still have other entities to fix/find.

Let's add a spaczz ruler to round this pipeline out. We will also include the spaczz_ent custom attribute in the results to denote which entities were set via spaczz.

spaczz_ruler = nlp.create_pipe("spaczz_ruler") # Works due to spaCy factory entry point.
spaczz_ruler.add_patterns([
    {"label": "NAME", "pattern": "Grant Andersen", "type": "fuzzy", "kwargs": {"fuzzy_func": "token_sort"}},
    {"label": "STREET", "pattern": "street_addresses", "type": "regex", "kwargs": {"predef": True}},
    {"label": "GPE", "pattern": "Nashville", "type": "fuzzy"},
    {"label": "ZIP", "pattern": r"\b(?:55554){s<=1}(?:[-\s]\d{4})?\b", "type": "regex"}, # fuzzy regex
])
nlp.add_pipe(spaczz_ruler, before="ner")
doc = nlp(text)

for ent in doc.ents:
    print((ent.text, ent.start, ent.end, ent.label_, ent._.spaczz_ent))
('Anderson, Grint', 0, 3, 'NAME', True)
('spaczz', 4, 5, 'GPE', False)
('555 Fake St,', 9, 13, 'STREET', True)
('5', 15, 16, 'CARDINAL', False)
('Nashv1le', 17, 18, 'GPE', True)
('TN', 19, 20, 'GPE', False)
('55555-1234', 20, 23, 'ZIP', True)
('USA', 25, 26, 'GPE', False)

Awesome! The small English model still makes a couple named entity recognition mistakes, but we're satisfied overall.

Let's save this pipeline to disk and make sure we can load it back correctly.

nlp.to_disk("./example")
nlp = spacy.load("./example")
nlp.pipe_names
['tagger', 'parser', 'entity_ruler', 'spaczz_ruler', 'ner']

We can even ensure all the spaczz ruler patterns are still present.

spaczz_ruler = nlp.get_pipe("spaczz_ruler")
spaczz_ruler.patterns
[{'label': 'NAME',
  'pattern': 'Grant Andersen',
  'type': 'fuzzy',
  'kwargs': {'fuzzy_func': 'token_sort'}},
 {'label': 'GPE', 'pattern': 'Nashville', 'type': 'fuzzy'},
 {'label': 'STREET',
  'pattern': 'street_addresses',
  'type': 'regex',
  'kwargs': {'predef': True}},
 {'label': 'ZIP',
  'pattern': '\\b(?:55554){s<=1}(?:[-\\s]\\d{4})?\\b',
  'type': 'regex'}]

Limitations

Spaczz is written in pure Python and it's matchers do not currently utilize spaCy language vocabularies, which means following it's logic should be easy to those familiar with Python. However, this means spaczz components will run slower and likely consume more memory than their spaCy counterparts, especially as more patterns are added and documents get longer. It is therefore recommended to use spaCy components like the EntityRuler for entities with little uncertainty, like consistent spelling errors. Use spaczz components when there are not viable spaCy alternatives.

Future State

  1. API support for adding user-defined regexes to the predefined regex.
    1. Saving these additional predefined regexes as part of the SpaczzRuler will also be supported.
  2. Entity start/end trimming on the token level to prevent fuzzy matches from starting/ending with unwanted tokens, i.e. spaces/punctuation. Will support similar options as spaCy's matcher.

Wishful thinking:

  1. Having the fuzzy/regex matchers utilize spaCy vocabularies.
  2. Rewrite the fuzzy searching algorithm in Cython to utilize C speed.
  3. Fuzzy/regex matching with token patterns along with phrase patterns.

Development

Pull requests and contributors are welcome.

spaczz is linted with Flake8, formatted with Black, type-checked with MyPy (although this could benefit from improved specificity), tested with Pytest, automated with Nox, and built/packaged with Poetry. There are a few other development tools detailed in the noxfile.py, along with Git pre-commit hooks.

To contribute to spaczz's development, fork the repository then install spaczz and it's dev dependencies with Poetry. If you're interested in being a regular contributor please contact me directly.

poetry install # Within spaczz's root directory.

The only package that will not be installed via Poetry but is used for testing and in doc examples is the spaCy medium English model (en-core-web-md). This will need to be installed separately. The command below should do the trick:

poetry run python -m spacy download("en_core_web_md")

References

  • Spaczz tries to stay as close to spaCy's API as possible. Whenever it made sense to use existing spaCy code within spaczz this was done.
  • Fuzzy matching is performed using RapidFuzz.
  • Regexes are performed using the regex library.
  • The search algorithm for fuzzy matching was heavily influnced by Stack Overflow user Ulf Aslak's answer in this thread.
  • Spaczz's predefined regex patterns were borrowed from the commonregex package.
  • Spaczz's development and CI/CD patterns were inspired by Claudio Jolowicz's Hypermodern Python article series.

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

spaczz-0.3.1.tar.gz (40.3 kB view hashes)

Uploaded Source

Built Distribution

spaczz-0.3.1-py3-none-any.whl (42.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page