Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass manual=True to visualize_ner #27

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/03_visualize-ner-manual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Example of using manual=True for visualize_ner.
"""
import spacy_streamlit
import streamlit as st

st.title("My cool app")

doc = [{
"text": "But Google is starting from behind.",
"ents": [{"start": 4, "end": 10, "label": "ORG"}],
"title": None
}]

spacy_streamlit.visualize_ner(
doc,
labels=["ORG"],
show_table=False,
title="Persons, dates and locations",
manual=True
)
15 changes: 11 additions & 4 deletions spacy_streamlit/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,28 @@ def visualize_parser(


def visualize_ner(
doc: spacy.tokens.Doc,
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",
colors: Dict[str, str] = {},
key: Optional[str] = None,
manual: Optional[bool] = False,
) -> None:
"""Visualizer for named entities."""
if title:
st.header(title)

labels = labels or [ent.label_ for ent in doc.ents]

if manual:
if show_table:
st.warning("When the parameter 'manual' is set to True, the parameter 'show_table' must be set to False.")
if not isinstance(doc, list):
st.warning("When the parameter 'manual' is set to True, the parameter 'doc' must be of type 'list', not 'spacy.tokens.Doc'.")
else:
labels = labels or [ent.label_ for ent in doc.ents]

if not labels:
st.warning("The parameter 'labels' should not be empty or None.")
else:
Expand All @@ -188,7 +195,7 @@ def visualize_ner(
key=f"{key}_ner_label_select",
)
html = displacy.render(
doc, style="ent", options={"ents": label_select, "colors": colors}
doc, style="ent", options={"ents": label_select, "colors": colors}, manual=manual
)
style = "<style>mark.entity { display: inline-block }</style>"
st.write(f"{style}{get_html(html)}", unsafe_allow_html=True)
Expand Down