Skip to content

Commit a478a03

Browse files
committed
Malt Parser support
1 parent d43d890 commit a478a03

8 files changed

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

src/SyntaxTree/Command/TensorFlowCommand.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ class TensorFlowCommand extends AbstractParseSyntaxCommand implements CommandInt
66
{
77

88
protected $syntaxnetPath;
9-
109
protected $modelPath;
1110

12-
public function setSyntaxnetPath($syntaxnetPath) {
11+
public function setSyntaxnetPath($syntaxnetPath)
12+
{
1313
$this->syntaxnetPath = $syntaxnetPath;
1414
return $this;
1515
}
1616

17-
public function setModelPath($modelPath) {
17+
public function setModelPath($modelPath)
18+
{
1819
$this->modelPath = $modelPath;
1920
return $this;
2021
}
@@ -45,4 +46,5 @@ public function execute()
4546

4647
return $csv;
4748
}
49+
4850
}

src/api/Config/Configuration.php.tpl

+6
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ class Configuration extends DefaultConfiguration
1717
*/
1818
protected static $syntaxNetModelPath = '/home/tensor/tensorflow/Russian-SynTagRus';
1919
20+
/**
21+
* Path to MaltParser (directory with russian-malt.sh)
22+
* @var string
23+
*/
24+
protected static $maltParserPath = '/home/tensor/malt/installation';
25+
2026
}

src/api/Config/ConfigurationInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ interface ConfigurationInterface
88
public static function getSyntaxNetPath();
99

1010
public static function getSyntaxNetModelPath();
11+
12+
public static function getMaltParserPath();
1113
}

src/api/Config/DefaultConfiguration.php

+5
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public static function getSyntaxNetModelPath()
1818
return static::$syntaxNetModelPath;
1919
}
2020

21+
public static function getMaltParserPath()
22+
{
23+
return static::$maltParserPath;
24+
}
25+
2126
}

src/api/Controller/Controller.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public function process($data)
1616

1717
$option = $data['option'] ?? null;
1818

19-
$command = ParseSyntaxCommandFactory::createTensorFlowCommand()
19+
$system = $data['system'] ?? null;
20+
21+
$command = ParseSyntaxCommandFactory::create($system)
2022
->setText($text);
2123
$csv = $command->execute();
2224

src/api/Model/ParseSyntaxCommandFactory.php

+27
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,28 @@
33
namespace SyntaxTreeApi\Model;
44

55
use SyntaxTree\Command\TensorFlowCommand;
6+
use SyntaxTree\Command\MaltParserCommand;
67
use SyntaxTreeApi\Config\Configuration;
78

89
class ParseSyntaxCommandFactory
910
{
11+
12+
const SYSTEM_TENSOR_FLOW = 'TensorFlow';
13+
const SYSTEM_MALT_PARSER = 'MaltParser';
14+
15+
public static function create($system = self::SYSTEM_TENSOR_FLOW)
16+
{
17+
switch ($system)
18+
{
19+
case self::SYSTEM_TENSOR_FLOW:
20+
return self::createTensorFlowCommand();
21+
case self::SYSTEM_MALT_PARSER:
22+
return self::createMaltParserCommand();
23+
default:
24+
throw new Exception(sprintf('Unknown system: %s', $system));
25+
}
26+
}
27+
1028
public static function createTensorFlowCommand()
1129
{
1230
$command = new TensorFlowCommand();
@@ -15,4 +33,13 @@ public static function createTensorFlowCommand()
1533
->setSyntaxnetPath(Configuration::getSyntaxNetPath())
1634
->setModelPath(Configuration::getSyntaxNetModelPath());
1735
}
36+
37+
public static function createMaltParserCommand()
38+
{
39+
$command = new MaltParserCommand();
40+
41+
return $command
42+
->setPath(Configuration::getMaltParserPath());
43+
}
44+
1845
}

src/api/view/index.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@
4646
</div>
4747
</div>
4848

49+
<div class="row">
50+
<div class="col-md-1">
51+
<div class="form-group">
52+
<label for="format">System</label>
53+
</div>
54+
</div>
55+
<div class="col-md-2">
56+
<div class="form-group">
57+
<select class="form-control" id="system" name="system">
58+
<option>TensorFlow</option>
59+
<option>MaltParser</option>
60+
</select>
61+
</div>
62+
</div>
63+
</div>
64+
4965
<div class="row">
5066
<div class="col-md-2">
5167
<button type="submit" class="btn btn-primary">Submit</button>
@@ -71,7 +87,7 @@
7187
<script src="vendor/d3/d3.min.js"></script>
7288
<script src="js/syntax-tree.js"></script>
7389
<script>
74-
var data = JSON.parse('<?php echo $trees[0]->toJson(); ?>');
90+
var data = JSON.parse('<?php echo $trees[0] ? $trees[0]->toJson() : 'null'; ?>');
7591
buildSyntaxTree(data);
7692
</script>
7793

0 commit comments

Comments
 (0)