interactive_text_analyzer.ipynb 4.85 KB
Newer Older
Ivan Bogatyy's avatar
Ivan Bogatyy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\n",
    "import tensorflow as tf\n",
    "import uuid\n",
    "from IPython import display\n",
    "from dragnn.protos import spec_pb2\n",
    "from dragnn.python import graph_builder\n",
    "from dragnn.python import load_dragnn_cc_impl  # This loads the actual op definitions\n",
    "from dragnn.python import render_parse_tree_graphviz\n",
    "from dragnn.python import visualization\n",
    "from google.protobuf import text_format\n",
    "from syntaxnet import load_parser_ops  # This loads the actual op definitions\n",
    "from syntaxnet import sentence_pb2\n",
    "from tensorflow.python.platform import tf_logging as logging\n",
    "\n",
    "# Read the master spec\n",
    "master_spec = spec_pb2.MasterSpec()\n",
    "with open(\"data/master_spec_es.prototext\", \"r\") as f:\n",
    "    text_format.Merge(f.read(), master_spec)\n",
    "\n",
    "logging.set_verbosity(logging.WARN)  # Turn off TensorFlow spam.\n",
    "\n",
    "# Initialize a graph\n",
    "graph = tf.Graph()\n",
    "with graph.as_default():\n",
    "    hyperparam_config = spec_pb2.GridPoint()\n",
    "    builder = graph_builder.MasterBuilder(master_spec, hyperparam_config)\n",
    "    # This is the component that will annotate test sentences.\n",
    "    annotator = builder.add_annotation(enable_tracing=True)\n",
    "    builder.add_saver()  # \"Savers\" can save and load models; here, we're only going to load."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sess = tf.Session(graph=graph)\n",
    "with graph.as_default():\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    sess.run('save/restore_all', {'save/Const:0': \"data/mini-spanish.checkpoint\"})\n",
    "\n",
    "def annotate_text(text):\n",
    "    sentence = sentence_pb2.Sentence(\n",
    "        token=[sentence_pb2.Token(word=word, start=-1, end=-1) for word in text.split()]\n",
    "    )\n",
    "    with graph.as_default():\n",
    "        annotations, traces = sess.run([annotator['annotations'], annotator['traces']],\n",
    "                                       feed_dict={annotator['input_batch']: [sentence.SerializeToString()]})\n",
    "        assert len(annotations) == 1\n",
    "        assert len(traces) == 1\n",
    "        return sentence_pb2.Sentence.FromString(annotations[0]), traces[0]\n",
    "annotate_text(\"casa\"); None  # just make sure it works"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Interactive trace explorer\n",
    "Run the cell below, and then enter text in the interactive widget."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def _trace_explorer():  # put stuff in a function to not pollute global scope\n",
    "    text = widgets.Text()\n",
    "    display.display(text)\n",
    "\n",
    "    output = visualization.InteractiveVisualization()\n",
    "    display.display(display.HTML(output.initial_html()))\n",
    "\n",
    "    def handle_submit(sender):\n",
    "        del sender  # unused\n",
    "        parse_tree, trace = annotate_text(text.value)\n",
    "        display.display(display.HTML(output.show_trace(trace)))\n",
    "\n",
    "\n",
    "    text.on_submit(handle_submit)\n",
    "_trace_explorer()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Interactive parse tree explorer\n",
    "Run the cell below, and then enter text in the interactive widget."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def _parse_tree_explorer():  # put stuff in a function to not pollute global scope\n",
    "    text = widgets.Text()\n",
    "    display.display(text)\n",
    "    html = widgets.HTML()\n",
    "    display.display(html)\n",
    "\n",
    "    def handle_submit(sender):\n",
    "        del sender  # unused\n",
    "        parse_tree, trace = annotate_text(text.value)\n",
    "        html.value = render_parse_tree_graphviz.parse_tree_graph(parse_tree)\n",
    "\n",
    "    text.on_submit(handle_submit)\n",
    "_parse_tree_explorer()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}