Skip to content

Commit 6b0f1e8

Browse files
committed
api, refactoring
1 parent d1eaed5 commit 6b0f1e8

File tree

10 files changed

+337
-278
lines changed

10 files changed

+337
-278
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
},
1717
"autoload": {
1818
"psr-4": {
19-
"SyntaxTree\\": "src/SyntaxTree"
19+
"SyntaxTree\\": "src/SyntaxTree",
20+
"SyntaxTreeApi\\": "src/api"
2021
}
2122
},
2223
"scripts": {

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/index.php

Lines changed: 2 additions & 232 deletions
Original file line numberDiff line numberDiff line change
@@ -1,239 +1,9 @@
1-
<!DOCTYPE html>
2-
<html lang="en">
3-
4-
<title>Syntax tree</title>
5-
<meta charset="utf-8">
6-
<meta name="viewport" content="width=device-width, initial-scale=1">
7-
<link rel="stylesheet" href="vendor/bootstrap/css/bootstrap.min.css">
8-
<script src="vendor/d3/d3.min.js"></script>
9-
10-
<link rel="stylesheet" href="css/style.css">
11-
12-
<body>
13-
14-
<div class="container-fluid">
15-
16-
<h1>Parser of syntax of russian sentences</h1>
17-
18-
<form method="post">
19-
<div class="row">
20-
<div class="col-md-1">
21-
<div class="form-group">
22-
<label for="text">Text</label>
23-
</div>
24-
</div>
25-
<div class="col-md-6">
26-
<div class="form-group">
27-
<textarea class="form-control" id="text" name="text" placeholder="Sentence"></textarea>
28-
</div>
29-
</div>
30-
<div class="col-md-2">
31-
<button type="submit" class="btn btn-primary">Submit</button>
32-
</div>
33-
</div>
34-
</form>
35-
36-
<hr>
37-
38-
391
<?php
402

413
ini_set('display_errors', TRUE);
424
error_reporting(-1);
435

446
require '../vendor/autoload.php';
457

46-
$text = $_POST['text'] ?? null;
47-
48-
echo '<p><label>Text:</label> ';
49-
echo "$text</p>";
50-
51-
$command = "syntaxnet/models/parsey_universal/parse.sh /home/tensor/tensorflow/Russian-SynTagRus";
52-
$path = '/home/tensor/tensorflow/models/syntaxnet/';
53-
54-
$descriptors = array(
55-
0 => array('pipe', 'r'), // stdin
56-
1 => array('pipe', 'w'), // stdout
57-
2 => array('pipe', 'w') // stderr
58-
);
59-
60-
// ! Sure what ~/.cache/bazel is acceseble for www-data !
61-
$process = proc_open($command, $descriptors, $pipes, $path);
62-
63-
//echo '<p><label>Command:</label></p>';
64-
//echo "<p>$command</p>";
65-
66-
if (is_resource($process))
67-
{
68-
69-
fwrite($pipes[0], $text);
70-
fclose($pipes[0]);
71-
72-
$csv = stream_get_contents($pipes[1]);
73-
74-
$return_value = proc_close($process);
75-
// echo '<p><label>Val:</label></p>';
76-
// echo "<p>$return_value</p>";
77-
}
78-
79-
$syntaxTree = new \SyntaxTree\SyntaxTree();
80-
$tree = $syntaxTree->build($csv);
81-
82-
?>
83-
84-
<p><label>Processed response:</label></p>
85-
86-
<div id="tree"></div>
87-
88-
<script>
89-
90-
var margin = {top: 20, right: 120, bottom: 20, left: 120},
91-
width = window.innerWidth - margin.right - margin.left,
92-
height = 800 - margin.top - margin.bottom
93-
;
94-
95-
var i = 0,
96-
duration = 750,
97-
root;
98-
99-
var tree = d3.layout.tree()
100-
.size([height, width]);
101-
102-
var diagonal = d3.svg.diagonal()
103-
.projection(function(d) { return [d.y, d.x]; });
104-
105-
var svg = d3.select("#tree").append("svg")
106-
.attr("width", width + margin.right + margin.left)
107-
.attr("height", height + margin.top + margin.bottom)
108-
.append("g")
109-
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
110-
111-
var data = JSON.parse('<?php echo $tree->toJson(); ?>');
112-
113-
console.log(data);
114-
root = data[0];
115-
root.x0 = height / 2;
116-
root.y0 = 0;
117-
118-
function collapse(d) {
119-
if (d.children) {
120-
d._children = d.children;
121-
d._children.forEach(collapse);
122-
d.children = null;
123-
}
124-
}
125-
126-
update(root);
127-
128-
d3.select(self.frameElement).style("height", "800px");
129-
130-
function update(source) {
131-
132-
// Compute the new tree layout.
133-
var nodes = tree.nodes(root).reverse(),
134-
links = tree.links(nodes);
135-
136-
// Normalize for fixed-depth.
137-
nodes.forEach(function(d) { d.y = d.depth * 180; });
138-
139-
// Update the nodes…
140-
var node = svg.selectAll("g.node")
141-
.data(nodes, function(d) { return d.id || (d.id = ++i); });
142-
143-
// Enter any new nodes at the parent's previous position.
144-
var nodeEnter = node.enter().append("g")
145-
.attr("class", "node")
146-
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
147-
.on("click", click);
148-
149-
nodeEnter.append("circle")
150-
.attr("r", 1e-6)
151-
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
152-
153-
nodeEnter.append("text")
154-
.attr("x", function(d) { return d.children || d._children ? -10 : 10; })
155-
.attr("dy", ".35em")
156-
.attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
157-
.text(function(d) { return d.text; })
158-
.style("fill-opacity", 1e-6);
159-
160-
// Transition nodes to their new position.
161-
var nodeUpdate = node.transition()
162-
.duration(duration)
163-
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
164-
165-
nodeUpdate.select("circle")
166-
.attr("r", 4.5)
167-
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
168-
169-
nodeUpdate.select("text")
170-
.style("fill-opacity", 1);
171-
172-
// Transition exiting nodes to the parent's new position.
173-
var nodeExit = node.exit().transition()
174-
.duration(duration)
175-
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
176-
.remove();
177-
178-
nodeExit.select("circle")
179-
.attr("r", 1e-6);
180-
181-
nodeExit.select("text")
182-
.style("fill-opacity", 1e-6);
183-
184-
// Update the links…
185-
var link = svg.selectAll("path.link")
186-
.data(links, function(d) { return d.target.id; });
187-
188-
// Enter any new links at the parent's previous position.
189-
link.enter().insert("path", "g")
190-
.attr("class", "link")
191-
.attr("d", function(d) {
192-
var o = {x: source.x0, y: source.y0};
193-
return diagonal({source: o, target: o});
194-
});
195-
196-
// Transition links to their new position.
197-
link.transition()
198-
.duration(duration)
199-
.attr("d", diagonal);
200-
201-
// Transition exiting nodes to the parent's new position.
202-
link.exit().transition()
203-
.duration(duration)
204-
.attr("d", function(d) {
205-
var o = {x: source.x, y: source.y};
206-
return diagonal({source: o, target: o});
207-
})
208-
.remove();
209-
210-
// Stash the old positions for transition.
211-
nodes.forEach(function(d) {
212-
d.x0 = d.x;
213-
d.y0 = d.y;
214-
});
215-
}
216-
217-
// Toggle children on click.
218-
function click(d) {
219-
if (d.children) {
220-
d._children = d.children;
221-
d.children = null;
222-
} else {
223-
d.children = d._children;
224-
d._children = null;
225-
}
226-
update(d);
227-
}
228-
229-
</script>
230-
231-
<?php
232-
echo '<p><label>Response &mdash; <a href="http://ilk.uvt.nl/conll/#dataformat">CoNLL-X</a> (CSV):</label></p>';
233-
echo "<pre>$csv</pre>";
234-
?>
235-
236-
</div>
237-
238-
</body>
239-
</html>
8+
$controller = new \SyntaxTreeApi\Controller\Controller();
9+
$controller->process($_POST);

0 commit comments

Comments
 (0)