Skip to content

Commit 08b715a

Browse files
committed
command
1 parent 6b0f1e8 commit 08b715a

File tree

5 files changed

+95
-23
lines changed

5 files changed

+95
-23
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace SyntaxTree\Command;
4+
5+
abstract class AbstractParseSyntaxCommand implements CommandInterface
6+
{
7+
8+
protected $text;
9+
10+
public function setText($text)
11+
{
12+
$this->text = $text;
13+
return $this;
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace SyntaxTree\Command;
4+
5+
interface CommandInterface
6+
{
7+
8+
public function execute();
9+
10+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace SyntaxTree\Command;
4+
5+
class TensorFlowCommand extends AbstractParseSyntaxCommand implements CommandInterface
6+
{
7+
8+
protected $syntaxnetPath;
9+
10+
protected $modelPath;
11+
12+
public function setSyntaxnetPath($syntaxnetPath) {
13+
$this->syntaxnetPath = $syntaxnetPath;
14+
return $this;
15+
}
16+
17+
public function setModelPath($modelPath) {
18+
$this->modelPath = $modelPath;
19+
return $this;
20+
}
21+
22+
public function execute()
23+
{
24+
$command = "syntaxnet/models/parsey_universal/parse.sh " . $this->modelPath;
25+
26+
$descriptors = [
27+
0 => ['pipe', 'r'], // stdin
28+
1 => ['pipe', 'w'], // stdout
29+
2 => ['pipe', 'w'], // stderr
30+
];
31+
32+
// ! Sure what ~/.cache/bazel is acceseble for www-data !
33+
$process = proc_open($command, $descriptors, $pipes, $this->syntaxnetPath);
34+
35+
if (is_resource($process))
36+
{
37+
38+
fwrite($pipes[0], $this->text);
39+
fclose($pipes[0]);
40+
41+
$csv = stream_get_contents($pipes[1]);
42+
43+
proc_close($process);
44+
}
45+
46+
return $csv;
47+
}
48+
}

src/api/Controller/Controller.php

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace SyntaxTreeApi\Controller;
44

5+
use SyntaxTreeApi\Model\ParseSyntaxCommandFactory;
6+
57
class Controller
68
{
79

@@ -14,29 +16,9 @@ public function process($data)
1416

1517
$option = $data['option'] ?? null;
1618

17-
18-
$command = "syntaxnet/models/parsey_universal/parse.sh /home/tensor/tensorflow/Russian-SynTagRus";
19-
$path = '/home/tensor/tensorflow/models/syntaxnet/';
20-
21-
$descriptors = [
22-
0 => ['pipe', 'r'], // stdin
23-
1 => ['pipe', 'w'], // stdout
24-
2 => ['pipe', 'w'], // stderr
25-
];
26-
27-
// ! Sure what ~/.cache/bazel is acceseble for www-data !
28-
$process = proc_open($command, $descriptors, $pipes, $path);
29-
30-
if (is_resource($process))
31-
{
32-
33-
fwrite($pipes[0], $text);
34-
fclose($pipes[0]);
35-
36-
$csv = stream_get_contents($pipes[1]);
37-
38-
proc_close($process);
39-
}
19+
$command = ParseSyntaxCommandFactory::createTensorFlowCommand()
20+
->setText($text);
21+
$csv = $command->execute();
4022

4123
$syntaxTree = new \SyntaxTree\SyntaxTree();
4224
$tree = $syntaxTree->build($csv);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace SyntaxTreeApi\Model;
4+
5+
use SyntaxTree\Command\TensorFlowCommand;
6+
7+
class ParseSyntaxCommandFactory
8+
{
9+
public static function createTensorFlowCommand()
10+
{
11+
$command = new TensorFlowCommand();
12+
13+
return $command
14+
->setSyntaxnetPath('/home/tensor/tensorflow/models/syntaxnet/')
15+
->setModelPath('/home/tensor/tensorflow/Russian-SynTagRus');
16+
}
17+
}

0 commit comments

Comments
 (0)