2020-07-08 15:00:07 +03:00
|
|
|
from typing import Optional, Sequence
|
2018-05-20 21:26:56 +03:00
|
|
|
import requests
|
2017-03-18 21:29:36 +03:00
|
|
|
import sys
|
Generalize handling of tokenizer special cases (#4259)
* Generalize handling of tokenizer special cases
Handle tokenizer special cases more generally by using the Matcher
internally to match special cases after the affix/token_match
tokenization is complete.
Instead of only matching special cases while processing balanced or
nearly balanced prefixes and suffixes, this recognizes special cases in
a wider range of contexts:
* Allows arbitrary numbers of prefixes/affixes around special cases
* Allows special cases separated by infixes
Existing tests/settings that couldn't be preserved as before:
* The emoticon '")' is no longer a supported special case
* The emoticon ':)' in "example:)" is a false positive again
When merged with #4258 (or the relevant cache bugfix), the affix and
token_match properties should be modified to flush and reload all
special cases to use the updated internal tokenization with the Matcher.
* Remove accidentally added test case
* Really remove accidentally added test
* Reload special cases when necessary
Reload special cases when affixes or token_match are modified. Skip
reloading during initialization.
* Update error code number
* Fix offset and whitespace in Matcher special cases
* Fix offset bugs when merging and splitting tokens
* Set final whitespace on final token in inserted special case
* Improve cache flushing in tokenizer
* Separate cache and specials memory (temporarily)
* Flush cache when adding special cases
* Repeated `self._cache = PreshMap()` and `self._specials = PreshMap()`
are necessary due to this bug:
https://github.com/explosion/preshed/issues/21
* Remove reinitialized PreshMaps on cache flush
* Update UD bin scripts
* Update imports for `bin/`
* Add all currently supported languages
* Update subtok merger for new Matcher validation
* Modify blinded check to look at tokens instead of lemmas (for corpora
with tokens but not lemmas like Telugu)
* Use special Matcher only for cases with affixes
* Reinsert specials cache checks during normal tokenization for special
cases as much as possible
* Additionally include specials cache checks while splitting on infixes
* Since the special Matcher needs consistent affix-only tokenization
for the special cases themselves, introduce the argument
`with_special_cases` in order to do tokenization with or without
specials cache checks
* After normal tokenization, postprocess with special cases Matcher for
special cases containing affixes
* Replace PhraseMatcher with Aho-Corasick
Replace PhraseMatcher with the Aho-Corasick algorithm over numpy arrays
of the hash values for the relevant attribute. The implementation is
based on FlashText.
The speed should be similar to the previous PhraseMatcher. It is now
possible to easily remove match IDs and matches don't go missing with
large keyword lists / vocabularies.
Fixes #4308.
* Restore support for pickling
* Fix internal keyword add/remove for numpy arrays
* Add test for #4248, clean up test
* Improve efficiency of special cases handling
* Use PhraseMatcher instead of Matcher
* Improve efficiency of merging/splitting special cases in document
* Process merge/splits in one pass without repeated token shifting
* Merge in place if no splits
* Update error message number
* Remove UD script modifications
Only used for timing/testing, should be a separate PR
* Remove final traces of UD script modifications
* Update UD bin scripts
* Update imports for `bin/`
* Add all currently supported languages
* Update subtok merger for new Matcher validation
* Modify blinded check to look at tokens instead of lemmas (for corpora
with tokens but not lemmas like Telugu)
* Add missing loop for match ID set in search loop
* Remove cruft in matching loop for partial matches
There was a bit of unnecessary code left over from FlashText in the
matching loop to handle partial token matches, which we don't have with
PhraseMatcher.
* Replace dict trie with MapStruct trie
* Fix how match ID hash is stored/added
* Update fix for match ID vocab
* Switch from map_get_unless_missing to map_get
* Switch from numpy array to Token.get_struct_attr
Access token attributes directly in Doc instead of making a copy of the
relevant values in a numpy array.
Add unsatisfactory warning for hash collision with reserved terminal
hash key. (Ideally it would change the reserved terminal hash and redo
the whole trie, but for now, I'm hoping there won't be collisions.)
* Restructure imports to export find_matches
* Implement full remove()
Remove unnecessary trie paths and free unused maps.
Parallel to Matcher, raise KeyError when attempting to remove a match ID
that has not been added.
* Switch to PhraseMatcher.find_matches
* Switch to local cdef functions for span filtering
* Switch special case reload threshold to variable
Refer to variable instead of hard-coded threshold
* Move more of special case retokenize to cdef nogil
Move as much of the special case retokenization to nogil as possible.
* Rewrap sort as stdsort for OS X
* Rewrap stdsort with specific types
* Switch to qsort
* Fix merge
* Improve cmp functions
* Fix realloc
* Fix realloc again
* Initialize span struct while retokenizing
* Temporarily skip retokenizing
* Revert "Move more of special case retokenize to cdef nogil"
This reverts commit 0b7e52c797cd8ff1548f214bd4186ebb3a7ce8b1.
* Revert "Switch to qsort"
This reverts commit a98d71a942fc9bca531cf5eb05cf89fa88153b60.
* Fix specials check while caching
* Modify URL test with emoticons
The multiple suffix tests result in the emoticon `:>`, which is now
retokenized into one token as a special case after the suffixes are
split off.
* Refactor _apply_special_cases()
* Use cdef ints for span info used in multiple spots
* Modify _filter_special_spans() to prefer earlier
Parallel to #4414, modify _filter_special_spans() so that the earlier
span is preferred for overlapping spans of the same length.
* Replace MatchStruct with Entity
Replace MatchStruct with Entity since the existing Entity struct is
nearly identical.
* Replace Entity with more general SpanC
* Replace MatchStruct with SpanC
* Add error in debug-data if no dev docs are available (see #4575)
* Update azure-pipelines.yml
* Revert "Update azure-pipelines.yml"
This reverts commit ed1060cf59e5895b5fe92ad5b894fd1078ec4c49.
* Use latest wasabi
* Reorganise install_requires
* add dframcy to universe.json (#4580)
* Update universe.json [ci skip]
* Fix multiprocessing for as_tuples=True (#4582)
* Fix conllu script (#4579)
* force extensions to avoid clash between example scripts
* fix arg order and default file encoding
* add example config for conllu script
* newline
* move extension definitions to main function
* few more encodings fixes
* Add load_from_docbin example [ci skip]
TODO: upload the file somewhere
* Update README.md
* Add warnings about 3.8 (resolves #4593) [ci skip]
* Fixed typo: Added space between "recognize" and "various" (#4600)
* Fix DocBin.merge() example (#4599)
* Replace function registries with catalogue (#4584)
* Replace functions registries with catalogue
* Update __init__.py
* Fix test
* Revert unrelated flag [ci skip]
* Bugfix/dep matcher issue 4590 (#4601)
* add contributor agreement for prilopes
* add test for issue #4590
* fix on_match params for DependencyMacther (#4590)
* Minor updates to language example sentences (#4608)
* Add punctuation to Spanish example sentences
* Combine multilanguage examples for lang xx
* Add punctuation to nb examples
* Always realloc to a larger size
Avoid potential (unlikely) edge case and cymem error seen in #4604.
* Add error in debug-data if no dev docs are available (see #4575)
* Update debug-data for GoldCorpus / Example
* Ignore None label in misaligned NER data
2019-11-13 23:24:35 +03:00
|
|
|
from wasabi import msg
|
2020-06-21 22:35:01 +03:00
|
|
|
import typer
|
2017-03-18 21:29:36 +03:00
|
|
|
|
2021-01-30 13:03:25 +03:00
|
|
|
from ._util import app, Arg, Opt, WHEEL_SUFFIX, SDIST_SUFFIX
|
2017-03-18 17:14:48 +03:00
|
|
|
from .. import about
|
2021-06-21 10:39:22 +03:00
|
|
|
from ..util import is_package, get_minor_version, run_command
|
2023-01-31 13:31:17 +03:00
|
|
|
from ..util import is_prerelease_version, get_installed_models
|
|
|
|
from ..util import get_package_version
|
2020-07-08 15:00:07 +03:00
|
|
|
|
2016-03-24 13:19:43 +03:00
|
|
|
|
2020-06-21 14:44:00 +03:00
|
|
|
@app.command(
|
|
|
|
"download",
|
|
|
|
context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
|
|
|
|
)
|
2020-06-21 22:35:01 +03:00
|
|
|
def download_cli(
|
2020-06-21 14:44:00 +03:00
|
|
|
# fmt: off
|
2020-06-21 22:35:01 +03:00
|
|
|
ctx: typer.Context,
|
2020-09-03 14:13:03 +03:00
|
|
|
model: str = Arg(..., help="Name of pipeline package to download"),
|
2020-06-22 01:57:28 +03:00
|
|
|
direct: bool = Opt(False, "--direct", "-d", "-D", help="Force direct download of name + version"),
|
2022-09-02 12:58:21 +03:00
|
|
|
sdist: bool = Opt(False, "--sdist", "-S", help="Download sdist (.tar.gz) archive instead of pre-built binary wheel"),
|
2020-06-21 14:44:00 +03:00
|
|
|
# fmt: on
|
2020-01-01 15:15:46 +03:00
|
|
|
):
|
2017-05-27 21:01:46 +03:00
|
|
|
"""
|
2020-09-03 14:13:03 +03:00
|
|
|
Download compatible trained pipeline from the default download path using
|
|
|
|
pip. If --direct flag is set, the command expects the full package name with
|
|
|
|
version. For direct downloads, the compatibility check will be skipped. All
|
2020-06-21 22:35:01 +03:00
|
|
|
additional arguments provided to this command will be passed to `pip install`
|
2020-09-03 14:13:03 +03:00
|
|
|
on package installation.
|
|
|
|
|
2021-01-30 12:09:38 +03:00
|
|
|
DOCS: https://spacy.io/api/cli#download
|
2020-09-03 14:13:03 +03:00
|
|
|
AVAILABLE PACKAGES: https://spacy.io/models
|
2017-05-22 13:28:58 +03:00
|
|
|
"""
|
2021-02-01 15:18:43 +03:00
|
|
|
download(model, direct, sdist, *ctx.args)
|
2020-06-21 22:35:01 +03:00
|
|
|
|
|
|
|
|
2022-09-02 12:58:21 +03:00
|
|
|
def download(
|
|
|
|
model: str,
|
|
|
|
direct: bool = False,
|
|
|
|
sdist: bool = False,
|
|
|
|
*pip_args,
|
|
|
|
) -> None:
|
2021-01-05 05:41:53 +03:00
|
|
|
if (
|
|
|
|
not (is_package("spacy") or is_package("spacy-nightly"))
|
|
|
|
and "--no-deps" not in pip_args
|
|
|
|
):
|
2019-09-17 00:32:41 +03:00
|
|
|
msg.warn(
|
2020-09-03 14:13:03 +03:00
|
|
|
"Skipping pipeline package dependencies and setting `--no-deps`. "
|
2019-09-17 00:32:41 +03:00
|
|
|
"You don't seem to have the spaCy package itself installed "
|
|
|
|
"(maybe because you've built from source?), so installing the "
|
2020-09-03 14:13:03 +03:00
|
|
|
"package dependencies would cause spaCy to be downloaded, which "
|
|
|
|
"probably isn't what you want. If the pipeline package has other "
|
2019-09-17 00:32:41 +03:00
|
|
|
"dependencies, you'll have to install them manually."
|
|
|
|
)
|
|
|
|
pip_args = pip_args + ("--no-deps",)
|
2017-03-15 19:37:18 +03:00
|
|
|
if direct:
|
2019-03-07 23:07:19 +03:00
|
|
|
components = model.split("-")
|
|
|
|
model_name = "".join(components[:-1])
|
|
|
|
version = components[-1]
|
2017-03-15 19:37:18 +03:00
|
|
|
else:
|
2020-07-08 15:00:07 +03:00
|
|
|
model_name = model
|
2018-05-20 21:26:56 +03:00
|
|
|
compatibility = get_compatibility()
|
2017-03-16 21:54:51 +03:00
|
|
|
version = get_version(model_name, compatibility)
|
2022-09-02 12:58:21 +03:00
|
|
|
|
2023-01-31 13:31:17 +03:00
|
|
|
# If we already have this version installed, skip downloading
|
|
|
|
installed = get_installed_models()
|
|
|
|
if model_name in installed:
|
|
|
|
installed_version = get_package_version(model_name)
|
|
|
|
if installed_version == version:
|
|
|
|
msg.warn(f"{model_name} v{version} already installed, skipping")
|
|
|
|
return
|
|
|
|
|
2022-09-02 12:58:21 +03:00
|
|
|
filename = get_model_filename(model_name, version, sdist)
|
|
|
|
|
|
|
|
download_model(filename, pip_args)
|
2020-06-21 22:35:01 +03:00
|
|
|
msg.good(
|
|
|
|
"Download and installation successful",
|
2020-09-03 14:13:03 +03:00
|
|
|
f"You can now load the package via spacy.load('{model_name}')",
|
2020-06-21 22:35:01 +03:00
|
|
|
)
|
2017-03-17 01:23:26 +03:00
|
|
|
|
2018-01-03 22:14:50 +03:00
|
|
|
|
2022-09-02 12:58:21 +03:00
|
|
|
def get_model_filename(model_name: str, version: str, sdist: bool = False) -> str:
|
|
|
|
dl_tpl = "{m}-{v}/{m}-{v}{s}"
|
|
|
|
suffix = SDIST_SUFFIX if sdist else WHEEL_SUFFIX
|
|
|
|
filename = dl_tpl.format(m=model_name, v=version, s=suffix)
|
|
|
|
return filename
|
|
|
|
|
|
|
|
|
2020-07-08 15:00:07 +03:00
|
|
|
def get_compatibility() -> dict:
|
2022-08-04 16:14:19 +03:00
|
|
|
if is_prerelease_version(about.__version__):
|
|
|
|
version: Optional[str] = about.__version__
|
|
|
|
else:
|
|
|
|
version = get_minor_version(about.__version__)
|
2020-07-08 15:00:07 +03:00
|
|
|
r = requests.get(about.__compatibility__)
|
2018-05-20 21:26:56 +03:00
|
|
|
if r.status_code != 200:
|
2018-11-30 22:16:14 +03:00
|
|
|
msg.fail(
|
2019-12-22 03:53:56 +03:00
|
|
|
f"Server error ({r.status_code})",
|
2020-09-03 14:13:03 +03:00
|
|
|
f"Couldn't fetch compatibility table. Please find a package for your spaCy "
|
2019-12-22 03:53:56 +03:00
|
|
|
f"installation (v{about.__version__}), and download it manually. "
|
|
|
|
f"For more details, see the documentation: "
|
2021-01-30 12:09:38 +03:00
|
|
|
f"https://spacy.io/usage/models",
|
2018-11-30 22:16:14 +03:00
|
|
|
exits=1,
|
|
|
|
)
|
2020-07-08 15:00:07 +03:00
|
|
|
comp_table = r.json()
|
2018-11-30 22:16:14 +03:00
|
|
|
comp = comp_table["spacy"]
|
2017-03-15 19:37:18 +03:00
|
|
|
if version not in comp:
|
2020-09-03 14:13:03 +03:00
|
|
|
msg.fail(f"No compatible packages found for v{version} of spaCy", exits=1)
|
2017-04-26 19:00:28 +03:00
|
|
|
return comp[version]
|
2017-03-15 19:37:18 +03:00
|
|
|
|
|
|
|
|
2020-06-21 22:35:01 +03:00
|
|
|
def get_version(model: str, comp: dict) -> str:
|
2017-03-15 19:37:18 +03:00
|
|
|
if model not in comp:
|
2018-11-30 22:16:14 +03:00
|
|
|
msg.fail(
|
2020-09-03 14:13:03 +03:00
|
|
|
f"No compatible package found for '{model}' (spaCy v{about.__version__})",
|
2018-11-30 22:16:14 +03:00
|
|
|
exits=1,
|
|
|
|
)
|
2017-03-15 19:37:18 +03:00
|
|
|
return comp[model][0]
|
|
|
|
|
|
|
|
|
2022-09-02 12:58:21 +03:00
|
|
|
def get_latest_version(model: str) -> str:
|
|
|
|
comp = get_compatibility()
|
|
|
|
return get_version(model, comp)
|
|
|
|
|
|
|
|
|
2020-06-21 22:35:01 +03:00
|
|
|
def download_model(
|
|
|
|
filename: str, user_pip_args: Optional[Sequence[str]] = None
|
|
|
|
) -> None:
|
2018-11-30 22:16:14 +03:00
|
|
|
download_url = about.__download_url__ + "/" + filename
|
2021-02-01 15:15:00 +03:00
|
|
|
pip_args = list(user_pip_args) if user_pip_args is not None else []
|
2018-11-30 22:16:14 +03:00
|
|
|
cmd = [sys.executable, "-m", "pip", "install"] + pip_args + [download_url]
|
2020-06-21 22:35:01 +03:00
|
|
|
run_command(cmd)
|