This repository has been archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scfg_translator.js
295 lines (255 loc) · 12.4 KB
/
scfg_translator.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* ScfgTranslator = a translator based on a synchronous context-free grammar.
* Uses a parsing algorithm inspired by Earley top-down parsing.
* See the main program, at the bottom of this file, for an example.
*/
var RegexpWithNames = require('./regexp_names');
var LoggingOneTimeQueue = require('./LoggingOneTimeQueue');
var LoggingStack = require('./LoggingStack');
var sets = require('simplesets');
var scfg = require('./scfg');
var assert = require('assert');
var util = require('util');
/* internal classes */
/**
* This is the basic element in the queues used by the algorithm.
*
* @author Erel Segal Halevi
* @since 2013-03
*/
var SubtextRulePair = function(subText, variableName, naturalLanguage, semanticTranslation, assignment) {
this.subText = subText; // String - some conversion of T to a flat string
this.variableName = variableName; // String - V
this.naturalLanguage = naturalLanguage; // String - some conversion of N to a flat string
this.semanticTranslation = semanticTranslation; // String - M (Meaning representation)
this.assignment = assignment; // VariableAssignments (optional)
}
SubtextRulePair.prototype = {
/**
* Assert that all variables in the natural language and the semantic representation are assigned.
*/
assertVariablesAreAssigned: function() {
assert.ok(this.assignment, "Must have a non-null assignment to the variables");
assert.ok(assignment.variableNames().containsAll(TextVariablesUtils.allVariablesInString(this.naturalLanguage)),
"All variables in the natural language sentence '"+this.naturalLanguage+"' should have an assignment, but the assignment is '"+assignment+"'");
assert.ok(assignment.variableNames().containsAll(TextVariablesUtils.allVariablesInString(this.semanticTranslation)),
"All variables in the Semantic sentence '"+this.semanticTranslation+"' should have an assignment, but the assignment is '"+assignment+"'");
},
/**
* Assert that there are no variables in the natural language or semantic representation.
*/
assertNoVariables: function() {
//assert.equal(TextVariablesUtils.hasVariables(this.naturalLanguage), false, "Natural language sentence '"+this.naturalLanguage+"' should be clean, with no variables"); // input must be clean, with no variables
//assert.equal(TextVariablesUtils.hasVariables(this.semanticTranslation), false, "Semantic sentence '"+this.semanticTranslation+"' should be clean, with no variables"); // input must be clean, with no variables
},
toString: function() {
return "('"+this.subText +"', " +
this.variableName + " -> " + this.naturalLanguage + " / "+this.semanticTranslation +
(this.assignment==null? "": ", "+JSON.stringify(this.assignment)) + ")";
},
hasTextAndVariableName: function(subText, variableName) {
//console.log("hasTextAndVariableName "+subText+","+variableName);
var result =
(this.variableName == variableName) &&
(this.subText == subText);
//console.log(result);
return result;
},
withNewAssignment: function(newAssignment) {
return new SubtextRulePair(this.subText, this.variableName, this.naturalLanguage, this.semanticTranslation, newAssignment);
}
} // end class SubtextRulePair
/**
* @return a clone of the given array of strings, sorted from long to short.
*/
function sortedFromLongToShort(array) {
array = array.map(function(a){return a;}); // == clone
array.sort(function(a,b) { // sort from long to short
if (a.length!=b.length) return b.length - a.length; // note reverse order
else return a.localeCompare(b);
});
return array;
}
/**
* Returns a copy of the object where the keys have become the values and the values are arrays of keys. For this to work, all of your object's values should be string serializable
*/
function inverted(hash) {
var inverted = {};
for (key in hash) {
var value = hash[key];
if (!inverted[value])
inverted[value]=[];
inverted[value].push(key);
}
return inverted;
}
/* main class */
/**
* Initialize a ScfgTranslator.
*
* @param grammar a SynchronousContextFreeGrammar (see scfg.js).
* @param entail [optional] a function that gets text (T) and hypothesis with variables (H), and returns a list of assignments to the variables of H, such that the result is entailed by T.
* The default 'entail' function is just identity match (see regexp_names.js).
*/
var ScfgTranslator = function(grammar, entail) {
if (!grammar) throw new Error("empty grammar");
this.grammar = grammar;
if (!entail) { // create a default (identity match) entail function:
// create the variables configuration:
var variables = grammar.variables();
grammar.nonterminals().forEach(function(nonterminal) {
if (!(nonterminal in variables))
variables[nonterminal] = ".+";
});
entail = RegexpWithNames.matches(variables);
}
this.entail = entail;
}
ScfgTranslator.prototype = {
/**
* Translate the string back or forth.
*
* @param string a text string.
* @param forward (boolean) - true to translate from source to target, false to translate from target to source.
* @return a set of translations (a hash whose KEYS are translations)..
*/
translate: function(text, forward) {
var grammar = this.grammar;
var logger = this.logger;
logger.startAction("translate '"+text+"'");
// DATA STRUCTURES:
var openQueue = new LoggingOneTimeQueue(this.logger);
var goodStack = new LoggingStack(this.logger);
var cleanSet = new sets.Set();
var mainTextId = null;
logger.startAction("Initialization");
{
var variableName = grammar.root();
var nonterminalTranslationMap = grammar.translationsOfNonterminal(variableName);
if (!forward)
nonterminalTranslationMap = inverted(nonterminalTranslationMap);
var sortedNonterminalProductions = sortedFromLongToShort(Object.keys(nonterminalTranslationMap));
sortedNonterminalProductions.forEach(function(nonterminalProduction) {
var semanticTranslations = nonterminalTranslationMap[nonterminalProduction];
if (semanticTranslations instanceof Array) {
semanticTranslations.forEach(function(semanticTranslation) {
openQueue.add(new SubtextRulePair(
text, variableName, nonterminalProduction, semanticTranslation)); // add the pair (Text, {root}->Ni/Mj)
});
} else {
openQueue.add(new SubtextRulePair(
text, variableName, nonterminalProduction, semanticTranslations)); // add the pair (Text, {root}->Ni/Mj)
}
});
} // end of initialization
logger.endAction("Initialization; Open.size="+openQueue.size()+" Good.size="+goodStack.size()+"\n");
logger.startAction("Downward Loop");
while (!openQueue.isEmpty()) {
var currentPair = openQueue.remove(); // (T, V->N)
logger.startAction("Drilling down on "+currentPair);
var assignments = this.entail(currentPair.subText, currentPair.naturalLanguage); // assignment=entail(T,N)
assignments.forEach(function(assignment) {
goodStack.push(currentPair.withNewAssignment(assignment)); // (T, V->N, assignment)
for (var variableName in assignment) {
var variableValue = assignment[variableName];
if (grammar.hasNonterminal(variableName)) { // variable has translations - it is a nonterminal:
var nonterminalTranslationMap = grammar.translationsOfNonterminal(variableName); // N/M
if (!forward)
nonterminalTranslationMap = inverted(nonterminalTranslationMap);
var sortedNonterminalProductions = sortedFromLongToShort(Object.keys(nonterminalTranslationMap));
sortedNonterminalProductions.forEach(function(nonterminalProduction) {
var semanticTranslations = nonterminalTranslationMap[nonterminalProduction];
if (semanticTranslations instanceof Array) {
semanticTranslations.forEach(function(semanticTranslation) {
openQueue.add(new SubtextRulePair(
variableValue, variableName, nonterminalProduction, semanticTranslation)); // add the pair (Text, {root}->Ni/Mj)
});
} else {
openQueue.add(new SubtextRulePair(
variableValue, variableName, nonterminalProduction, semanticTranslations)); // add the pair (Text, {root}->Ni/Mj)
}
});
}
} // end of loop over variables in a single assignment
}); // end of loop over assignments in a single entailment
logger.endAction("Open.size="+openQueue.size()+" Good.size="+goodStack.size());
} // end of downward loop (if openQueue is empty, exit)
logger.endAction("Downward Loop\n");
logger.startAction("Upward Loop");
while (!goodStack.isEmpty()) {
var currentTriple = goodStack.pop(); // (T, V->N, assignment)
logger.startAction("Climbing up on "+currentTriple);
if (!Object.keys(currentTriple.assignment).length) { // empty assignment (no variables) - clean pair!
var newTriple = currentTriple.withNewAssignment(null);
cleanSet.add(newTriple);
} else { //current assignment has variables - take one of them:
var variableName = Object.keys(currentTriple.assignment)[0]; // Vk
var variableValue = currentTriple.assignment[variableName]; // Ak
var assignmentWithoutVariable = {}; // assignment without Vk
for (var varName in currentTriple.assignment)
if (varName!=variableName)
assignmentWithoutVariable[varName] = currentTriple.assignment[varName];
if (grammar.hasNonterminal(variableName)) { // variable has translations - it is a nonterminal - clean it by replacing previously clean pairs:
cleanSet.each(function(previousCleanPair) {
if (previousCleanPair.hasTextAndVariableName(variableValue, variableName)) { // find pairs with (Ak, Vk -> ?)
var newTriple = currentTriple.withNewAssignment(assignmentWithoutVariable);
newTriple.naturalLanguage = newTriple.naturalLanguage.replace(variableName, previousCleanPair.naturalLanguage); // N*
newTriple.semanticTranslation = newTriple.semanticTranslation.replace(variableName, previousCleanPair.semanticTranslation); // M*
goodStack.push(newTriple);
}
});
} else { // variable has no translations - it is a terminal variable:
var newTriple = new SubtextRulePair(
currentTriple, assignmentWithoutVariable);
newTriple.naturalLanguage = newTriple.naturalLanguage.replace(variableName, variableValue); // N*
newTriple.semanticTranslation = newTriple.semanticTranslation.replace(variableName, variableValue); // M*
goodStack.push(newTriple);
}
} // end of handling assignment with variables
logger.endAction("Good.size="+goodStack.size()+" Clean.size="+cleanSet.size());
} // end of upward loop
logger.endAction("Upward loop\n");
logger.startAction("Finalization");
var rootTranslations = {};
cleanSet.each(function(cleanPair) {
logger.info("Checking "+cleanPair);
if (cleanPair.hasTextAndVariableName(text, grammar.root())) { // find pairs with (Text, {root} -> ?)
logger.info("Results += "+cleanPair.naturalLanguage+"/"+cleanPair.semanticTranslation);
cleanPair.assertNoVariables();
rootTranslations[cleanPair.semanticTranslation]=true;
}
});
logger.endAction(JSON.stringify(rootTranslations));
return rootTranslations;
},
logger: {
active: false,
startAction: function(topic) {
if (this.active) console.log("SCFGT "+topic+" start");
},
endAction: function(string) {
if (this.active) console.log("SCFGT "+"end "+string);
},
info: function(string) {
if (this.active) console.log("SCFGT "+string);
},
},
}
module.exports = ScfgTranslator;
// DEMO PROGRAM:
if (process.argv[1] === __filename) {
console.log("scfg_translator.js demo start");
var fs = require('fs');
var grammar = scfg.fromString(fs.readFileSync("grammars/Grammar1Flat.txt",'utf8'));
var translator = new ScfgTranslator(grammar);
translator.logger.active=false;
console.log("single forward translation: "+JSON.stringify(translator.translate("a", true)));
console.log("single backward translation: "+JSON.stringify(translator.translate("b", false)));
console.log("two forward translations: "+JSON.stringify(translator.translate("a c", true)));
console.log("two backward translations: "+JSON.stringify(translator.translate("b d", false)));
console.log("three forward translations: "+JSON.stringify(translator.translate("a c a", true)));
console.log("three backward translations: "+JSON.stringify(translator.translate("b d b", false)));
console.log("no forward translations: "+JSON.stringify(translator.translate("b d", true)));
console.log("no backward translations: "+JSON.stringify(translator.translate("a c", false)));
console.log("scfg_translator.js demo end");
}