-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseSentance.php
221 lines (190 loc) · 7.23 KB
/
parseSentance.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
header('Content-type: text/html; charset=utf-8');
$rezs['SHIFT'] = $rezs['REDUCE'] = $rezs['LEFT ARC'] = $rezs['RIGHT ARC'] = 0;
//teikums
if(!isset($_GET['sentence'])){
echo "<form action='?' method='get'>
Teikums: <input type='text' name='sentence'><br/>
<input type='submit' value='Aiziet!'>
</form>";
}else{
echo "<form action='?' method='get'>
Teikums: <input type='text' name='sentence'><br/>
<input type='submit' value='Aiziet!'>
</form>";
$sentence = $_GET['sentence'];
//lēmumu koks
$file = fopen("training.json", "r");
$json = fgets($file);
$tree = json_decode($json, true);
$buffer = $arcz = $moves = $currentMoves = array();
$stack = new SplDoublyLinkedList();
$buffer = explode(" ",$sentence);
array_walk($buffer, function(&$item) { static $i=1; $item = $i++."_".$item; });
nivre(serialize($stack), $buffer, $arcz, serialize($moves));
//rezultāti
$finalMoves = unserialize($currentMoves);
$finalArcs = $finalMoves[count($finalMoves)-1][3];
if($finalMoves[count($finalMoves)-1][1]->count()==2)$finalMoves[count($finalMoves)-1][1]->pop();
//teikuma galvenais elements
if(!$finalMoves[count($finalMoves)-1][1]->isEmpty()) $lastWord = $finalMoves[count($finalMoves)-1][1]->top();
if(count($finalMoves[count($finalMoves)-1][2])>0) $lastWord = $finalMoves[count($finalMoves)-1][2][0];
//rezultējošās bultas
foreach($finalArcs as $finalArc){
$wordNumbers = explode("→",$finalArc);
$numberOne = explode("_",$wordNumbers[0])[0];
$numberTwo = explode("_",$wordNumbers[1])[0];
$finalRez[] = $numberTwo."-".$numberOne;
}
$finalRez[] = explode("_",$lastWord)[0]."-0";
sort($finalRez);
var_dump($finalRez);
}
//oriģinālais arc-eager shift-reduce variants, kas izdara visas darbības...
//Nivres arc-eager shift-reduce funkcija
function nivre($stack, $buffer, $arcs, $moves){
$stackCopy = unserialize($stack);
if((count($buffer)==0)&&($stackCopy->isEmpty())){
return;
}
$arcsString = "";
//saliek masīvus simbolu virknēs
foreach($arcs as $val) { $arcsString .= ltrim(explode("→", $val)[1], '0123456789_').";"; }
$stackTopHasArc = ($stackCopy->isEmpty() ? false : (strrpos($arcsString, $stackCopy->top())!== false));
$bufferNextHasArc = (count($buffer)!=0 ? (strrpos($arcsString, $buffer[0])!== false) : false);
$sample = array(
'stack'=>($stackCopy->isEmpty() ? "NULL" : ltrim($stackCopy->top(), '0123456789_')),
'buffer'=>(count($buffer)==0 ? "NULL" : ltrim($buffer[0], '0123456789_')),
'stackTopHasArc'=>($stackTopHasArc ? 'true' : 'false'),
'bufferNextHasArc'=>($bufferNextHasArc ? 'true' : 'false')
);
$toDoMove = getMove($sample);
$stackCopy = unserialize($stack);
var_dump($toDoMove);
if($toDoMove=="SHIFT"&&count($buffer)!=0){ // vai drīkst SHIFT?
$bufferCopy = $buffer;
$movesCopy = unserialize($moves);
$movesCopy[] = array("SHIFT", $stackCopy, $bufferCopy, $arcs);
$movesCopy = serialize($movesCopy);
$stackCopy->push(array_shift($bufferCopy));
if($stackCopy->count()==1&&count($bufferCopy)==0){
global $currentMoves;
$currentMoves = $movesCopy;
return;
}
nivre(serialize($stackCopy), $bufferCopy, $arcs, $movesCopy);
}
$stackCopy = unserialize($stack);
foreach($arcs as $val) { $arcsString .= ltrim(explode("→", $val)[1], '0123456789_').";"; }
$stackTopHasArc = ($stackCopy->isEmpty() ? false : (strrpos($arcsString, $stackCopy->top())!== false));
$bufferNextHasArc = (count($buffer)!=0 ? (strrpos($arcsString, $buffer[0])!== false) : false);
if($toDoMove=="REDUCE"&&(!$stackCopy->isEmpty())&&($stackTopHasArc)){ // vai drīkst REDUCE?
$movesCopy = unserialize($moves);
$movesCopy[] = array("REDUCE", $stackCopy, $buffer, $arcs);
$movesCopy = serialize($movesCopy);
$stackCopy->pop();
if($stackCopy->count()==1&&count($buffer)==0){
global $currentMoves;
$currentMoves = $movesCopy;
return;
}
nivre(serialize($stackCopy), $buffer, $arcs, $movesCopy);
}
$stackCopy = unserialize($stack);
foreach($arcs as $val) { $arcsString .= ltrim(explode("→", $val)[1], '0123456789_').";"; }
$stackTopHasArc = ($stackCopy->isEmpty() ? false : (strrpos($arcsString, $stackCopy->top())!== false));
$bufferNextHasArc = (count($buffer)!=0 ? (strrpos($arcsString, $buffer[0])!== false) : false);
if($toDoMove=="LEFT ARC"&&count($buffer)!=0&&!$stackCopy->isEmpty()&&!$stackTopHasArc){ // vai drīkst LEFTARC?
$arcsCopy = $arcs;
$movesCopy = unserialize($moves);
$movesCopy[] = array("LEFT ARC", $stackCopy, $buffer, $arcs);
$movesCopy = serialize($movesCopy);
$arcsCopy[] = $buffer[0]."→".$stackCopy->pop();
if(count($buffer)==0&&$stackCopy->count()==1) {
global $currentMoves;;
$currentMoves = $movesCopy;
return;
}
nivre(serialize($stackCopy), $buffer, $arcsCopy, $movesCopy);
}
$stackCopy = unserialize($stack);
foreach($arcs as $val) { $arcsString .= ltrim(explode("→", $val)[1], '0123456789_').";"; }
$stackTopHasArc = ($stackCopy->isEmpty() ? false : (strrpos($arcsString, $stackCopy->top())!== false));
$bufferNextHasArc = (count($buffer)!=0 ? (strrpos($arcsString, $buffer[0])!== false) : false);
if($toDoMove=="RIGHT ARC"&&count($buffer)!=0&&!$stackCopy->isEmpty()&&!$bufferNextHasArc){// vai drīkst RIGHTARC?
$bufferCopy = $buffer;
$arcsCopy = $arcs;
$movesCopy = unserialize($moves);
$movesCopy[] = array("RIGHT ARC", $stackCopy, $bufferCopy, $arcs);
$movesCopy = serialize($movesCopy);
$arcsCopy[] = $stackCopy->top()."→".$bufferCopy[0];
$stackCopy->push(array_shift($bufferCopy));
if(count($bufferCopy)==0&&$stackCopy->count()==1) {
global $currentMoves;
$currentMoves = $movesCopy;
return;
}
nivre(serialize($stackCopy), $bufferCopy, $arcsCopy, $movesCopy);
}
global $currentMoves;
if(is_array($currentMoves)){$currentMoves=$moves;}
}
//funkcija darbības uzminēšanai
function getMove($sample){
global $tree;
$root = $tree;
while($root['type']!="result"){
$attr = $root['name'];
$sampleVal = $sample[$attr];
foreach($root['vals'] as $val){
if ($val['name']==$sampleVal){
$childNode = $val;
break;
}
}
if(isset($childNode)) {
$root = $childNode['child'];
}else{//ja netiek pie konkrēta gājiena kokā, mēģina uzminēt
$root['type']="result";
global $rezs;
traverseArray($root);
//pārbauda, kurus gājienus vispār drīkst veikt
if(//Vai drīkst shift
$sample['buffer']==""
) $disallowed[]="SHIFT";
if(//Vai drīkst reduce
$sample['stack']=="" ||
$sample['stackTopHasArc']=='false'
) $disallowed[]="REDUCE";
if(//Vai drīkst left arc
$sample['buffer']=="" ||
$sample['stack']=="" ||
$sample['stackTopHasArc']=='true'
) $disallowed[]="LEFT ARC";
if(//Vai drīkst right arc
$sample['buffer']=="" ||
$sample['stack']=="" ||
$sample['bufferNextHasArc']=='true'
) $disallowed[]="RIGHT ARC";
//noņem no iespējamajiem minējumiem neatļautos
foreach($disallowed as $nomove) unset($rezs[$nomove]);
$root['val'] = array_search(max($rezs), $rezs);
$rezs['SHIFT'] = $rezs['REDUCE'] = $rezs['LEFT ARC'] = $rezs['RIGHT ARC'] = 0;
}
unset($childNode);
}
return $root['val'];
}
function traverseArray($array)
{
foreach($array['vals'] as $value)
{
if(isset($value['child']['vals']))
{
traverseArray($value['child']);
}else{
global $rezs;
$rezs[$value['child']['name']]++;
}
}
}