kleinay commited on
Commit
45fd487
1 Parent(s): 4e56234

add tooltip to output html

Browse files
Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -11,9 +11,9 @@ description = f"""This is a demo of QANom's nominalization detection algorithm,
11
  links = """<p style='text-align: center'>
12
  <a href='https://github.com/kleinay/QANom' target='_blank'>QANom repo</a> |
13
  <a href='https://huggingface.co/kleinay/nominalization-candidate-classifier' target='_blank'>Model Repo at Huggingface Hub</a> |
14
- <a href='https://www.aclweb.org/anthology/2020.coling-main.274/' target='_blank'>QANom Paper</a> |
15
  </p>"""
16
- examples = [["The doctor was interested in Luke 's treatment .", True, 0.6],
17
  ["the construction of the officer 's building finished right after the beginning of the destruction of the previous construction .", True, 0.7]]
18
 
19
  def call(sentence: str, return_all_candidates: bool, threshold: float):
@@ -27,9 +27,11 @@ def call(sentence: str, return_all_candidates: bool, threshold: float):
27
  def color(idx):
28
  if idx in positives: return "lightgreen"
29
  if idx in negatives: return "pink"
30
- highlighted = [(word, color(idx)) for idx, word in enumerate(sentence.split(" "))]
 
31
  def word_span(word, idx):
32
- return f'<span style="background-color: {color(idx)}">{word}</span>'
 
33
  html = '<span>' + ' '.join(word_span(word, idx) for idx, word in enumerate(sentence.split(" "))) + '</span>'
34
  return html, ret
35
 
11
  links = """<p style='text-align: center'>
12
  <a href='https://github.com/kleinay/QANom' target='_blank'>QANom repo</a> |
13
  <a href='https://huggingface.co/kleinay/nominalization-candidate-classifier' target='_blank'>Model Repo at Huggingface Hub</a> |
14
+ <a href='https://www.aclweb.org/anthology/2020.coling-main.274/' target='_blank'>QANom Paper</a>
15
  </p>"""
16
+ examples = [["The doctor was interested in Luke 's treatment .", True, 0.5],
17
  ["the construction of the officer 's building finished right after the beginning of the destruction of the previous construction .", True, 0.7]]
18
 
19
  def call(sentence: str, return_all_candidates: bool, threshold: float):
27
  def color(idx):
28
  if idx in positives: return "lightgreen"
29
  if idx in negatives: return "pink"
30
+ idx2verb = {d["predicate_idx"] : d["verb_form"] for d in ret}
31
+ idx2prob = {d["predicate_idx"] : d["predicate_detector_probability"] for d in ret}
32
  def word_span(word, idx):
33
+ tooltip = f'title=" probability={idx2prob[idx]:.2}&#010;verb={idx2verb[idx]}"' if idx in idx2verb else ''
34
+ return f'<span {tooltip} style="background-color: {color(idx)}">{word}</span>'
35
  html = '<span>' + ' '.join(word_span(word, idx) for idx, word in enumerate(sentence.split(" "))) + '</span>'
36
  return html, ret
37