Replies: 2 comments 3 replies
-
There is no built-in solution to annotate specific words in a text with Taipy. Do you have a specific example of what you would like so we could create an issue to improve Taipy? However, it is possible to create annotations and style words like this. main.py: from taipy.gui import Gui
sentence = "The quick brown fox jumps in the garden"
md = """
<|{sentence}|>
<|Annotate this|button|on_action=annotate|>
<|part|partial={text_annotation}|>
"""
def annotate(state):
new_sentence = ''
for word in sentence.split():
if word == 'fox':
new_sentence += f'<|{word}|text|class_name=animal|hover_text=animal|> '
elif word == 'garden':
new_sentence += f'<|{word}|text|class_name=place|hover_text=place|>'
else:
new_sentence += word + ' '
state.text_annotation.update_content(state, new_sentence)
if __name__ == '__main__':
gui = Gui(md)
text_annotation = gui.add_partial('')
gui.run() main.css: .place{
color: rgb(255, 146, 63);
font-weight: bold;
}
.animal{
color: rgb(80, 182, 255);
font-weight: bold;
} |
Beta Was this translation helpful? Give feedback.
-
As you said, what is missing is the character selection. You could create an input to replace it or a multiple selector that has the words in the sentence as a list of values. Do you have a reference of a Python or JS library that has this feature? I tried to create a code to describe how you could do that in Taipy: In Taipy 3.0: from taipy.gui import Gui
import pandas as pd
sentences = pd.DataFrame({"sentences": ["The quick brown fox jumped over the lazy dog.",
"A cat in a hat.",
"The garden is full of flowers."]})
open_form = False
words = ''
sentence = ''
entity = ''
role = ''
group = ''
value = ''
info_line = {}
form_dialog="""
<|{open_form}|dialog|labels=Cancel; Save|title=Annotation|on_action=validate_help|
<|{sentence}|>
**Sentence**\n
<|{words}|input|>
**Entity**\n
<|{entity}|input|>
**Role**\n
<|{role}|input|>
**Group**\n
<|{group}|input|>
**Value**\n
<|{value}|input|>
|>
"""
md = form_dialog + "<|{sentences}|table|on_action=on_action_sentence|>"
def validate_help(state, action, payload):
# Click on Save
if payload['args'][0]==1:
temp = state.sentences
sentence = temp.loc[state.info_line['index'], state.info_line['col']]
new_words = f'[{state.words}]'+'{'+f'"entity": "{state.entity}", "role": "{state.role}", "group": "{state.group}", "value": "{state.value}"'+' }'
sentence = sentence.replace(state.words, new_words)
temp.loc[state.info_line['index'], state.info_line['col']] = sentence
state.sentences = temp
state.open_form = False
def on_action_sentence(state, name, payload):
state.info_line = payload
state.sentence = state.sentences.loc[state.info_line['index'], state.info_line['col']]
state.open_form = True
Gui(md).run() In Taipy 2.4 version: from taipy.gui import Gui
import pandas as pd
sentences = pd.DataFrame({"sentences": ["The quick brown fox jumped over the lazy dog.",
"A cat in a hat.",
"The garden is full of flowers."]})
open_form = False
words = ''
sentence = ''
entity = ''
role = ''
group = ''
value = ''
info_line = {}
form_dialog="""
<|{open_form}|dialog|labels=Cancel; Save|title=Annotation|on_action=validate_help|
<|{sentence}|>
**Sentence**\n
<|{words}|input|>
**Entity**\n
<|{entity}|input|>
**Role**\n
<|{role}|input|>
**Group**\n
<|{group}|input|>
**Value**\n
<|{value}|input|>
|>
"""
md = form_dialog + "<|{sentences}|table|on_action=on_action_sentence|>"
def validate_help(state, name, _, payload):
# Click on Save
if payload['args'][0]==1:
temp = state.sentences
sentence = temp.loc[state.info_line['index'], state.info_line['col']]
new_words = f'[{state.words}]'+'{'+f'"entity": "{state.entity}", "role": "{state.role}", "group": "{state.group}", "value": "{state.value}"'+' }'
sentence = sentence.replace(state.words, new_words)
temp.loc[state.info_line['index'], state.info_line['col']] = sentence
state.sentences = temp
state.open_form = False
def on_action_sentence(state, name, _, payload):
state.info_line = payload
state.info_line['col'] = 'sentences'
state.sentence = state.sentences.loc[state.info_line['index'], state.info_line['col']]
state.open_form = True
Gui(md).run() |
Beta Was this translation helpful? Give feedback.
-
Hello, in natural language processing projects it is often necessary to annotate text, e.g., mark entities in a text and then label them. Which of Taipy's features could I use to realize such an annotation case?
Beta Was this translation helpful? Give feedback.
All reactions