1- import { NonTerminal , Terminal , Category } from "../../src/grammar/category" ;
1+ import { NonTerminal , Terminal } from "../../src/grammar/category" ;
22import { getViterbiParse , ParseTreeWithScore , Grammar } from "../../src/index" ;
33
4- import * as Mocha from 'mocha'
5- import { expect } from 'chai' ;
6- import { scan } from "../../src/earley/scan" ;
7- import { LogSemiring } from "semiring" ;
8- import { Chart } from "../../src/earley/chart/chart" ;
4+ import { expect } from "chai" ;
95import { g , A } from "../sample-grammar" ;
106import { parseSentenceIntoChart } from "../../src/earley/parser" ;
117
12- //TODO
13- describe ( ' parser' , ( ) => {
8+ // TODO
9+ describe ( " parser" , ( ) => {
1410
1511
16- it ( ' should complete correctly' , ( ) => {
12+ it ( " should complete correctly" , ( ) => {
1713 // complete(
1814 // 0,
1915 // "e",
2016 // LogSemiring,
2117 // ss
2218 // )
2319 } ) ;
24- it ( ' should predict correctly' , ( ) => {
20+ it ( " should predict correctly" , ( ) => {
2521 // complete(
2622 // 0,
2723 // "e",
2824 // LogSemiring,
2925 // ss
3026 // )
3127 } ) ;
32- it ( ' should parse the man chase the man with a stick' , ( ) => {
28+ it ( " should parse the man chase the man with a stick" , ( ) => {
3329 const S : NonTerminal = "S" ;
3430 const NP : NonTerminal = "NP" ;
3531 const VP : NonTerminal = "VP" ;
@@ -47,8 +43,8 @@ describe('parser', () => {
4743 const stick : Terminal < string > = ( token ) => ! ! token . match ( / s t i c k / ) ;
4844 const with_ : Terminal < string > = ( token ) => ! ! token . match ( / w i t h / ) ;
4945
50- const grammar : Grammar < string , number > = Grammar . builder ( "test" )
51- //.setSemiring(new LogSemiring()) // If not set, defaults to Log semiring which is probably what you want
46+ const grammar : Grammar < string , number > = Grammar . builder ( "test" )
47+ // .setSemiring(new LogSemiring()) // If not set, defaults to Log semiring which is probably what you want
5248 . addNewRule (
5349 1.0 , // Probability between 0.0 and 1.0, defaults to 1.0. The builder takes care of converting it to the semiring element
5450 S , // Left hand side of the rule
@@ -88,18 +84,45 @@ describe('parser', () => {
8884 grammar ,
8985 tokens
9086 ) ;
91- //console.log(JSON.stringify(viterbi.parseTree)); // {"category":"<start>","children":[{"category":"S","children":[{"category":"NP","children":[{"category":"Det","children":[{"token":"The","children":[ ]}]},{"category":"N","children":[{"token":"man","children":[]}]}] },{"category":"VP","children":[{"category":"TV","children":[{"token":"chased","children":[]}]},{"category":"NP","children":[{"category":"Det","children":[{"token":"the","children":[]}]},{"category":"N","children":[{"token":"man","c hildren":[]}]},{"category":"Mod","children":[{"token":"with","children":[]},{"category":"NP","children":[{"category":"Det","children":[{"token":"a", "children":[]}]},{"category":"N","children":[{"token":"stick","children":[]}]}]}]}]}]}] }] }
92- //console.log(viterbi.probability); // 0.6
93- //Parser.recognize(S, grammar, Tokens.tokenize("the", "stick", "chased", "the", "man"))
87+ // console.log(JSON.stringify(viterbi.parseTree)); // {"category":"<start>","children":[{"category":"S","children":[{"category":"NP","children":[{"category":"Det","children":[{"token":"The","children":[ ]}]},{"category":"N","children":[{"token":"man","children":[]}]}] },{"category":"VP","children":[{"category":"TV","children":[{"token":"chased","children":[]}]},{"category":"NP","children":[{"category":"Det","children":[{"token":"the","children":[]}]},{"category":"N","children":[{"token":"man","c hildren":[]}]},{"category":"Mod","children":[{"token":"with","children":[]},{"category":"NP","children":[{"category":"Det","children":[{"token":"a", "children":[]}]},{"category":"N","children":[{"token":"stick","children":[]}]}]}]}]}]}] }] }
88+ // console.log(viterbi.probability); // 0.6
89+ // Parser.recognize(S, grammar, Tokens.tokenize("the", "stick", "chased", "the", "man"))
9490 } ) ;
9591
9692
97- it ( 'should parse aaaaa' , ( ) => {
98- const tokens = [ "a" , "a" , "a" , "e" ] ;
99- const [ chart , i , init ] = parseSentenceIntoChart (
93+ const tokens = [ "a" , "a" , "a" , "e" ] ;
94+ it ( "should deal with scan probability correctly" , ( ) => {
95+ const p1 = getViterbiParse (
10096 A ,
10197 g ,
102- tokens
98+ tokens ,
99+ ( ignore , ignored ) => {
100+ return g . probabilityMapping . fromProbability ( 1.0 ) ;
101+ }
102+ ) . probability ;
103+
104+ const p2 = getViterbiParse (
105+ A ,
106+ g ,
107+ tokens ,
108+ ( word , ignored ) => {
109+ return word === "a" ? g . probabilityMapping . fromProbability ( 0.5 ) : undefined ;
110+ }
111+ ) . probability ;
112+
113+ const eq = p2 * 2 * 2 * 2 ;
114+ const epsilon = 0.0000000000000001 ;
115+ expect ( p1 ) . to . be . above ( eq - epsilon ) . and . below ( eq + epsilon ) ;
116+ } ) ;
117+
118+ it ( "should parse aaae" , ( ) => {
119+ const [ chart , ignored , init ] = parseSentenceIntoChart (
120+ A ,
121+ g ,
122+ tokens ,
123+ ( word , terminalTypes ) => {
124+ return g . probabilityMapping . fromProbability ( 1.0 ) ;
125+ }
103126 ) ;
104127
105128 expect ( chart . getCompletedStates ( tokens . length ) . has (
@@ -108,9 +131,5 @@ it('should parse aaaaa', () => {
108131 )
109132 ) ) . to . equal ( true ) ;
110133
111- /*console.log(g.probabilityMapping.toProbability(
112- chart.viterbiScores.get(chart.getOrCreate(
113- tokens.length, 0, init.rule.right.length, init.rule
114- )).innerScore));*/
115134 } ) ;
116135} ) ;
0 commit comments