Skip to content

Commit

Permalink
Changes as per comments on PR. Allow smoother transition to deprecati…
Browse files Browse the repository at this point in the history
…ons of colors arg from visualize_ner method.
  • Loading branch information
narayanacharya6 committed Dec 22, 2021
1 parent ee27497 commit 3041134
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
22 changes: 17 additions & 5 deletions examples/04_visualize-ner-extra-options.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
Example of using extra_options for visualize_ner.
"""
import spacy
import streamlit as st

import spacy_streamlit

st.title("My cool app")

nlp = spacy.blank("en")
text = "But Google is starting from behind."
doc = nlp.make_doc(text)
Expand All @@ -18,6 +15,21 @@
doc,
labels=["ORG"],
show_table=False,
title="Persons, dates and locations",
extra_options={"kb_url_template": "https://www.wikidata.org/wiki/{}"}
title="Custom Colors NER Visualization",
colors={"ORG": "#EEE"},
options={
"kb_url_template": "https://www.wikidata.org/wiki/{}"
},
key="Custom Colors"
)

spacy_streamlit.visualize_ner(
doc,
labels=["ORG"],
show_table=False,
title="Default Colors NER Visualization",
options={
"kb_url_template": "https://www.wikidata.org/wiki/{}"
},
key="Default Colors"
)
46 changes: 42 additions & 4 deletions spacy_streamlit/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,44 @@ def visualize_ner(
colors: Dict[str, str] = {},
key: Optional[str] = None,
manual: Optional[bool] = False,
extra_options: Optional[Dict] = {},
options: Optional[Dict] = {},
):
"""
Visualizer for named entities.
doc (Doc, List): The document to visualize.
labels (list): The entity labels to visualize.
attrs (list): The attributes on the entity Span to be labeled. Attributes are displayed only when the show_table
argument is True.
title (str): The title displayed at the top of the NER visualization.
colors (Dict): Dictionary of colors for the entity spans to visualize, with keys as labels and corresponding colors
as the values. This argument will be deprecated soon. In future the colors arg need to be passed in the options arg
with the key "colors".
key (str): Key used for the streamlit component for selecting labels.
manual (bool): Flag signifying whether the doc argument is a Doc object or a List of Dicts containing entity span
information.
options: Dictionary of options to be passed to the displacy render method for generating the HTML to be rendered.
"""
if colors:
if not options:
options = dict()

options["colors"] = colors

_visualize_ner(doc, labels=labels, attrs=attrs, show_table=show_table, title=title, key=key, manual=manual,
options=options)


def _visualize_ner(
doc: Union[spacy.tokens.Doc, List[Dict[str, str]]],
*,
labels: Sequence[str] = tuple(),
attrs: List[str] = NER_ATTRS,
show_table: bool = True,
title: Optional[str] = "Named Entities",
key: Optional[str] = None,
manual: Optional[bool] = False,
options: Optional[Dict] = {},
) -> None:
"""Visualizer for named entities."""
if title:
Expand Down Expand Up @@ -199,9 +236,10 @@ def visualize_ner(
default=list(labels),
key=f"{key}_ner_label_select",
)
options = {"ents": label_select, "colors": colors}
if extra_options:
options.update(extra_options)
if not options:
options = dict()

options["ents"] = label_select
html = displacy.render(
doc,
style="ent",
Expand Down

0 comments on commit 3041134

Please sign in to comment.