-
Notifications
You must be signed in to change notification settings - Fork 0
/
myParseTree.php
203 lines (182 loc) · 5.01 KB
/
myParseTree.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
<?php
class Node
{
public $category; // NP, VP, PP, ...
public $word; // contains the word string
public $parent; // the parent Node
public $level; // the integer level of the node
public $visited; // for chunking
public $chunk; // a chunk of all child words/chunks
public $children; // array of all child nodes
public function __construct($category, $parent = null) {
$this->category = $category;
$this->parent = $parent;
$this->visited = false;
}
public function addChild($childNode){
$this->children[] = $childNode;
}
public function hasChildren(){
return (count($this->children)>0?true:false);
}
public function setParent($parentNode){
$this->parent = $parentNode;
}
public function getParent(){
return $this->parent;
}
public function setWord($word){
$this->word = $word;
}
//combines the current chunk with its siblings if they are just words and not chunks
public function getSiblingWords($currentChunk){
$combinedChunk = "";
if(isset($this->parent) && is_array($this->parent->children)){
foreach($this->parent->children as $sibling){
if((isset($sibling->word) && $sibling->visited) || (!isset($sibling->word) && $sibling != $this)){
return $currentChunk;
}
if($sibling != $this){
$combinedChunk .= $sibling->word." ";
$sibling->visited = true;
}else{
$combinedChunk .= $currentChunk." ";
}
}
}else {
$combinedChunk = $currentChunk;
}
if(isset($this->parent) && str_word_count($combinedChunk) < 7){
$tempNode = $this->parent;
$combinedChunk = $tempNode->getSiblingWords($combinedChunk);
}
return $combinedChunk;
}
public function allChildrenAreWords(){
if($this->hasChildren()){
foreach($this->children as $child){
if(!isset($child->word))
return false;
}
}else{
return false;
}
return true;
}
public function allSiblingsAreWords(){
if(!isset($this->parent))
return false;
foreach($this->parent->children as $sibling){
if(!isset($sibling->word) && $sibling != $this)
return false;
}
return true;
}
//traversal
public function traverse($method, $sentece) {
switch($method) {
case 'inorder':
return $this->_inorder($this, $sentece);
break;
case 'revorder':
return $this->_revorder($this, $sentece);
break;
default:
break;
}
}
private function _inorder($node, &$sentece) {
if(isset($node->word)){
$sentece .= $node->word." ";
}else{
if($node->hasChildren()){
foreach($node->children as $child){
$this->_inorder($child, $sentece);
}
}
}
return $sentece;
}
private function _revorder($node, &$sentece) {
if(isset($node->word)){
$sentece .= $node->word." ";
}else{
foreach(array_reverse($node->children) as $child){
$this->_revorder($child, $sentece);
}
}
return $sentece;
}
public function getChunks($node, &$chunks){
if(!$node->hasChildren() && !$node->visited){
$chunks[] = array('chunk' => trim($node->word), 'level' => $node->level);
$node->visited = true;
}elseif($node->allChildrenAreWords()){
$tempChunk = "";
foreach($node->children as $child){
$tempChunk .= $child->word." ";
$child->visited = true;
}
if($node->allSiblingsAreWords()){
$tempChunk = $node->getSiblingWords(trim($tempChunk));
}
$chunks[] = array('chunk' => trim($tempChunk), 'level' => $node->level);
}else{
if($node->hasChildren())
foreach(array_reverse($node->children) as $child){
$this->getChunks($child, $chunks);
}
}
return $chunks;
}
public function setInnerChunks($node){
if(isset($node->word)){
$node->chunk = $node->word;
return $node->word;
}else{
$tempInnerChunk = "";
if($node->hasChildren()){
foreach($node->children as $innerChild){
$tempInnerChunk .= $innerChild->setInnerChunks($innerChild)." ";
}
}
$tempInnerChunk = trim($tempInnerChunk);
$node->chunk = $tempInnerChunk;
return $tempInnerChunk;
}
}
public function clearInnerChunks($node){
if(isset($node->chunk)){
unset($node->chunk);
}
if($node->hasChildren()){
foreach($node->children as $nodeChild){
$this->clearInnerChunks($nodeChild);
}
}
}
public function getChunksToSize($node, $maxSize, &$chunks){
if(!isset($node->chunk)){
$node->setInnerChunks($node);
}
if(str_word_count($node->chunk) > $maxSize && $node->hasChildren()){
foreach(array_reverse($node->children) as $nodeChild){
$nodeChild->getChunksToSize($nodeChild, $maxSize, $chunks);
}
}else{
if(count($chunks) > 0){
$lastChunk = array_pop($chunks);
//combine one-word chunks and non alphanumerical chunks with to bigger chunks
if(str_word_count($node->chunk) <= 1 || strlen(preg_replace( "/[^a-zA-Z0-9]/i", "", $lastChunk )) == 0 || is_numeric(preg_replace( "/[^a-zA-Z0-9]/i", "", $lastChunk )) || is_numeric($node->chunk)){
$chunks[] = $node->chunk." ".$lastChunk;
}else{
$chunks[] = $lastChunk;
$chunks[] = $node->chunk;
}
}else{
$chunks[] = $node->chunk;
}
}
return $chunks;
}
}