Skip to content

Commit 7df475a

Browse files
committed
Processing of several sentences per one request
1 parent 2aa8219 commit 7df475a

File tree

5 files changed

+121
-8
lines changed

5 files changed

+121
-8
lines changed

src/SyntaxTree/ConllXParserFactory.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,45 @@ class ConllXParserFactory
1111
* @param string $delimiter
1212
* @param string $enclosure
1313
* @param string $escape
14-
* @return Tree
14+
* @return TreeCollection
1515
*/
1616
public static function create($csv, $delimiter = "\t", $enclosure = '"', $escape = "\\" )
1717
{
1818
$strings = explode("\n", $csv);
19+
$sentences = [];
1920
$array = [];
2021

2122
foreach ($strings as $string)
2223
{
2324
if (!$string)
2425
{
26+
$sentences[] = $array;
27+
$array = [];
2528
continue;
2629
}
2730
$string = str_replace('"', '\\"', $string);
2831
$array[] = str_getcsv($string, $delimiter, $enclosure, $escape);
2932
}
30-
$nodes = static::createNodes($array);
33+
if ($array)
34+
{
35+
$sentences[] = $array;
36+
}
37+
38+
return static::createTrees($sentences);
39+
}
40+
41+
protected static function createTrees(array $sentences)
42+
{
43+
$trees = [];
44+
foreach ($sentences as $array)
45+
{
46+
$trees[] = new Tree(static::createNodes($array));
47+
}
3148

32-
return new Tree($nodes);
49+
return new TreeCollection($trees);
3350
}
3451

35-
protected static function createNodes(&$array, $rootIndex = 0)
52+
protected static function createNodes(array &$array, $rootIndex = 0)
3653
{
3754
$data = [];
3855
foreach ($array as $item)

src/SyntaxTree/SyntaxTree.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public function setEscape($escape)
6161
return $this;
6262
}
6363

64+
/**
65+
* Build syntax tree for every sentence
66+
* @param string $source
67+
* @return Tree[]
68+
*/
6469
public function build($source)
6570
{
6671
switch ($this->system)

src/SyntaxTree/TreeCollection.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace SyntaxTree;
4+
5+
use ArrayAccess;
6+
use Iterator;
7+
8+
class TreeCollection implements Iterator, ArrayAccess
9+
{
10+
11+
/**
12+
* @var Tree[]
13+
*/
14+
protected $trees;
15+
protected $position = 0;
16+
17+
public function __construct($trees)
18+
{
19+
$this->trees = $trees;
20+
}
21+
22+
function rewind()
23+
{
24+
$this->position = 0;
25+
}
26+
27+
function current()
28+
{
29+
return $this->trees[$this->position];
30+
}
31+
32+
function key()
33+
{
34+
return $this->position;
35+
}
36+
37+
function next()
38+
{
39+
++$this->position;
40+
}
41+
42+
function valid()
43+
{
44+
return isset($this->trees[$this->position]);
45+
}
46+
47+
public function offsetSet($offset, $value)
48+
{
49+
if (is_null($offset))
50+
{
51+
$this->trees[] = $value;
52+
}
53+
else
54+
{
55+
$this->trees[$offset] = $value;
56+
}
57+
}
58+
59+
public function offsetExists($offset)
60+
{
61+
return isset($this->trees[$offset]);
62+
}
63+
64+
public function offsetUnset($offset)
65+
{
66+
unset($this->trees[$offset]);
67+
}
68+
69+
public function offsetGet($offset)
70+
{
71+
return isset($this->trees[$offset]) ? $this->trees[$offset] : null;
72+
}
73+
74+
public function toArray()
75+
{
76+
$array = [];
77+
78+
foreach ($this->trees as $tree)
79+
{
80+
$array[] = $tree->toArray();
81+
}
82+
83+
return $array;
84+
}
85+
86+
public function toJson($options = 0)
87+
{
88+
return json_encode($this->toArray(), $options);
89+
}
90+
91+
}

src/api/Controller/Controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function process($data)
2121
$csv = $command->execute();
2222

2323
$syntaxTree = new \SyntaxTree\SyntaxTree();
24-
$tree = $syntaxTree->build($csv);
24+
$trees = $syntaxTree->build($csv);
2525

2626
switch ($format)
2727
{
@@ -31,7 +31,7 @@ public function process($data)
3131

3232
case 'JSON':
3333
$options = $option === 'JSON_PRETTY_PRINT' ? JSON_PRETTY_PRINT : 0;
34-
echo $tree->toJson($options);
34+
echo $trees->toJson($options);
3535
break;
3636

3737
default:

src/api/view/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@
6565
<pre><?php echo $csv ?></pre>
6666

6767
<p><label>Json:</label></p>
68-
<pre><?php echo $tree->toJson(JSON_PRETTY_PRINT); ?></pre>
68+
<pre><?php echo $trees->toJson(JSON_PRETTY_PRINT); ?></pre>
6969
</div>
7070

7171
<script src="vendor/d3/d3.min.js"></script>
7272
<script src="js/syntax-tree.js"></script>
7373
<script>
74-
var data = JSON.parse('<?php echo $tree->toJson(); ?>');
74+
var data = JSON.parse('<?php echo $trees[0]->toJson(); ?>');
7575
buildSyntaxTree(data);
7676
</script>
7777

0 commit comments

Comments
 (0)