-
Notifications
You must be signed in to change notification settings - Fork 11
/
parser.js
158 lines (134 loc) · 3.9 KB
/
parser.js
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
// Inspired by phpSyntaxTree by Andre Eisenbach <[email protected]>
// https://code.google.com/p/phpsyntaxtree/
var pos = 0;
var data;
// root node
var root;
var currentNode;
function parse(textData) {
// Reset
pos = 0;
currentNode = null;
root = null;
data = textData;
// If this is a CCG tree keep only the categories and words
if (data.match(/<T\s+(.*?)\s+[0-9]+\s+[0-9]+>/)) {
// Replace any category-internal ()'s with [] to avoid confusing the parser
// will be changed back later in TreeNode.toJSON()
data = data.replace(/<T\s+(.*?)\s+[0-9]+\s+[0-9]+>/g, function(a, b){
return b.replace(/\[/g,"{").replace(/\]/g,"}").replace(/\(/g,"[").replace(/\)/g,"]");
});
data = data.replace(/<L\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)>/g, function(a, b, c, d, e){
return b.replace(/\[/g,"{").replace(/\]/g,"}").replace(/\(/g,"[").replace(/\)/g,"]") + ' ' + e;
});
}
// Clean up
data = data.replace(/\s+/g, " ");
data = data.replace(/\)\s+\(/g, ")(");
data = data.replace(/\(\s+\(/g, "((");
// Not sure why I have to do this twice... apparently
data = data.replace(/\)\s+\)/g, "))").replace(/\)\s+\)/g, "))");
// Parse the string
makeTree();
root = currentNode;
while (root.getParent() != null) root = root.getParent();
return jQuery.parseJSON(root.toJSON());
}
function makeTree() {
var token = getNextToken().trim();
// console.log("First token: " + token);
var parts = [2];
while( token != "" && token != ")" ) {
switch(token.charAt(0)) {
case "(":
token = token.substr(1, token.length - 1);
var spaceAt = token.indexOf(" ");
var childNode;
if (spaceAt != -1) {
parts[0] = token.substr(0, spaceAt);
parts[1] = token.substr(spaceAt, token.length - spaceAt);
childNode = new TreeNode(parts[0]);
childNode.addChild(new TreeNode(parts[1]));
}
else {
childNode = new TreeNode(token);
}
if (currentNode) {
// console.log("Current node: " + currentNode.toJSON());
// console.log("Adding child" + childNode.toJSON());
currentNode.addChild(childNode);
}
currentNode = childNode;
makeTree();
break;
default:
if (token != "") {
currentNode.addChild(new TreeNode(token));
}
break;
}
token = getNextToken().trim();
}
if (token == ")") {
if (currentNode.getParent()) {
currentNode = currentNode.getParent();
// console.log("Back to: " + currentNode.toJSON());
}
}
}
function getNextToken() {
var gotToken = false;
var token = "";
var i = 0;
if ((pos + 1) >= data.length) return "";
while((pos + i) < data.length && !gotToken ) {
var ch = data.charAt(pos + i);
switch(ch) {
case "(":
if( i > 0 ) gotToken = true;
else token += ch;
break;
case ")":
if( i == 0 ) token += ch;
gotToken = true;
break;
default:
token += ch;
break;
}
i++;
}
if (i > 1) pos += i - 1;
else pos++;
return token;
}
function TreeNode(label) {
this.parent;
this.name = label;
this.children = [];
this.addChild = function(child) {
child.parent = this;
this.children[this.children.length] = child;
}
this.getParent = function() {
return this.parent;
}
this.getChild = function(index) {
return this.children[index];
}
this.toJSON = function() {
label = label.replace(/\[/g,"(").replace(/\]/g,")").replace(/{/g,"[").replace(/}/g,"]");
var text = "{\"name\":" + "\"" + label + "\"";
if (this.children.length != 0) {
text += ",\"children\":[";
for (var index = 0; index < this.children.length; ++index) {
text += this.children[index].toJSON() + ",";
}
//Remove the last comma
text = text.substr(0, text.length-1);
text += "]";
}
text += "}";
return text;
}
}