2019-03-08 13:42:26 +03:00
|
|
|
"""
|
|
|
|
spaCy's built in visualization suite for dependencies and named entities.
|
|
|
|
|
2021-01-30 12:09:38 +03:00
|
|
|
DOCS: https://spacy.io/api/top-level#displacy
|
|
|
|
USAGE: https://spacy.io/usage/visualizers
|
2019-03-08 13:42:26 +03:00
|
|
|
"""
|
2020-07-25 16:01:15 +03:00
|
|
|
from typing import Union, Iterable, Optional, Dict, Any, Callable
|
2020-02-28 14:20:23 +03:00
|
|
|
import warnings
|
|
|
|
|
2017-05-14 18:50:23 +03:00
|
|
|
from .render import DependencyRenderer, EntityRenderer
|
2018-06-25 15:55:16 +03:00
|
|
|
from ..tokens import Doc, Span
|
2020-02-28 14:20:23 +03:00
|
|
|
from ..errors import Errors, Warnings
|
2018-11-30 22:16:14 +03:00
|
|
|
from ..util import is_in_jupyter
|
2017-05-14 18:50:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
_html = {}
|
2018-12-20 19:32:04 +03:00
|
|
|
RENDER_WRAPPER = None
|
2017-05-14 18:50:23 +03:00
|
|
|
|
|
|
|
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
def render(
|
2020-08-17 17:45:24 +03:00
|
|
|
docs: Union[Iterable[Union[Doc, Span]], Doc, Span],
|
2020-07-25 16:01:15 +03:00
|
|
|
style: str = "dep",
|
|
|
|
page: bool = False,
|
|
|
|
minify: bool = False,
|
|
|
|
jupyter: Optional[bool] = None,
|
|
|
|
options: Dict[str, Any] = {},
|
|
|
|
manual: bool = False,
|
|
|
|
) -> str:
|
2017-05-14 18:50:23 +03:00
|
|
|
"""Render displaCy visualisation.
|
|
|
|
|
2020-07-25 16:01:15 +03:00
|
|
|
docs (Union[Iterable[Doc], Doc]): Document(s) to visualise.
|
2020-05-24 18:20:58 +03:00
|
|
|
style (str): Visualisation style, 'dep' or 'ent'.
|
2017-05-14 18:50:23 +03:00
|
|
|
page (bool): Render markup as full HTML page.
|
|
|
|
minify (bool): Minify HTML markup.
|
2019-04-22 15:18:32 +03:00
|
|
|
jupyter (bool): Override Jupyter auto-detection.
|
2017-05-14 18:50:23 +03:00
|
|
|
options (dict): Visualiser-specific options, e.g. colors.
|
2017-10-27 15:39:19 +03:00
|
|
|
manual (bool): Don't parse `Doc` and instead expect a dict/list of dicts.
|
2020-05-24 18:20:58 +03:00
|
|
|
RETURNS (str): Rendered HTML markup.
|
2019-03-08 13:42:26 +03:00
|
|
|
|
2021-01-30 12:09:38 +03:00
|
|
|
DOCS: https://spacy.io/api/top-level#displacy.render
|
|
|
|
USAGE: https://spacy.io/usage/visualizers
|
2017-05-14 18:50:23 +03:00
|
|
|
"""
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
factories = {
|
|
|
|
"dep": (DependencyRenderer, parse_deps),
|
|
|
|
"ent": (EntityRenderer, parse_ents),
|
|
|
|
}
|
2017-05-22 19:48:20 +03:00
|
|
|
if style not in factories:
|
2018-04-03 16:50:31 +03:00
|
|
|
raise ValueError(Errors.E087.format(style=style))
|
2018-06-25 15:55:16 +03:00
|
|
|
if isinstance(docs, (Doc, Span, dict)):
|
2017-05-22 19:48:20 +03:00
|
|
|
docs = [docs]
|
2018-06-25 15:55:16 +03:00
|
|
|
docs = [obj if not isinstance(obj, Span) else obj.as_doc() for obj in docs]
|
|
|
|
if not all(isinstance(obj, (Doc, Span, dict)) for obj in docs):
|
|
|
|
raise ValueError(Errors.E096)
|
2020-07-25 16:01:15 +03:00
|
|
|
renderer_func, converter = factories[style]
|
|
|
|
renderer = renderer_func(options=options)
|
2017-05-22 19:48:20 +03:00
|
|
|
parsed = [converter(doc, options) for doc in docs] if not manual else docs
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
_html["parsed"] = renderer.render(parsed, page=page, minify=minify).strip()
|
|
|
|
html = _html["parsed"]
|
2018-12-20 19:32:04 +03:00
|
|
|
if RENDER_WRAPPER is not None:
|
|
|
|
html = RENDER_WRAPPER(html)
|
2019-04-22 15:18:32 +03:00
|
|
|
if jupyter or (jupyter is None and is_in_jupyter()):
|
|
|
|
# return HTML rendered by IPython display()
|
2020-01-01 15:15:05 +03:00
|
|
|
# See #4840 for details on span wrapper to disable mathjax
|
2017-05-14 19:39:01 +03:00
|
|
|
from IPython.core.display import display, HTML
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
|
2020-01-01 15:15:05 +03:00
|
|
|
return display(HTML('<span class="tex2jax_ignore">{}</span>'.format(html)))
|
2017-05-14 19:39:01 +03:00
|
|
|
return html
|
2017-05-14 18:50:23 +03:00
|
|
|
|
|
|
|
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
def serve(
|
2020-07-25 16:01:15 +03:00
|
|
|
docs: Union[Iterable[Doc], Doc],
|
|
|
|
style: str = "dep",
|
|
|
|
page: bool = True,
|
|
|
|
minify: bool = False,
|
|
|
|
options: Dict[str, Any] = {},
|
|
|
|
manual: bool = False,
|
|
|
|
port: int = 5000,
|
|
|
|
host: str = "0.0.0.0",
|
|
|
|
) -> None:
|
2017-05-14 18:50:23 +03:00
|
|
|
"""Serve displaCy visualisation.
|
|
|
|
|
|
|
|
docs (list or Doc): Document(s) to visualise.
|
2020-05-24 18:20:58 +03:00
|
|
|
style (str): Visualisation style, 'dep' or 'ent'.
|
2017-05-14 18:50:23 +03:00
|
|
|
page (bool): Render markup as full HTML page.
|
|
|
|
minify (bool): Minify HTML markup.
|
|
|
|
options (dict): Visualiser-specific options, e.g. colors.
|
2017-10-27 15:39:19 +03:00
|
|
|
manual (bool): Don't parse `Doc` and instead expect a dict/list of dicts.
|
2017-05-14 18:50:23 +03:00
|
|
|
port (int): Port to serve visualisation.
|
2020-05-24 18:20:58 +03:00
|
|
|
host (str): Host to serve visualisation.
|
2019-03-08 13:42:26 +03:00
|
|
|
|
2021-01-30 12:09:38 +03:00
|
|
|
DOCS: https://spacy.io/api/top-level#displacy.serve
|
|
|
|
USAGE: https://spacy.io/usage/visualizers
|
2017-05-14 18:50:23 +03:00
|
|
|
"""
|
|
|
|
from wsgiref import simple_server
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
|
2019-02-08 16:14:49 +03:00
|
|
|
if is_in_jupyter():
|
2020-02-28 14:20:23 +03:00
|
|
|
warnings.warn(Warnings.W011)
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
render(docs, style=style, page=page, minify=minify, options=options, manual=manual)
|
2018-12-20 19:32:04 +03:00
|
|
|
httpd = simple_server.make_server(host, port, app)
|
2019-12-22 03:53:56 +03:00
|
|
|
print(f"\nUsing the '{style}' visualizer")
|
|
|
|
print(f"Serving on http://{host}:{port} ...\n")
|
2017-06-03 14:24:56 +03:00
|
|
|
try:
|
|
|
|
httpd.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
2019-12-22 03:53:56 +03:00
|
|
|
print(f"Shutting down server on port {port}.")
|
2017-06-03 14:24:56 +03:00
|
|
|
finally:
|
|
|
|
httpd.server_close()
|
2017-05-14 18:50:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
def app(environ, start_response):
|
2019-12-22 03:53:56 +03:00
|
|
|
headers = [("Content-type", "text/html; charset=utf-8")]
|
|
|
|
start_response("200 OK", headers)
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
res = _html["parsed"].encode(encoding="utf-8")
|
2017-05-14 18:50:23 +03:00
|
|
|
return [res]
|
|
|
|
|
|
|
|
|
2020-07-25 16:01:15 +03:00
|
|
|
def parse_deps(orig_doc: Doc, options: Dict[str, Any] = {}) -> Dict[str, Any]:
|
2017-05-14 18:50:23 +03:00
|
|
|
"""Generate dependency parse in {'words': [], 'arcs': []} format.
|
|
|
|
|
|
|
|
doc (Doc): Document do parse.
|
|
|
|
RETURNS (dict): Generated dependency parse keyed by words and arcs.
|
|
|
|
"""
|
2021-03-12 11:41:59 +03:00
|
|
|
doc = Doc(orig_doc.vocab).from_bytes(orig_doc.to_bytes(exclude=["user_data", "user_hooks"]))
|
2020-09-17 01:14:01 +03:00
|
|
|
if not doc.has_annotation("DEP"):
|
2020-02-28 14:20:23 +03:00
|
|
|
warnings.warn(Warnings.W005)
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
if options.get("collapse_phrases", False):
|
2019-02-15 12:29:44 +03:00
|
|
|
with doc.retokenize() as retokenizer:
|
|
|
|
for np in list(doc.noun_chunks):
|
|
|
|
attrs = {
|
|
|
|
"tag": np.root.tag_,
|
|
|
|
"lemma": np.root.lemma_,
|
|
|
|
"ent_type": np.root.ent_type_,
|
|
|
|
}
|
|
|
|
retokenizer.merge(np, attrs=attrs)
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
if options.get("collapse_punct", True):
|
2017-05-14 18:50:23 +03:00
|
|
|
spans = []
|
|
|
|
for word in doc[:-1]:
|
|
|
|
if word.is_punct or not word.nbor(1).is_punct:
|
|
|
|
continue
|
|
|
|
start = word.i
|
|
|
|
end = word.i + 1
|
|
|
|
while end < len(doc) and doc[end].is_punct:
|
|
|
|
end += 1
|
2017-10-27 15:39:19 +03:00
|
|
|
span = doc[start:end]
|
2019-02-15 12:29:44 +03:00
|
|
|
spans.append((span, word.tag_, word.lemma_, word.ent_type_))
|
|
|
|
with doc.retokenize() as retokenizer:
|
|
|
|
for span, tag, lemma, ent_type in spans:
|
|
|
|
attrs = {"tag": tag, "lemma": lemma, "ent_type": ent_type}
|
|
|
|
retokenizer.merge(span, attrs=attrs)
|
2020-02-22 16:11:51 +03:00
|
|
|
fine_grained = options.get("fine_grained")
|
|
|
|
add_lemma = options.get("add_lemma")
|
2020-03-25 14:28:12 +03:00
|
|
|
words = [
|
|
|
|
{
|
|
|
|
"text": w.text,
|
|
|
|
"tag": w.tag_ if fine_grained else w.pos_,
|
|
|
|
"lemma": w.lemma_ if add_lemma else None,
|
|
|
|
}
|
|
|
|
for w in doc
|
|
|
|
]
|
2017-05-14 18:50:23 +03:00
|
|
|
arcs = []
|
|
|
|
for word in doc:
|
|
|
|
if word.i < word.head.i:
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
arcs.append(
|
|
|
|
{"start": word.i, "end": word.head.i, "label": word.dep_, "dir": "left"}
|
|
|
|
)
|
2017-05-14 18:50:23 +03:00
|
|
|
elif word.i > word.head.i:
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
arcs.append(
|
|
|
|
{
|
|
|
|
"start": word.head.i,
|
|
|
|
"end": word.i,
|
|
|
|
"label": word.dep_,
|
|
|
|
"dir": "right",
|
|
|
|
}
|
|
|
|
)
|
2019-03-11 20:52:50 +03:00
|
|
|
return {"words": words, "arcs": arcs, "settings": get_doc_settings(orig_doc)}
|
2017-05-14 18:50:23 +03:00
|
|
|
|
|
|
|
|
2020-07-25 16:01:15 +03:00
|
|
|
def parse_ents(doc: Doc, options: Dict[str, Any] = {}) -> Dict[str, Any]:
|
2017-05-14 18:50:23 +03:00
|
|
|
"""Generate named entities in [{start: i, end: i, label: 'label'}] format.
|
|
|
|
|
|
|
|
doc (Doc): Document do parse.
|
|
|
|
RETURNS (dict): Generated entities keyed by text (original text) and ents.
|
|
|
|
"""
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
ents = [
|
|
|
|
{"start": ent.start_char, "end": ent.end_char, "label": ent.label_}
|
|
|
|
for ent in doc.ents
|
|
|
|
]
|
2018-04-03 16:50:31 +03:00
|
|
|
if not ents:
|
2020-02-28 14:20:23 +03:00
|
|
|
warnings.warn(Warnings.W006)
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 19:03:03 +03:00
|
|
|
title = doc.user_data.get("title", None) if hasattr(doc, "user_data") else None
|
2019-03-11 20:52:50 +03:00
|
|
|
settings = get_doc_settings(doc)
|
|
|
|
return {"text": doc.text, "ents": ents, "title": title, "settings": settings}
|
2018-12-20 19:32:04 +03:00
|
|
|
|
|
|
|
|
2020-07-25 16:01:15 +03:00
|
|
|
def set_render_wrapper(func: Callable[[str], str]) -> None:
|
2018-12-20 19:32:04 +03:00
|
|
|
"""Set an optional wrapper function that is called around the generated
|
|
|
|
HTML markup on displacy.render. This can be used to allow integration into
|
|
|
|
other platforms, similar to Jupyter Notebooks that require functions to be
|
|
|
|
called around the HTML. It can also be used to implement custom callbacks
|
|
|
|
on render, or to embed the visualization in a custom page.
|
|
|
|
|
|
|
|
func (callable): Function to call around markup before rendering it. Needs
|
|
|
|
to take one argument, the HTML markup, and should return the desired
|
|
|
|
output of displacy.render.
|
|
|
|
"""
|
|
|
|
global RENDER_WRAPPER
|
|
|
|
if not hasattr(func, "__call__"):
|
|
|
|
raise ValueError(Errors.E110.format(obj=type(func)))
|
|
|
|
RENDER_WRAPPER = func
|
2019-03-11 20:52:50 +03:00
|
|
|
|
|
|
|
|
2020-07-25 16:01:15 +03:00
|
|
|
def get_doc_settings(doc: Doc) -> Dict[str, Any]:
|
2019-03-11 20:52:50 +03:00
|
|
|
return {
|
|
|
|
"lang": doc.lang_,
|
|
|
|
"direction": doc.vocab.writing_system.get("direction", "ltr"),
|
|
|
|
}
|