Skip to content

Commit

Permalink
Pass manual=True to visualize_ner (#27)
Browse files Browse the repository at this point in the history
* Add manual option to visualize_ner

* Error checking

Error 1: When manual=True but show_table=True
Error 2: When manual=True but doc is not of type 'list'
  • Loading branch information
callistachang authored Nov 9, 2021
1 parent aa9d029 commit 6eaaf43
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
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

0 comments on commit 6eaaf43

Please sign in to comment.