2017-05-14 18:50:23 +03:00
|
|
|
# coding: utf8
|
2019-03-08 13:42:26 +03:00
|
|
|
"""
|
|
|
|
spaCy's built in visualization suite for dependencies and named entities.
|
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/top-level#displacy
|
|
|
|
USAGE: https://spacy.io/usage/visualizers
|
|
|
|
"""
|
2017-05-14 18:50:23 +03:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from .render import DependencyRenderer, EntityRenderer
|
2018-06-25 15:55:16 +03:00
|
|
|
from ..tokens import Doc, Span
|
2017-08-01 02:11:35 +03:00
|
|
|
from ..compat import b_to_str
|
2018-04-03 16:50:31 +03:00
|
|
|
from ..errors import Errors, Warnings, user_warning
|
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(
|
2019-04-22 15:18:32 +03:00
|
|
|
docs, style="dep", page=False, minify=False, jupyter=None, options={}, manual=False
|
💫 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
|
|
|
):
|
2017-05-14 18:50:23 +03:00
|
|
|
"""Render displaCy visualisation.
|
|
|
|
|
|
|
|
docs (list or Doc): Document(s) to visualise.
|
|
|
|
style (unicode): Visualisation style, 'dep' or 'ent'.
|
|
|
|
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.
|
2017-05-14 20:30:47 +03:00
|
|
|
RETURNS (unicode): Rendered HTML markup.
|
2019-03-08 13:42:26 +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)
|
2017-05-22 19:48:20 +03:00
|
|
|
renderer, converter = factories[style]
|
|
|
|
renderer = renderer(options=options)
|
|
|
|
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(
|
2018-12-20 19:32:04 +03:00
|
|
|
docs,
|
|
|
|
style="dep",
|
|
|
|
page=True,
|
|
|
|
minify=False,
|
|
|
|
options={},
|
|
|
|
manual=False,
|
|
|
|
port=5000,
|
|
|
|
host="0.0.0.0",
|
💫 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
|
|
|
):
|
2017-05-14 18:50:23 +03:00
|
|
|
"""Serve displaCy visualisation.
|
|
|
|
|
|
|
|
docs (list or Doc): Document(s) to visualise.
|
|
|
|
style (unicode): Visualisation style, 'dep' or 'ent'.
|
|
|
|
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.
|
2018-12-20 19:32:04 +03:00
|
|
|
host (unicode): Host to serve visualisation.
|
2019-03-08 13:42:26 +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():
|
2018-12-20 19:32:04 +03:00
|
|
|
user_warning(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)
|
2018-11-30 22:16:14 +03:00
|
|
|
print("\nUsing the '{}' visualizer".format(style))
|
2018-12-20 19:32:04 +03:00
|
|
|
print("Serving on http://{}:{} ...\n".format(host, port))
|
2017-06-03 14:24:56 +03:00
|
|
|
try:
|
|
|
|
httpd.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
2018-11-30 22:16:14 +03:00
|
|
|
print("Shutting down server on port {}.".format(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):
|
💫 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
|
|
|
# Headers and status need to be bytes in Python 2, see #1227
|
|
|
|
headers = [(b_to_str(b"Content-type"), b_to_str(b"text/html; charset=utf-8"))]
|
|
|
|
start_response(b_to_str(b"200 OK"), headers)
|
|
|
|
res = _html["parsed"].encode(encoding="utf-8")
|
2017-05-14 18:50:23 +03:00
|
|
|
return [res]
|
|
|
|
|
|
|
|
|
2017-06-03 14:24:43 +03:00
|
|
|
def parse_deps(orig_doc, options={}):
|
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.
|
|
|
|
"""
|
2019-06-26 15:37:05 +03:00
|
|
|
doc = Doc(orig_doc.vocab).from_bytes(orig_doc.to_bytes(exclude=["user_data"]))
|
2018-04-03 16:50:31 +03:00
|
|
|
if not doc.is_parsed:
|
|
|
|
user_warning(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)
|
💫 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("fine_grained"):
|
|
|
|
words = [{"text": w.text, "tag": w.tag_} for w in doc]
|
2017-12-09 17:11:12 +03:00
|
|
|
else:
|
💫 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
|
|
|
words = [{"text": w.text, "tag": w.pos_} 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
|
|
|
|
|
|
|
|
|
|
|
def parse_ents(doc, options={}):
|
|
|
|
"""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:
|
|
|
|
user_warning(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
|
|
|
|
|
|
|
|
|
|
|
def set_render_wrapper(func):
|
|
|
|
"""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
|
|
|
|
|
|
|
|
|
|
|
def get_doc_settings(doc):
|
|
|
|
return {
|
|
|
|
"lang": doc.lang_,
|
|
|
|
"direction": doc.vocab.writing_system.get("direction", "ltr"),
|
|
|
|
}
|