processors.visualization   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 79
dl 0
loc 104
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A JupyterVisualizer.display_graph() 0 4 1
A JupyterVisualizer.graph_to_html() 0 19 2
B JupyterVisualizer.mention_to_html() 0 45 5
A JupyterVisualizer.display_mention() 0 4 1
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3
from itertools import count
4
from IPython.core.display import display, HTML
5
import os
6
7
8
class JupyterVisualizer(object):
9
    """
10
    Widgets for use with jupyter notebook
11
    """
12
13
    ASSETS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets")
14
15
    with open(os.path.join(ASSETS_DIR, "displacy-processors.js")) as js_file:
16
        dp_lib = js_file.read()
17
    with open(os.path.join(ASSETS_DIR, "displacy-processors.html")) as html_file:
18
        base_contents = html_file.read()
19
    with open(os.path.join(ASSETS_DIR, "displacy-processors.css")) as css_file:
20
        base_css = css_file.read()
21
    with open(os.path.join(ASSETS_DIR, "mentions.css")) as css_file:
22
        mentions_css = css_file.read()
23
    # style loosely corresponding to mention highlighting
24
    with open(os.path.join(ASSETS_DIR, "parse.css")) as css_file:
25
        parse_css = css_file.read()
26
27
    _id_gen = count(start=0, step=1)
28
29
    @staticmethod
30
    def graph_to_html(s, graph_name="stanford-collapsed", css=None, distance=None, div_id=None):
31
        distance = distance or int((sum(len(w) for w in s.words) + s.length) * 1.75)
32
33
        def next_id(): return next(JupyterVisualizer._id_gen)
34
        nid = next_id()
35
        div_id = div_id or "graph_{}".format(nid)
36
37
        # apply css only to current viz
38
        custom_css = css.replace(".displacy", "#{} .displacy".format(div_id)) if css else ""
39
        html = JupyterVisualizer.base_contents.format(
40
            dp_lib=JupyterVisualizer.dp_lib,
41
            dist=distance,
42
            sent_json=s.to_JSON(),
43
            div_id=div_id,
44
            css=custom_css,
45
            gn=graph_name
46
        )
47
        return html
48
49
    @staticmethod
50
    def display_graph(s, graph_name="stanford-collapsed", css=None, distance=None, div_id=None):
51
        res = JupyterVisualizer.graph_to_html(s=s, graph_name=graph_name, css=css, distance=distance, div_id=div_id)
52
        display(HTML(data=res))
53
54
    @staticmethod
55
    def mention_to_html(mention):
56
        SENTENCE_BOS = """<span class="sentence">"""
57
        MENTION_LABEL = """<sub class="mention-label">{}</sub>"""
58
        MENTION_SPAN_BOS = """<span class="mention-span sentence">"""
59
        ARG_BOS = """<span class="mention-arg mention-span sentence">"""
60
        TRIGGER_BOS = """<span class="mention-trigger mention-span sentence">"""
61
        EOS = "</span>"
62
63
        def add_label(label):
64
            return """<sup class="mention-role">{}</sup>""".format(label)
65
66
        def start_span(tag, w):
67
            return "{}{}".format(tag, w)
68
        def end_span(w, tag=""):
69
            return "{}{}{}".format(w,tag, EOS)
70
71
        sent = mention.sentenceObj
72
        sent_span = [w for w in sent.words]
73
        # mention trigger
74
        if mention.trigger:
75
            start = mention.trigger.start
76
            end = mention.trigger.end - 1
77
            sent_span[start] = start_span(TRIGGER_BOS, sent_span[start])
78
            sent_span[end] = end_span(sent_span[end],tag=add_label("TRIGGER"))
79
        # mention args
80
        if mention.arguments:
81
            for (role, args) in mention.arguments.items():
82
                for arg in args:
83
                    start  = arg.start
84
                    end = arg.end - 1
85
                    sent_span[start] = start_span(ARG_BOS + MENTION_LABEL.format(arg.label), sent_span[start])
86
                    sent_span[end] = end_span(sent_span[end], tag=add_label(role))
87
        # mention span
88
        start = mention.start
89
        end = mention.end - 1
90
        sent_span[start] = start_span(MENTION_SPAN_BOS + MENTION_LABEL.format(mention.label), sent_span[start])
91
        sent_span[end] = end_span(sent_span[end])
92
        # sentence tag
93
        start = 0
94
        end = -1
95
        sent_span[start] = start_span(SENTENCE_BOS, sent_span[start])
96
        sent_span[end] = end_span(sent_span[end])
97
        html = " ".join(sent_span)
98
        return """<style>{css}</style>{mention_html}""".format(css=JupyterVisualizer.mentions_css, mention_html=html)
99
100
    @staticmethod
101
    def display_mention(mention):
102
        res = JupyterVisualizer.mention_to_html(mention)
103
        display(HTML(res))
104