ArneBinder commited on
Commit
54625d7
1 Parent(s): bc6f57a

use rendering utils

Browse files
Files changed (1) hide show
  1. app.py +3 -53
app.py CHANGED
@@ -11,61 +11,11 @@ from pytorch_ie.documents import TextDocumentWithLabeledSpansBinaryRelationsAndL
11
  from pytorch_ie.models import * # noqa: F403
12
  from pytorch_ie.taskmodules import * # noqa: F403
13
 
14
- RENDER_WITH_DISPLACY = "displaCy + highlighted arguments"
15
- RENDER_WITH_PRETTY_TABLE = "Pretty Table"
16
-
17
-
18
- def render_pretty_table(
19
- document: TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions, **render_kwargs
20
- ):
21
- from prettytable import PrettyTable
22
-
23
- t = PrettyTable()
24
- t.field_names = ["head", "tail", "relation"]
25
- t.align = "l"
26
- for relation in list(document.binary_relations) + list(document.binary_relations.predictions):
27
- t.add_row([str(relation.head), str(relation.tail), relation.label])
28
-
29
- html = t.get_html_string(format=True)
30
- html = "<div style='max-width:100%; max-height:360px; overflow:auto'>" + html + "</div>"
31
-
32
- return html
33
 
34
 
35
- def render_spacy(
36
- document: TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions,
37
- style="ent",
38
- inject_relations=True,
39
- colors_hover=None,
40
- **render_kwargs,
41
- ):
42
- from spacy import displacy
43
-
44
- spans = list(document.labeled_spans) + list(document.labeled_spans.predictions)
45
- spacy_doc = {
46
- "text": document.text,
47
- "ents": [
48
- {"start": entity.start, "end": entity.end, "label": entity.label} for entity in spans
49
- ],
50
- "title": None,
51
- }
52
-
53
- html = displacy.render(
54
- spacy_doc, page=True, manual=True, minify=True, style=style, **render_kwargs
55
- )
56
- html = "<div style='max-width:100%; max-height:360px; overflow:auto'>" + html + "</div>"
57
- if inject_relations:
58
- binary_relations = list(document.binary_relations) + list(
59
- document.binary_relations.predictions
60
- )
61
- sorted_entities = sorted(spans, key=lambda x: (x.start, x.end))
62
- html = inject_relation_data(
63
- html,
64
- sorted_entities=sorted_entities,
65
- binary_relations=binary_relations,
66
- additional_colors=colors_hover,
67
- )
68
- return html
69
 
70
 
71
  def inject_relation_data(
 
11
  from pytorch_ie.models import * # noqa: F403
12
  from pytorch_ie.taskmodules import * # noqa: F403
13
 
14
+ from .rendering_utils import render_pretty_table, render_spacy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
 
17
+ RENDER_WITH_DISPLACY = "displaCy + highlighted arguments"
18
+ RENDER_WITH_PRETTY_TABLE = "Pretty Table"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
 
21
  def inject_relation_data(