Check-in [cff43748de]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Finished implementation of desired parser and fixed a couple of bugs with the existing code.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:cff43748de7c476b6e51d3fdecc90d598d98ed5d
User & Date: adamc 2016-10-22 18:11:57
Context
2016-10-24
19:21
[3e1532df89] Added a new npm package and some tsconfig mojo to fix the problem that I was experiencing with errors being thrown when trying to build the tsUnit-based unit tests. There's still a problem with running tham via gulp. Found the solution at: http://stackoverflow.com/questions/35660498/angular-2-cant-find-promise-map-set-and-iterator check-in: 28c3a8dcb9 user: CrosslandA tags: trunk
2016-10-22
18:11
Finished implementation of desired parser and fixed a couple of bugs with the existing code. check-in: cff43748de user: adamc tags: trunk
18:07
Fixed a bug in the all parser that caused it to indicate a successful parse when no characters had been matched. check-in: 49d510b670 user: adamc tags: trunk
Changes

Changes to app/chattymath.ts.

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
..
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
...
120
121
122
123
124
125
126

127
128
129
130
131
132
133
...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
...
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
}

class ChattyData implements pcomb.ParserOutput {
    matched: string[];
    leftOperand: number;
    rightOperand: number;
    operator: MathOps;
    accumulator: number;

    copy(): ChattyData {
        let newData = new ChattyData();
        if (newData.matched) {
            newData.matched = this.matched.slice();
        } else {
            newData.matched = Array<string>();
        }
        newData.leftOperand = this.leftOperand;
        newData.rightOperand = this.rightOperand;
        newData.operator = this.operator;


        return newData;
    }
}

export class Chatty {
    formulaParser: pcomb.Parser;

    numberParser: pcomb.Parser;
    operandPlus: pcomb.Parser;
    operandMinus: pcomb.Parser;
    operandMultiply: pcomb.Parser;
    operandDivide: pcomb.Parser;
    operand: pcomb.Parser;
    singleDigitNumbers: pcomb.Parser;


    englishNumbers: pcomb.Parser;
    parsers: pcomb.ParserSet;


    private operandSet(matched: string, output: pcomb.ParserOutput): pcomb.ParserOutput {
        let result: ChattyData = <ChattyData>output.copy();

        switch (matched) {
            case "+":
            case "plus":
................................................................................
                result.operator = MathOps.Divide;
                break;
        }

        return result;
    }

    private numberSet(matched: string, output: pcomb.ParserOutput): pcomb.ParserOutput {
        let result: ChattyData = <ChattyData>output.copy();
        let matchedAsNumber: number = Number(matched);

        if (result.leftOperand == null) {
            result.leftOperand = matchedAsNumber;
        } else {
            result.rightOperand = matchedAsNumber;
................................................................................
                result.accumulator += 8;
                break;
            case "nine":
                result.accumulator += 9;
                break;
            case "ten":
                result.accumulator += 10;

            case "eleven":
                result.accumulator += 11;
                break;
            case "twelve":
                result.accumulator += 12;
                break;
            case "thirteen":
................................................................................
                break;
            case "eighteen":
                result.accumulator += 18;
                break;
            case "nineteen":
                result.accumulator += 19;
                break;
            case "twnety":
                result.accumulator += 20;
                break;
            case "thirty":
                result.accumulator += 30;
                break;
            case "forty":
                result.accumulator += 40;
................................................................................
            case "ninety":
                result.accumulator += 90;
                break;
        }

        return result;
    }

    constructor() {






        this.operandPlus = pcomb.or([pcomb.lit("+", this.operandSet),
            pcomb.lit("plus", this.operandSet)]);
        this.operandMinus = pcomb.or([pcomb.lit("-", this.operandSet),
            pcomb.lit("minus", this.operandSet)]);
        this.operandMultiply = pcomb.or([pcomb.lit("*", this.operandSet),
            pcomb.lit("times", this.operandSet)]);
        this.operandDivide = pcomb.or([pcomb.lit("/", this.operandSet),
            pcomb.lit("divided by", this.operandSet)]);

        this.operand = pcomb.or([this.operandPlus,
            this.operandMinus,
            this.operandMultiply,
            this.operandDivide]);

        this.singleDigitNumbers = pcomb.or([
            pcomb.lit("one"),
            pcomb.lit("two"),
            pcomb.lit("three"),
            pcomb.lit("four"),
            pcomb.lit("five"),
            pcomb.lit("six"),
            pcomb.lit("seven"),
            pcomb.lit("eight"),
            pcomb.lit("nine")]
        );

























        this.numberParser = pcomb.all(pcomb.numeric(), this.numberSet);













        this.formulaParser = pcomb.and([
            pcomb.opt(pcomb.lit("what does")),
            pcomb.opt(pcomb.lit("what is")),
            this.numberParser,

            this.operand,

            this.numberParser,

            pcomb.opt(pcomb.or([pcomb.lit("="), pcomb.lit("equal")]))]);

        this.parsers = new pcomb.ParserSet(new ChattyInput(), new ChattyData());
    }

    ask(text: string): string {
        let theAnswer = "sorry, but I couldn't understand that";







|











>







>







>
>


>







 







|







 







>







 







|







 







>

>
>
>
>
>
>

|

|

|

|


|
|
|










|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>





>

>

>







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
..
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
...
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
}

class ChattyData implements pcomb.ParserOutput {
    matched: string[];
    leftOperand: number;
    rightOperand: number;
    operator: MathOps;
    accumulator: number = 0;

    copy(): ChattyData {
        let newData = new ChattyData();
        if (newData.matched) {
            newData.matched = this.matched.slice();
        } else {
            newData.matched = Array<string>();
        }
        newData.leftOperand = this.leftOperand;
        newData.rightOperand = this.rightOperand;
        newData.operator = this.operator;
        newData.accumulator = this.accumulator;

        return newData;
    }
}

export class Chatty {
    formulaParser: pcomb.Parser;
    numericParser: pcomb.Parser;
    numberParser: pcomb.Parser;
    operandPlus: pcomb.Parser;
    operandMinus: pcomb.Parser;
    operandMultiply: pcomb.Parser;
    operandDivide: pcomb.Parser;
    operand: pcomb.Parser;
    singleDigitNumbers: pcomb.Parser;
    doubleDigitNumbersLessThen20: pcomb.Parser;
    tensIncrementNumbers: pcomb.Parser;
    englishNumbers: pcomb.Parser;
    parsers: pcomb.ParserSet;
    translatedNumberSave: pcomb.ParserAction;

    private operandSet(matched: string, output: pcomb.ParserOutput): pcomb.ParserOutput {
        let result: ChattyData = <ChattyData>output.copy();

        switch (matched) {
            case "+":
            case "plus":
................................................................................
                result.operator = MathOps.Divide;
                break;
        }

        return result;
    }

    private numberSet(matched: string, output: pcomb.ParserOutput): ChattyData {
        let result: ChattyData = <ChattyData>output.copy();
        let matchedAsNumber: number = Number(matched);

        if (result.leftOperand == null) {
            result.leftOperand = matchedAsNumber;
        } else {
            result.rightOperand = matchedAsNumber;
................................................................................
                result.accumulator += 8;
                break;
            case "nine":
                result.accumulator += 9;
                break;
            case "ten":
                result.accumulator += 10;
                break;
            case "eleven":
                result.accumulator += 11;
                break;
            case "twelve":
                result.accumulator += 12;
                break;
            case "thirteen":
................................................................................
                break;
            case "eighteen":
                result.accumulator += 18;
                break;
            case "nineteen":
                result.accumulator += 19;
                break;
            case "twenty":
                result.accumulator += 20;
                break;
            case "thirty":
                result.accumulator += 30;
                break;
            case "forty":
                result.accumulator += 40;
................................................................................
            case "ninety":
                result.accumulator += 90;
                break;
        }

        return result;
    }

    constructor() {
        this.translatedNumberSave = (matched: string, output: ChattyData) => {
            let accuStr: string = output.accumulator.toString();
            output.accumulator = 0;
            return this.numberSet(accuStr, output);
        };

        this.operandPlus = pcomb.or([pcomb.lit("+", this.operandSet),
        pcomb.lit("plus", this.operandSet)]);
        this.operandMinus = pcomb.or([pcomb.lit("-", this.operandSet),
        pcomb.lit("minus", this.operandSet)]);
        this.operandMultiply = pcomb.or([pcomb.lit("*", this.operandSet),
        pcomb.lit("times", this.operandSet)]);
        this.operandDivide = pcomb.or([pcomb.lit("/", this.operandSet),
        pcomb.lit("divided by", this.operandSet)]);

        this.operand = pcomb.or([this.operandPlus,
        this.operandMinus,
        this.operandMultiply,
        this.operandDivide]);

        this.singleDigitNumbers = pcomb.or([
            pcomb.lit("one"),
            pcomb.lit("two"),
            pcomb.lit("three"),
            pcomb.lit("four"),
            pcomb.lit("five"),
            pcomb.lit("six"),
            pcomb.lit("seven"),
            pcomb.lit("eight"),
            pcomb.lit("nine")
        ], this.numberTranslate);

        this.doubleDigitNumbersLessThen20 = pcomb.or([
            pcomb.lit("ten"),
            pcomb.lit("eleven"),
            pcomb.lit("tweleve"),
            pcomb.lit("thirteen"),
            pcomb.lit("fourteen"),
            pcomb.lit("fifteen"),
            pcomb.lit("sixteen"),
            pcomb.lit("seventeen"),
            pcomb.lit("eighteen"),
            pcomb.lit("nineteen")
        ], this.numberTranslate);

        this.tensIncrementNumbers = pcomb.or([
            pcomb.lit("twenty"),
            pcomb.lit("thirty"),
            pcomb.lit("forty"),
            pcomb.lit("fifty"),
            pcomb.lit("sixty"),
            pcomb.lit("seventy"),
            pcomb.lit("eighty"),
            pcomb.lit("ninety")
        ], this.numberTranslate);

        this.numericParser = pcomb.all(pcomb.numeric(), this.numberSet);

        this.numberParser = pcomb.or([
            this.numericParser,
            pcomb.apply(this.doubleDigitNumbersLessThen20, this.translatedNumberSave),
            pcomb.apply(pcomb.and([
                this.tensIncrementNumbers,
                pcomb.or([pcomb.lit("-"), pcomb.whitespace()]),
                this.singleDigitNumbers
            ]), this.translatedNumberSave),
            pcomb.apply(this.tensIncrementNumbers, this.translatedNumberSave),
            pcomb.apply(this.singleDigitNumbers, this.translatedNumberSave)
        ]);

        this.formulaParser = pcomb.and([
            pcomb.opt(pcomb.lit("what does")),
            pcomb.opt(pcomb.lit("what is")),
            this.numberParser,
            pcomb.opt(pcomb.whitespace()),
            this.operand,
            pcomb.opt(pcomb.whitespace()),
            this.numberParser,
            pcomb.opt(pcomb.whitespace()),
            pcomb.opt(pcomb.or([pcomb.lit("="), pcomb.lit("equal")]))]);

        this.parsers = new pcomb.ParserSet(new ChattyInput(), new ChattyData());
    }

    ask(text: string): string {
        let theAnswer = "sorry, but I couldn't understand that";