notebooks
Folders and files
Name | Name | Last commit date | ||
---|---|---|---|---|
parent directory.. | ||||
{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "[![Tests](https://github.com/gandersen101/spaczz/workflows/Lint,%20Typecheck%20and%20Test/badge.svg)](https://github.com/gandersen101/spaczz/actions?workflow=Lint,%20Typecheck%20and%20Test)\n", "[![Codecov](https://codecov.io/gh/gandersen101/spaczz/branch/master/graph/badge.svg)](https://codecov.io/gh/gandersen101/spaczz)\n", "[![PyPI](https://img.shields.io/pypi/v/spaczz.svg)](https://pypi.org/project/spaczz/)\n", "[![Read the Docs](https://readthedocs.org/projects/spaczz/badge/)](https://spaczz.readthedocs.io/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# spaczz: Fuzzy matching and more for spaCy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "spaczz provides fuzzy matching and additional regex matching functionality for [spaCy](https://spacy.io/).\n", "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.\n", "\n", "Fuzzy matching is currently performed with matchers from [RapidFuzz](https://github.com/maxbachmann/rapidfuzz)'s fuzz module and regex matching currently relies on the [regex](https://pypi.org/project/regex/) library. spaczz certainly takes additional influence from other libraries and resources. For additional details see the references section.\n", "\n", "**Supports spaCy >= 3.0**\n", "\n", "spaczz has been tested on Ubuntu, MacOS, and Windows Server." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "*v0.6.0 Release Notes:*\n", "- *Returning the matching pattern for all matchers, this is a breaking change as matches are now tuples of length 5 instead of 4.*\n", "- *Regex and token matches now return match ratios.*\n", "- *Support for `python<=3.11,>=3.7`, along with `rapidfuzz>=1.0.0`.*\n", "- *Dropped support for spaCy v2. Sorry to do this without a deprecation cycle, but I stepped away from this project for a long time.*\n", "- *Removed support of `\"spaczz_\"` preprended optional `SpaczzRuler` init arguments. Also, sorry to do this without a deprecation cycle.*\n", "- *`Matcher.pipe` methods, which were deprecated, are now removed.*\n", "- *`spaczz_span` custom attribute, which was deprecated, is now removed.*\n", "\n", "Please see the [changelog](https://github.com/gandersen101/spaczz/blob/master/CHANGELOG.md) for previous release notes. This will eventually be moved to the [Read the Docs](https://spaczz.readthedocs.io/en/latest/) page." ] }, { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n", "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Installation\" data-toc-modified-id=\"Installation-1\">Installation</a></span></li><li><span><a href=\"#Basic-Usage\" data-toc-modified-id=\"Basic-Usage-2\">Basic Usage</a></span><ul class=\"toc-item\"><li><span><a href=\"#FuzzyMatcher\" data-toc-modified-id=\"FuzzyMatcher-2.1\">FuzzyMatcher</a></span></li><li><span><a href=\"#RegexMatcher\" data-toc-modified-id=\"RegexMatcher-2.2\">RegexMatcher</a></span></li><li><span><a href=\"#SimilarityMatcher\" data-toc-modified-id=\"SimilarityMatcher-2.3\">SimilarityMatcher</a></span></li><li><span><a href=\"#TokenMatcher\" data-toc-modified-id=\"TokenMatcher-2.4\">TokenMatcher</a></span></li><li><span><a href=\"#SpaczzRuler\" data-toc-modified-id=\"SpaczzRuler-2.5\">SpaczzRuler</a></span></li><li><span><a href=\"#Custom-Attributes\" data-toc-modified-id=\"Custom-Attributes-2.6\">Custom Attributes</a></span></li><li><span><a href=\"#Saving/Loading\" data-toc-modified-id=\"Saving/Loading-2.7\">Saving/Loading</a></span></li></ul></li><li><span><a href=\"#Known-Issues\" data-toc-modified-id=\"Known-Issues-3\">Known Issues</a></span><ul class=\"toc-item\"><li><span><a href=\"#Performance\" data-toc-modified-id=\"Performance-3.1\">Performance</a></span></li></ul></li><li><span><a href=\"#Roadmap\" data-toc-modified-id=\"Roadmap-4\">Roadmap</a></span></li><li><span><a href=\"#Development\" data-toc-modified-id=\"Development-5\">Development</a></span></li><li><span><a href=\"#References\" data-toc-modified-id=\"References-6\">References</a></span></li></ul></div>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Spaczz can be installed using pip." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "pip install spaczz\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Usage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Spaczz's primary features are the `FuzzyMatcher`, `RegexMatcher`, and \"fuzzy\" `TokenMatcher` that function similarly to spaCy's `Matcher` and `PhraseMatcher`, and the `SpaczzRuler` which integrates the spaczz matchers into a spaCy pipeline component similar to spaCy's `EntityRuler`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### FuzzyMatcher" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The basic usage of the fuzzy matcher is similar to spaCy's `PhraseMatcher` except it returns the fuzzy ratio and matched pattern, along with match id, start and end information, so make sure to include variables for the ratio and pattern when unpacking results." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-05-01T02:22:21.849610Z", "start_time": "2023-05-01T02:22:20.992545Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NAME Grint M Anderson 80 Grant Andersen\n", "GPE Nashv1le 82 Nashville\n" ] } ], "source": [ "import spacy\n", "from spaczz.matcher import FuzzyMatcher\n", "\n", "nlp = spacy.blank(\"en\")\n", "text = \"\"\"Grint M Anderson created spaczz in his home at 555 Fake St, \n", "Apt 5 in Nashv1le, TN 55555-1234 in the US.\"\"\" # Spelling errors intentional.\n", "doc = nlp(text)\n", "\n", "matcher = FuzzyMatcher(nlp.vocab)\n", "matcher.add(\"NAME\", [nlp(\"Grant Andersen\")])\n", "matcher.add(\"GPE\", [nlp(\"Nashville\")])\n", "matches = matcher(doc)\n", "\n", "for match_id, start, end, ratio, pattern in matches:\n", " print(match_id, doc[start:end], ratio, pattern)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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` above is simply a string instead of an integer value like in spaCy matchers." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-05-01T02:22:23.380118Z", "start_time": "2023-05-01T02:22:23.221137Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Grint M Anderson', 0, 3, 'NAME')\n" ] } ], "source": [ "import spacy\n", "from spacy.tokens import Span\n", "from spaczz.matcher import FuzzyMatcher\n", "\n", "nlp = spacy.blank(\"en\")\n", "text = \"\"\"Grint M Anderson created spaczz in his home at 555 Fake St, \n", "Apt 5 in Nashv1le, TN 55555-1234 in the US.\"\"\" # Spelling errors intentional.\n", "doc = nlp(text)\n", "\n", "\n", "def add_name_ent(matcher, doc, i, matches):\n", " \"\"\"Callback on match function. Adds \"NAME\" entities to doc.\"\"\"\n", " # Get the current match and create tuple of entity label, start and end.\n", " # Append entity to the doc's entity. (Don't overwrite doc.ents!)\n", " _match_id, start, end, _ratio, _pattern = matches[i]\n", " entity = Span(doc, start, end, label=\"NAME\")\n", " doc.ents += (entity,)\n", "\n", "\n", "matcher = FuzzyMatcher(nlp.vocab)\n", "matcher.add(\"NAME\", [nlp(\"Grant Andersen\")], on_match=add_name_ent)\n", "matches = matcher(doc)\n", "\n", "for ent in doc.ents:\n", " print((ent.text, ent.start, ent.end, ent.label_))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unlike spaCy's matchers, rules added to spaczz matchers have optional keyword arguments that can modify the matching behavior. Take the below fuzzy matching examples:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-05-01T02:22:56.790082Z", "start_time": "2023-05-01T02:22:56.635154Z" } }, "outputs": [], "source": [ "import spacy\n", "from spaczz.matcher import FuzzyMatcher\n", "\n", "nlp = spacy.blank(\"en\")\n", "# Let's modify the order of the name in the text.\n", "text = \"\"\"Anderson, Grint created spaczz in his home at 555 Fake St, \n", "Apt 5 in Nashv1le, TN 55555-1234 in the US.\"\"\" # Spelling errors intentional.\n", "doc = nlp(text)\n", "\n", "matcher = FuzzyMatcher(nlp.vocab)\n", "matcher.add(\"NAME\", [nlp(\"Grant Andersen\")])\n", "matches = matcher(doc)\n", "\n", "# The default fuzzy matching settings will not find a match.\n", "for match_id, start, end, ratio, pattern in matches:\n", " print(match_id, doc[start:end], ratio, pattern)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we change the fuzzy matching behavior for the pattern in the \"NAME\" rule." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2023-05-01T02:23:01.405062Z", "start_time": "2023-05-01T02:23:01.252148Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NAME Anderson, Grint 83 Grant Andersen\n" ] } ], "source": [ "import spacy\n", "from spaczz.matcher import FuzzyMatcher\n", "\n", "nlp = spacy.blank(\"en\")\n", "# Let's modify the order of the name in the text.\n", "text = \"\"\"Anderson, Grint created spaczz in his home at 555 Fake St, \n", "Apt 5 in Nashv1le, TN 55555-1234 in the US.\"\"\" # Spelling errors intentional.\n", "doc = nlp(text)\n", "\n", "matcher = FuzzyMatcher(nlp.vocab)\n", "matcher.add(\"NAME\", [nlp(\"Grant Andersen\")], kwargs=[{\"fuzzy_func\": \"token_sort\"}])\n", "matches = matcher(doc)\n", "\n", "# The default fuzzy matching settings will not find a match.\n", "for match_id, start, end, ratio, pattern in matches:\n", " print(match_id, doc[start:end], ratio, pattern)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The full list of keyword arguments available for fuzzy matching settings includes:\n", "\n", "- `ignore_case` (bool): Whether to lower-case text before matching. Default is `True`.\n", "- `min_r` (int): Minimum match ratio required.\n", "- `thresh` (int): If this ratio is exceeded in initial scan, and `flex > 0`, no optimization will be attempted. If `flex == 0`, `thresh` has no effect. Default is `100`.\n", "- `fuzzy_func` (str): Key name of fuzzy matching function to use. All rapidfuzz matching functions with default settings are available. Additional fuzzy matching functions can be registered by users. Default is `\"simple\"`:\n", " * `\"simple\"` = `ratio`\n", " * `\"partial\"` = `partial_ratio`\n", " * `\"token\"` = `token_ratio`\n", " * `\"token_set\"` = `token_set_ratio`\n", " * `\"token_sort\"` = `token_sort_ratio`\n", " * `\"partial_token\"` = `partial_token_ratio`\n", " * `\"partial_token_set\"` = `partial_token_set_ratio`\n", " * `\"partial_token_sort\"` = `partial_token_sort_ratio`\n", " * `\"weighted\"` = `WRatio`\n", " * `\"quick\"` = `QRatio`\n", " * `\"partial_alignment\"` = `partial_ratio_alignment` (Requires `rapidfuzz>=2.0.3`)\n", "- `flex` (int|Literal['default', 'min', 'max']): Number of tokens to move match boundaries left and right during optimization. Can be an `int` with a max of `len(pattern)` and a min of `0`, (will warn and change if higher or lower). `\"max\"`, `\"min\"`, or `\"default\"` are also valid. Default is `\"default\"`: `len(pattern) // 2`.\n", "- `min_r1` (int|None): Optional granular control over the minimum match ratio required for selection during the initial scan. If `flex == 0`, `min_r1` will be overwritten by `min_r2`. If `flex > 0`, `min_r1` must be lower than `min_r2` and \"low\" in general because match boundaries are not flexed initially. Default is `None`, which will result in `min_r1` being set to `round(min_r / 1.5)`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### RegexMatcher" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The basic usage of the regex matcher is also fairly similar to spaCy's `PhraseMatcher`. It accepts regex patterns as strings so flags must be inline. Regexes are compiled with the [regex](https://pypi.org/project/regex/) package so approximate \"fuzzy\" matching is supported. To provide access to these \"fuzzy\" match results the matcher returns a calculated fuzzy ratio and matched pattern, along with match id, start and end information, so make sure to include variables for the ratio and pattern when unpacking results." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2023-05-01T02:28:59.043233Z", "start_time": "2023-05-01T02:28:58.887314Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ZIP 55555-1234 100 \\b\\d{5}(?:[-\\s]\\d{4})?\\b\n", "GPE US 80 (usa){d<=1}\n" ] } ], "source": [ "import spacy\n", "from spaczz.matcher import RegexMatcher\n", "\n", "nlp = spacy.blank(\"en\")\n", "text = \"\"\"Anderson, Grint created spaczz in his home at 555 Fake St,\n", "Apt 5 in Nashv1le, TN 55555-1234 in the US.\"\"\" # Spelling errors intentional.\n", "doc = nlp(text)\n", "\n", "matcher = RegexMatcher(nlp.vocab)\n", "# Use inline flags for regex strings as needed\n", "matcher.add(\n", " \"ZIP\",\n", " [r\"\\b\\d{5}(?:[-\\s]\\d{4})?\\b\"],\n", ")\n", "matcher.add(\"GPE\", [r\"(usa){d<=1}\"]) # Fuzzy regex.\n", "matches = matcher(doc)\n", "\n", "for match_id, start, end, ratio, pattern in matches:\n", " print(match_id, doc[start:end], ratio, pattern)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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 above for details." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like the fuzzy matcher, the regex matcher has optional keyword arguments that can modify matching behavior. Take the below regex matching example." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2023-05-01T02:29:11.041786Z", "start_time": "2023-05-01T02:29:10.882058Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "STREET 555 Fake St, 100 street_addresses\n" ] } ], "source": [ "import spacy\n", "from spaczz.matcher import RegexMatcher\n", "\n", "nlp = spacy.blank(\"en\")\n", "text = \"\"\"Anderson, Grint created spaczz in his home at 555 Fake St,\n", "Apt 5 in Nashv1le, TN 55555-1234 in the USA.\"\"\" # Spelling errors intentional. Notice 'USA' here.\n", "doc = nlp(text)\n", "\n", "matcher = RegexMatcher(nlp.vocab)\n", "# Use inline flags for regex strings as needed\n", "matcher.add(\n", " \"STREET\", [\"street_addresses\"], kwargs=[{\"predef\": True}]\n", ") # Use predefined regex by key name.\n", "# Below will not expand partial matches to span boundaries.\n", "matcher.add(\"GPE\", [r\"(?i)[U](nited|\\.?) ?[S](tates|\\.?)\"], kwargs=[{\"partial\": False}])\n", "matches = matcher(doc)\n", "\n", "for match_id, start, end, ratio, pattern in matches:\n", " print(\n", " match_id, doc[start:end], ratio, pattern\n", " ) # comma in result isn't ideal - see \"Roadmap\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The full list of keyword arguments available for regex matching settings includes:\n", "\n", "- `ignore_case` (bool): Whether to lower-case text before matching. Default is `True`.\n", "- `min_r` (int): Minimum match ratio required.\n", "- `fuzzy_weights` (str): Name of weighting method for regex insertion, deletion, and substituion counts. Additional weighting methods can be registered by users. Default is `\"indel\"`.\n", " * `\"indel\"` = `(1, 1, 2)`\n", " * `\"lev\"` = `(1, 1, 1)`\n", "- `partial`: (bool): Whether partial matches should be extended to `Token` or `Span` boundaries in `doc` or not. For example, the regex only matches part of a `Token` or `Span` in `doc`. Default is `True`.\n", "- `predef` (string): Whether the regex string should be interpreted as a key to a predefined regex pattern or not. Additional predefined regex patterns can be registered by users. Default is `False.`\n", " * `\"dates\"`\n", " * `\"times\"`\n", " * `\"phones\"`\n", " * `\"phones_with_exts\"`\n", " * `\"links\"`\n", " * `\"emails\"`\n", " * `\"ips\"`\n", " * `\"ipv6s\"`\n", " * `\"prices\"`\n", " * `\"hex_colors\"`\n", " * `\"credit_cards\"`\n", " * `\"btc_addresses\"`\n", " * `\"street_addresses\"`\n", " * `\"zip_codes\"`\n", " * `\"po_boxes\"`\n", " * `\"ssn_numbers\"`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### SimilarityMatcher" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The basic usage of the similarity matcher is similar to spaCy's `PhraseMatcher` except it returns the vector similarity ratio and matched pattern, along with match id, start and end information, so make sure to include variables for the ratio and pattern when unpacking results.\n", "\n", "In order to produce meaningful results from the similarity matcher, a spaCy model with word vectors (ex. medium or large English models) must be used to initialize the matcher, process the target document, and process any patterns added." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2023-04-30T19:37:01.431613Z", "start_time": "2023-04-30T19:37:00.122443Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "FRUIT apples 70 fruit\n", "FRUIT grapes 73 fruit\n", "FRUIT bananas 70 fruit\n" ] } ], "source": [ "import spacy\n", "from spaczz.matcher import SimilarityMatcher\n", "\n", "nlp = spacy.load(\"en_core_web_md\")\n", "text = \"I like apples, grapes and bananas.\"\n", "doc = nlp(text)\n", "\n", "# lowering min_r2 from default of 75 to produce matches in this example\n", "matcher = SimilarityMatcher(nlp.vocab, min_r2=65)\n", "matcher.add(\"FRUIT\", [nlp(\"fruit\")])\n", "matches = matcher(doc)\n", "\n", "for match_id, start, end, ratio, pattern in matches:\n", " print(match_id, doc[start:end], ratio, pattern)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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](https://spacy.io/usage#gpu)) 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 (ex C)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Also as a somewhat experimental feature, the similarity matcher 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 fuzzy matcher on-match callback example above for ideas. If there is enough interest in integrating/creating a ruler for the similarity matcher this can be done." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The full list of keyword arguments available for similarity matching settings includes:\n", "\n", "- `ignore_case` (bool): Whether to lower-case text before fuzzy matching. Default is `True`.\n", "- `min_r` (int): Minimum match ratio required.\n", "- `thresh` (int): If this ratio is exceeded in initial scan, and `flex > 0`, no optimization will be attempted. If `flex == 0`, `thresh` has no effect. Default is `100`.\n", "- `flex` (int|Literal['default', 'min', 'max']): Number of tokens to move match boundaries left and right during optimization. Can be an `int` with a max of `len(pattern)` and a min of `0`, (will warn and change if higher or lower). `\"max\"`, `\"min\"`, or `\"default\"` are also valid. Default is `\"default\"`: `len(pattern) // 2`.\n", "- `min_r1` (int|None): Optional granular control over the minimum match ratio required for selection during the initial scan. If `flex == 0`, `min_r1` will be overwritten by `min_r2`. If `flex > 0`, `min_r1` must be lower than `min_r2` and \"low\" in general because match boundaries are not flexed initially. Default is `None`, which will result in `min_r1` being set to `round(min_r / 1.5)`.\n", "- `min_r2` (int|None): Optional granular control over the minimum match ratio required for selection during match optimization. Needs to be higher than `min_r1` and \"high\" in general to ensure only quality matches are returned. Default is `None`, which will result in `min_r2` being set to `min_r`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### TokenMatcher" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Note: spaCy's `Matcher` now supports [fuzzy matching](https://spacy.io/usage/v3-5#fuzzy), so unless you need a specific feature from spaczz's `TokenMatcher`, it is highly recommended to use spaCy's much faster `Matcher`.*\n", "\n", "The basic usage of the token matcher is similar to spaCy's `Matcher`. It accepts labeled patterns in the form of lists of dictionaries where each list describes an individual pattern and each dictionary describes an individual token.\n", "\n", "The token matcher accepts all the same token attributes and pattern syntax as it's spaCy counterpart but adds fuzzy and fuzzy-regex support.\n", "\n", "`\"FUZZY\"` and `\"FREGEX\"` are the two additional spaCy token pattern options.\n", "\n", "For example:\n", "\n", "```python\n", "[\n", " {\"TEXT\": {\"FREGEX\": \"(database){e<=1}\"}},\n", " {\"LOWER\": {\"FUZZY\": \"access\", \"MIN_R\": 85, \"FUZZY_FUNC\": \"quick_lev\"}},\n", "]\n", "```\n", "\n", "**Make sure to use uppercase dictionary keys in patterns.**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The full list of keyword arguments available for token matching settings includes:\n", "\n", "- `ignore_case` (bool): Whether to lower-case text before matching. Can only be set at the pattern level. For \"FUZZY\" and \"FREGEX\" patterns. Default is `True`.\n", "- `min_r` (int): Minimum match ratio required. For \"FUZZY\" and \"FREGEX\" patterns.\n", "- `fuzzy_func` (str): Key name of fuzzy matching function to use. Can only be set at the pattern level. For \"FUZZY\" patterns only. All rapidfuzz matching functions with default settings are available, however any token-based functions provide no utility at the individual token level. Additional fuzzy matching functions can be registered by users. Included, and useful, functions are (the default is `simple`):\n", " * `\"simple\"` = `ratio`\n", " * `\"partial\"` = `partial_ratio`\n", " * `\"quick\"` = `QRatio`\n", " * `\"partial_alignment\"` = `partial_ratio_alignment` (Requires `rapidfuzz>=2.0.3`)\n", "- `fuzzy_weights` (str): Name of weighting method for regex insertion, deletion, and substituion counts. Additional weighting methods can be registered by users. Default is `\"indel\"`.\n", " * `\"indel\"` = `(1, 1, 2)`\n", " * `\"lev\"` = `(1, 1, 1)`\n", "- `predef`: Whether regex should be interpreted as a key to a predefined regex pattern or not. Can only be set at the pattern level. For \"FREGEX\" patterns only. Default is `False`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2023-05-01T02:47:04.604236Z", "start_time": "2023-05-01T02:47:03.293570Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DATA SQL databesE acess 91 [{\"TEXT\":\"SQL\"},{\"LOWER\":{\"FREGEX\":\"(database){s<=1}\"}},{\"LOWER\":{\"FUZZY\":\"access\"}}]\n", "DATA Sequal DB 87 [{\"TEXT\":{\"FUZZY\":\"Sequel\"},\"POS\":\"PROPN\"},{\"LOWER\":\"db\"}]\n", "NAME Grfield 93 [{\"TEXT\":{\"FUZZY\":\"Garfield\"}}]\n" ] } ], "source": [ "import spacy\n", "from spaczz.matcher import TokenMatcher\n", "\n", "# Using model results like POS tagging in token patterns requires model that provides these.\n", "nlp = spacy.load(\"en_core_web_md\")\n", "text = \"\"\"The manager gave me SQL databesE acess so now I can acces the Sequal DB. \n", "My manager's name is Grfield\"\"\"\n", "doc = nlp(text)\n", "\n", "matcher = TokenMatcher(vocab=nlp.vocab)\n", "matcher.add(\n", " \"DATA\",\n", " [\n", " [\n", " {\"TEXT\": \"SQL\"},\n", " {\"LOWER\": {\"FREGEX\": \"(database){s<=1}\"}},\n", " {\"LOWER\": {\"FUZZY\": \"access\"}},\n", " ],\n", " [{\"TEXT\": {\"FUZZY\": \"Sequel\"}, \"POS\": \"PROPN\"}, {\"LOWER\": \"db\"}],\n", " ],\n", ")\n", "matcher.add(\"NAME\", [[{\"TEXT\": {\"FUZZY\": \"Garfield\"}}]])\n", "matches = matcher(doc)\n", "\n", "for match_id, start, end, ratio, pattern in matches:\n", " print(match_id, doc[start:end], ratio, pattern)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Even though the token matcher can be a drop-in replacement for spaCy's `Matcher`, it is still recommended to use spaCy's `Matcher` if you do not need the spaczz token matcher's fuzzy capabilities - it will slow processing down unnecessarily.\n", "\n", "*Reminder: spaCy's `Matcher` now supports [fuzzy matching](https://spacy.io/usage/v3-5#fuzzy), so unless you need a specific feature from spaczz's `TokenMatcher`, it is highly recommended to use spaCy's much faster `Matcher`.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### SpaczzRuler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The spaczz ruler combines the fuzzy and regex phrase matchers, and the \"fuzzy\" token matcher, into one pipeline component that can update a `Doc.ents` similar to spaCy's `EntityRuler`.\n", "\n", "Patterns must be added as an iterable of dictionaries in the format of *{label (str), pattern(str or list), type(str), optional kwargs (dict), and optional id (str)}*.\n", "\n", "For example, a fuzzy phrase pattern:\n", "\n", "`{'label': 'ORG', 'pattern': 'Apple' 'kwargs': {'min_r2': 90}, 'type': 'fuzzy'}`\n", "\n", "Or, a token pattern:\n", "\n", "`{'label': 'ORG', 'pattern': [{'TEXT': {'FUZZY': 'Apple'}}], 'type': 'token'}`" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2023-05-01T02:50:28.010755Z", "start_time": "2023-05-01T02:50:27.842256Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Anderson, Grint', 0, 3, 'NAME', 83, 'fuzzy', 'Grant Andersen')\n", "('555 Fake St,', 9, 13, 'STREET', 100, 'regex', 'street_addresses')\n", "('Nashv1le', 17, 18, 'GPE', 82, 'fuzzy', 'Nashville')\n", "('55555-1234', 20, 23, 'ZIP', 90, 'regex', '\\\\b(?:55554){s<=1}(?:(?:[-\\\\s])?\\\\d{4}\\\\b)')\n", "('USA', 25, 26, 'GPE', 100, 'regex', '(?i)[U](nited|\\\\.?) ?[S](tates|\\\\.?)')\n", "('Converg', 34, 35, 'BAND', 93, 'token', '[{\"LOWER\":{\"FREGEX\":\"(converge){e<=1}\"}}]')\n", "('Protet the Zero', 36, 39, 'BAND', 89, 'token', '[{\"TEXT\":{\"FUZZY\":\"Protest\"}},{\"IS_STOP\":true},{\"TEXT\":{\"FUZZY\":\"Hero\"}}]')\n" ] } ], "source": [ "import spacy\n", "from spaczz.pipeline import SpaczzRuler\n", "\n", "nlp = spacy.blank(\"en\")\n", "text = \"\"\"Anderson, Grint created spaczz in his home at 555 Fake St,\n", "Apt 5 in Nashv1le, TN 55555-1234 in the USA. \n", "Some of his favorite bands are Converg and Protet the Zero.\"\"\" # Spelling errors intentional.\n", "doc = nlp(text)\n", "\n", "patterns = [\n", " {\n", " \"label\": \"NAME\",\n", " \"pattern\": \"Grant Andersen\",\n", " \"type\": \"fuzzy\",\n", " \"kwargs\": {\"fuzzy_func\": \"token_sort\"},\n", " },\n", " {\n", " \"label\": \"STREET\",\n", " \"pattern\": \"street_addresses\",\n", " \"type\": \"regex\",\n", " \"kwargs\": {\"predef\": True},\n", " },\n", " {\"label\": \"GPE\", \"pattern\": \"Nashville\", \"type\": \"fuzzy\"},\n", " {\n", " \"label\": \"ZIP\",\n", " \"pattern\": r\"\\b(?:55554){s<=1}(?:(?:[-\\s])?\\d{4}\\b)\",\n", " \"type\": \"regex\",\n", " }, # fuzzy regex\n", " {\"label\": \"GPE\", \"pattern\": \"(?i)[U](nited|\\.?) ?[S](tates|\\.?)\", \"type\": \"regex\"},\n", " {\n", " \"label\": \"BAND\",\n", " \"pattern\": [{\"LOWER\": {\"FREGEX\": \"(converge){e<=1}\"}}],\n", " \"type\": \"token\",\n", " },\n", " {\n", " \"label\": \"BAND\",\n", " \"pattern\": [\n", " {\"TEXT\": {\"FUZZY\": \"Protest\"}},\n", " {\"IS_STOP\": True},\n", " {\"TEXT\": {\"FUZZY\": \"Hero\"}},\n", " ],\n", " \"type\": \"token\",\n", " },\n", "]\n", "\n", "ruler = SpaczzRuler(nlp)\n", "ruler.add_patterns(patterns)\n", "doc = ruler(doc)\n", "\n", "\n", "for ent in doc.ents:\n", " print(\n", " (\n", " ent.text,\n", " ent.start,\n", " ent.end,\n", " ent.label_,\n", " ent._.spaczz_ratio,\n", " ent._.spaczz_type,\n", " ent._.spaczz_pattern,\n", " )\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see in the example above that we are referencing some custom attributes, which are explained below." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more `SpaczzRuler` examples see [here](https://github.com/gandersen101/spaczz/blob/master/examples/fuzzy_matching_tweaks.ipynb). In particular this provides details about the ruler's sorting process and fuzzy matching parameters." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Custom Attributes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Spaczz initializes some custom attributes upon importing. These are under spaCy's `._.` attribute and are further prepended with `spaczz_` so there should be not conflicts with your own custom attributes. If there are spaczz will force overwrite them.\n", "\n", "These custom attributes are only set via the spaczz ruler at the token level. Span and doc versions of these attributes are getters that reference the token level attributes.\n", "\n", "The following `Token` attributes are available. All are mutable:\n", "\n", "- `spaczz_token`: default = `False`. Boolean that denotes if the token is part of an entity set by the spaczz ruler.\n", "- `spaczz_type`: default = `None`. String that shows which matcher produced an entity using the token.\n", "- `spaczz_ratio`: default = `None`. If the token is part of a matched entity, it will return fuzzy ratio.\n", "- `spaczz_pattern`: default = `None`. If the token is part of a matched entity, it will return the pattern as a string (JSON-formatted for token patterns) that produced the match.\n", "\n", "The following `Span` attributes reference the token attributes included in the span. All are immutable:\n", "\n", "- `spaczz_ent`: default = `False`. Boolean that denotes if all tokens in the span are part of an entity set by the spaczz ruler.\n", "- `spaczz_type`: default = `None`. String that denotes which matcher produced an entity using the included tokens.\n", "- `spaczz_types`: default = `set()`. Set that shows which matchers produced entities using the included tokens. An entity span should only have one type, but this allows you to see the types included in any arbitrary span.\n", "- `spaczz_ratio`: default = `None`. If all the tokens in span are part of a matched entity, it will return the fuzzy ratio.\n", "- `spaczz_pattern`: default = `None`. If all the tokens in a span are part of a matched entity, it will return the pattern as a string (JSON-formatted for token patterns) that produced the match.\n", "\n", "The following `Doc` attributes reference the token attributes included in the doc. All are immutable:\n", "\n", "- `spaczz_doc`: default = `False`. Boolean that denotes if any tokens in the doc are part of an entity set by the spaczz ruler.\n", "- `spaczz_types`: default = `set()`. Set that shows which matchers produced entities in the doc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Saving/Loading" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `SpaczzRuler` has it's own to/from disk/bytes methods and will accept `config` 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`." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2023-04-30T19:40:14.104842Z", "start_time": "2023-04-30T19:40:12.711476Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Anderson, Grint', 0, 3, 'ORG')\n", "('555', 9, 10, 'CARDINAL')\n", "('Apt 5', 14, 16, 'PRODUCT')\n", "('Nashv1le', 17, 18, 'GPE')\n", "('TN 55555-1234', 19, 23, 'ORG')\n", "('USA', 25, 26, 'GPE')\n", "('Converg', 34, 35, 'PERSON')\n", "('Zero', 38, 39, 'CARDINAL')\n" ] } ], "source": [ "import spacy\n", "from spaczz.pipeline import SpaczzRuler\n", "\n", "nlp = spacy.load(\"en_core_web_md\")\n", "text = \"\"\"Anderson, Grint created spaczz in his home at 555 Fake St,\n", "Apt 5 in Nashv1le, TN 55555-1234 in the USA. \n", "Some of his favorite bands are Converg and Protet the Zero.\"\"\" # Spelling errors intentional.\n", "doc = nlp(text)\n", "\n", "for ent in doc.ents:\n", " print((ent.text, ent.start, ent.end, ent.label_))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's add an entity ruler for some rules-based matches." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2023-04-30T19:40:22.816882Z", "start_time": "2023-04-30T19:40:22.791663Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Anderson, Grint', 0, 3, 'ORG')\n", "('555', 9, 10, 'CARDINAL')\n", "('Apt 5', 14, 16, 'PRODUCT')\n", "('Nashv1le', 17, 18, 'GPE')\n", "('TN', 19, 20, 'GPE')\n", "('USA', 25, 26, 'GPE')\n", "('Converg', 34, 35, 'PERSON')\n", "('Zero', 38, 39, 'CARDINAL')\n", "('Anderson, Grint', 0, 3, 'ORG')\n", "('555', 9, 10, 'CARDINAL')\n", "('Apt 5', 14, 16, 'PRODUCT')\n", "('Nashv1le', 17, 18, 'GPE')\n", "('TN', 19, 20, 'GPE')\n", "('USA', 25, 26, 'GPE')\n", "('Converg', 34, 35, 'PERSON')\n", "('Zero', 38, 39, 'CARDINAL')\n" ] } ], "source": [ "from spacy.pipeline import EntityRuler\n", "\n", "entity_ruler = nlp.add_pipe(\"entity_ruler\", before=\"ner\") #spaCy v3 syntax\n", "entity_ruler.add_patterns(\n", " [{\"label\": \"GPE\", \"pattern\": \"Nashville\"}, {\"label\": \"GPE\", \"pattern\": \"TN\"}]\n", ")\n", "\n", "doc = nlp(text)\n", "\n", "for ent in doc.ents:\n", " print((ent.text, ent.start, ent.end, ent.label_))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's add a spaczz ruler to round this pipeline out. We will also include the `spaczz_span` custom attribute in the results to denote which entities were set via spaczz." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2023-04-30T19:40:33.834459Z", "start_time": "2023-04-30T19:40:33.805176Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Anderson, Grint', 0, 3, 'NAME', True)\n", "('555 Fake St,', 9, 13, 'STREET', True)\n", "('Apt 5', 14, 16, 'PRODUCT', False)\n", "('Nashv1le', 17, 18, 'GPE', True)\n", "('TN', 19, 20, 'GPE', False)\n", "('55555-1234', 20, 23, 'ZIP', True)\n", "('USA', 25, 26, 'GPE', False)\n", "('Converg', 34, 35, 'BAND', True)\n", "('Protet the Zero', 36, 39, 'BAND', True)\n" ] } ], "source": [ "spaczz_ruler = nlp.add_pipe(\"spaczz_ruler\", before=\"ner\") #spaCy v3 syntax\n", "spaczz_ruler.add_patterns(\n", " [\n", " {\n", " \"label\": \"NAME\",\n", " \"pattern\": \"Grant Andersen\",\n", " \"type\": \"fuzzy\",\n", " \"kwargs\": {\"fuzzy_func\": \"token_sort\"},\n", " },\n", " {\n", " \"label\": \"STREET\",\n", " \"pattern\": \"street_addresses\",\n", " \"type\": \"regex\",\n", " \"kwargs\": {\"predef\": True},\n", " },\n", " {\"label\": \"GPE\", \"pattern\": \"Nashville\", \"type\": \"fuzzy\"},\n", " {\n", " \"label\": \"ZIP\",\n", " \"pattern\": r\"\\b(?:55554){s<=1}(?:[-\\s]\\d{4})?\\b\",\n", " \"type\": \"regex\",\n", " }, # fuzzy regex\n", " {\n", " \"label\": \"BAND\",\n", " \"pattern\": [{\"LOWER\": {\"FREGEX\": \"(converge){e<=1}\"}}],\n", " \"type\": \"token\",\n", " },\n", " {\n", " \"label\": \"BAND\",\n", " \"pattern\": [\n", " {\"TEXT\": {\"FUZZY\": \"Protest\"}},\n", " {\"IS_STOP\": True},\n", " {\"TEXT\": {\"FUZZY\": \"Hero\"}},\n", " ],\n", " \"type\": \"token\",\n", " },\n", " ]\n", ")\n", "\n", "doc = nlp(text)\n", "\n", "for ent in doc.ents:\n", " print((ent.text, ent.start, ent.end, ent.label_, ent._.spaczz_ent))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Awesome! The small English model still makes a named entity recognition mistake (\"5\" in \"Apt 5\" as `CARDINAL`), but we're satisfied overall." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's save this pipeline to disk and make sure we can load it back correctly." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2023-04-30T19:40:39.102923Z", "start_time": "2023-04-30T19:40:37.111957Z" } }, "outputs": [ { "data": { "text/plain": [ "['tok2vec',\n", " 'tagger',\n", " 'parser',\n", " 'attribute_ruler',\n", " 'lemmatizer',\n", " 'entity_ruler',\n", " 'spaczz_ruler',\n", " 'ner']" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import tempfile\n", "\n", "with tempfile.TemporaryDirectory() as tmp_dir:\n", " nlp.to_disk(f\"{tmp_dir}/example_pipeline\")\n", " nlp = spacy.load(f\"{tmp_dir}/example_pipeline\")\n", "\n", "nlp.pipe_names" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can even ensure all the spaczz ruler patterns are still present." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2023-04-30T19:40:43.140565Z", "start_time": "2023-04-30T19:40:43.135945Z" } }, "outputs": [ { "data": { "text/plain": [ "[{'label': 'NAME',\n", " 'pattern': 'Grant Andersen',\n", " 'type': 'fuzzy',\n", " 'kwargs': {'fuzzy_func': 'token_sort'}},\n", " {'label': 'GPE', 'pattern': 'Nashville', 'type': 'fuzzy'},\n", " {'label': 'STREET',\n", " 'pattern': 'street_addresses',\n", " 'type': 'regex',\n", " 'kwargs': {'predef': True}},\n", " {'label': 'ZIP',\n", " 'pattern': '\\\\b(?:55554){s<=1}(?:[-\\\\s]\\\\d{4})?\\\\b',\n", " 'type': 'regex'},\n", " {'label': 'BAND',\n", " 'pattern': [{'LOWER': {'FREGEX': '(converge){e<=1}'}}],\n", " 'type': 'token'},\n", " {'label': 'BAND',\n", " 'pattern': [{'TEXT': {'FUZZY': 'Protest'}},\n", " {'IS_STOP': True},\n", " {'TEXT': {'FUZZY': 'Hero'}}],\n", " 'type': 'token'}]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spaczz_ruler = nlp.get_pipe(\"spaczz_ruler\")\n", "spaczz_ruler.patterns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Known Issues" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Performance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The main reason for spaczz's slower speed is that the *c* in it's name is not capitalized like it is in spa*C*y.\n", "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.\n", "\n", "I am *not* currently working on performance optimizations to spaczz, but algorithmic and optimization suggestions are welcome.\n", "\n", "The primary methods for speeding up the `FuzzyMatcher` and `SimilarityMatcher` are by decreasing the `flex` parameter towards `0`, or if `flex > 0`, increasing the `min_r1` parameter towards the value of `min_r2` and/or lowering the `thresh` parameter towards `min_r2`. Be aware that all of these \"speed-ups\" come at the opportunity cost of potentially improved matches.\n", "\n", "As mentioned in the `SimilarityMatcher` description, utilizing a GPU may also help speed up it's matching process." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Roadmap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I am always open and receptive to feature requests but just be aware, as a solo-dev with a lot left to learn, development can move pretty slow. The following is my roadmap for spaczz so you can see where issues raised might fit into my current priorities.\n", "\n", "*Note: while I want to keep `spaczz` functional, I am **not** actively developing it. I try to be responsive to issues and requests but this project is not currently a focus of mine.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**High Priority**\n", "\n", "1. Bug fixes - both breaking and behavioral. Hopefully these will be minimal.\n", "1. Ease of use and error/warning handling and messaging enhancements.\n", "1. Building out Read the Docs.\n", "1. Option to prioritize match quality over length and/or weighing options.\n", "1. Profiling - hopefully to find \"easy\" performance optimizations.\n", "\n", "**Enhancements**\n", "\n", "1. Having spaczz matchers utilize spaCy vocabularies.\n", "1. Rewrite the phrase and token searching algorithms in Cython to utilize C speed.\n", " 1. Try to integrate closer with spaCy." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Development" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pull requests and contributors are welcome.\n", "\n", "spaczz is linted with [Flake8](https://flake8.pycqa.org/en/latest/), formatted with [Black](https://black.readthedocs.io/en/stable/), type-checked with [MyPy](http://mypy-lang.org/) (although this could benefit from improved specificity), tested with [Pytest](https://docs.pytest.org/en/stable/), automated with [Nox](https://nox.thea.codes/en/stable/), and built/packaged with [Poetry](https://python-poetry.org/). There are a few other development tools detailed in the noxfile.py, along with Git pre-commit hooks.\n", "\n", "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "poetry install # Within spaczz's root directory.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I keep Nox and pre-commit outside of my poetry environment as part of my Python toolchain environments. With pre-commit installed you may also need to run the below to commit changes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "pre-commit install\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The only other package that will not be installed via Poetry but is used for testing and in-documentation 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:" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2020-11-12T16:14:04.461530Z", "start_time": "2020-11-12T16:14:04.458674Z" } }, "source": [ "```python\n", "poetry run python -m spacy download \"en_core_web_md\"\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- spaczz tries to stay as close to [spaCy](https://spacy.io/)'s API as possible. Whenever it made sense to use existing spaCy code within spaczz this was done.\n", "- Fuzzy matching is performed using [RapidFuzz](https://github.com/maxbachmann/rapidfuzz).\n", "- Regexes are performed using the [regex](https://pypi.org/project/regex/) library.\n", "- The search algorithm for phrased-based fuzzy and similarity matching was heavily influnced by Stack Overflow user Ulf Aslak's answer in this [thread](https://stackoverflow.com/questions/36013295/find-best-substring-match).\n", "- spaczz's predefined regex patterns were borrowed from the [commonregex](https://github.com/madisonmay/CommonRegex) package.\n", "- spaczz's development and CI/CD patterns were inspired by Claudio Jolowicz's [*Hypermodern Python*](https://cjolowicz.github.io/posts/hypermodern-python-01-setup/) article series." ] } ], "metadata": { "kernelspec": { "display_name": "Python [poetry:spaczz]", "language": "python", "name": "python-poetry-spaczz" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.3" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "217.543px" }, "toc_section_display": true, "toc_window_display": true }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }