-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.wppl
76 lines (63 loc) · 2.88 KB
/
parser.wppl
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
var featureFn = function (d) { return semppl.ruleFeatureFn(d); };
var scoreFn = function (features, params) {
return sum(_.values(mapObject(function (k, v) {
return _.has(params, k) ? v * params[k] : 0;
}, features)));
};
var normalize = function (array) {
var s = sum(array);
return map(function (x) {
return x / s;
}, array);
};
var getSemantics = function (world) {
return function (derivation) {
return derivation.semantics(world);
};
};
var marginalizeDerivationsForAssertion = function(derivationProbabilities, semantics) {
return sum(map2(function (derivation, truth) {
return derivation * truth;
}, derivationProbabilities, semantics));
}
var marginalizeDerivationsForQuestion = function(derivationProbabilities, semantics) {
// Since semantics is a 2-D tensor here, use a dot product to multiply and sum
var result = T.dot(T.transpose(Vector(derivationProbabilities)), Matrix(semantics));
return T.toScalars(result);
}
var createParser = function (grammar, parserParams) {
var parser = semppl.createParser(grammar, parserParams, semppl.ruleFeatureFn);
return cache(function (utterance, startSymbol) {
// Need to use .call to call parser as a js function
var chart = parser.call(null, utterance);
var rootCellDerivations = semppl.getRootCellDerivations(chart, startSymbol);
assert.ok(chart && rootCellDerivations && rootCellDerivations.length > 0,
"Cannot parse utterance '" + utterance + "' with startSymbol " + startSymbol);
// For now, assert that there is only one parse
assert.equal(rootCellDerivations.length, 1);
// Gather the two relevant probabilities
var derivationProbabilities = normalize(map(function (d) {
return Math.exp(scoreFn(featureFn(d), parserParams));
}, rootCellDerivations));
var marginalizationFunction = startSymbol === "$S" ? marginalizeDerivationsForAssertion :
startSymbol === "$WH" ? marginalizeDerivationsForQuestion :
null;
return cache(function (world) {
// World dependent part starts here.
var semantics = map(getSemantics(world), rootCellDerivations);
// We now have probabilities that the sentences are true given derivations P(t | d)
// Also probabilities that derivations are correct P(d)
return marginalizationFunction(derivationProbabilities, semantics);
});
});
}
var createParserWeights = function (grammar) {
return _.fromPairs(map(function (entry) {
var name = semppl.makeRuleKey(entry);
return [
name, // key
// Temporary hack, re-enable modelParam when we figure out where NaN's coming from
0 // modelParam({ mu: 0, name: name }) // value
];
}, grammar));
};