-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
69 lines (53 loc) · 1.73 KB
/
example.php
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
<?php
class TomitaParser
{
/**
* @var string Path to Yandex`s Tomita-parser binary
*/
protected $execPath;
/**
* @var string Path to Yandex`s Tomita-parser configuration file
*/
protected $configPath;
/**
* @param string $execPath Path to Yandex`s Tomita-parser binary
* @param string $configPath Path to Yandex`s Tomita-parser configuration file
*/
public function __construct($execPath, $configPath)
{
$this->execPath = $execPath;
$this->configPath = $configPath;
}
public function run($text)
{
$descriptors = array(
0 => array('pipe', 'r'), // stdin
1 => array('pipe', 'w'), // stdout
2 => array('pipe', 'w') // stderr
);
$cmd = sprintf('%s %s', $this->execPath, $this->configPath);
$process = proc_open($cmd, $descriptors, $pipes, dirname($this->configPath));
if (is_resource($process))
{
fwrite($pipes[0], $text);
fclose($pipes[0]);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
return $this->processTextResult($output);
}
throw new \Exception('proc_open fails');
}
/**
* Обработка текстового результата
* @param string $text
* @return string[]
*/
public function processTextResult($text)
{
return array_filter(explode("\n", $text));
}
}
$parser = new TomitaParser('/home/mnv/tmp/tomita/tomita-linux64', '/home/mnv/tmp/tomita/config.proto');
var_dump($parser->run('Предложение раз. Предложение два.'));