From ff5ca9000ac5c5c749c20e71c13305b7a0478c75 Mon Sep 17 00:00:00 2001 From: callistachang <34191886+callistachang@users.noreply.github.com> Date: Sat, 23 Oct 2021 12:15:07 +0800 Subject: [PATCH 1/2] Add manual option to visualize_ner --- examples/03_visualize-ner-manual.py | 21 +++++++++++++++++++++ spacy_streamlit/visualizer.py | 5 +++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 examples/03_visualize-ner-manual.py diff --git a/examples/03_visualize-ner-manual.py b/examples/03_visualize-ner-manual.py new file mode 100644 index 0000000..61f4b69 --- /dev/null +++ b/examples/03_visualize-ner-manual.py @@ -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 +) diff --git a/spacy_streamlit/visualizer.py b/spacy_streamlit/visualizer.py index 59b3795..a241b45 100644 --- a/spacy_streamlit/visualizer.py +++ b/spacy_streamlit/visualizer.py @@ -162,7 +162,7 @@ def visualize_parser( def visualize_ner( - doc: spacy.tokens.Doc, + doc: Union[spacy.tokens.Doc, dict], *, labels: Sequence[str] = tuple(), attrs: List[str] = NER_ATTRS, @@ -170,6 +170,7 @@ def visualize_ner( title: Optional[str] = "Named Entities", colors: Dict[str, str] = {}, key: Optional[str] = None, + manual: Optional[bool] = False, ) -> None: """Visualizer for named entities.""" if title: @@ -188,7 +189,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 = "" st.write(f"{style}{get_html(html)}", unsafe_allow_html=True) From 8d00ca834cd10da4aabacfad639c3d9831195b45 Mon Sep 17 00:00:00 2001 From: callistachang <34191886+callistachang@users.noreply.github.com> Date: Mon, 8 Nov 2021 00:27:48 +0800 Subject: [PATCH 2/2] Error checking Error 1: When manual=True but show_table=True Error 2: When manual=True but doc is not of type 'list' --- spacy_streamlit/visualizer.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/spacy_streamlit/visualizer.py b/spacy_streamlit/visualizer.py index a241b45..d5fea4d 100644 --- a/spacy_streamlit/visualizer.py +++ b/spacy_streamlit/visualizer.py @@ -162,7 +162,7 @@ def visualize_parser( def visualize_ner( - doc: Union[spacy.tokens.Doc, dict], + doc: Union[spacy.tokens.Doc, List[Dict[str, str]]], *, labels: Sequence[str] = tuple(), attrs: List[str] = NER_ATTRS, @@ -176,8 +176,14 @@ def visualize_ner( 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: