-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.test.ts
235 lines (208 loc) · 7.19 KB
/
index.test.ts
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
import Spoken from '../index'
import * as graphlib from '../graphlib'
// import { allRecognizablePhrases } from '../build/build-utils'
beforeAll(async () => {
await Spoken.init()
})
test('it can load the grammar', async () => {
expect(Spoken.modules).not.toBeNull()
})
/*test('it can generate the power set off all phrases of a given commands', async () => {
const graph: graphlib.Graph = getGraph('write', 'en-US') as graphlib.Graph
const s = allRecognizablePhrases(graph, Spoken.context.templates)
expect(s.length).not.toBe(0)
})*/
// test.only('it can search for a command given a phrase 2', async () => {
// let command = Spoken.recognizePhrase('expression anything not less or equal than anything', 'en-US')
// console.log(command?.args.operation?.args)
// })
test('it can search for a command given an id', async () => {
const graph = Spoken.findById('variable_assignment', 'pt-BR')
expect(graph).not.toBe(null)
expect((graph || {}).id).toBe('variable_assignment')
})
test('it can search for a command given a phrase', async () => {
let command = Spoken.recognizePhrase('declarar constante chamada bola', 'pt-BR')
expect(command).toMatchObject({
id: 'variable_assignment',
args: {
memType: 1,
varName: 'bola'
}
})
command = Spoken.recognizePhrase('THE', 'pt-BR')
expect(command).toBeNull()
command = Spoken.recognizePhrase('write hello how are you ?', 'en-US')
expect(command).toMatchObject({
id: 'write',
path: [
'write',
null,
{ text: 'hello' },
{ text: 'how' },
{ text: 'are' },
{ text: 'you' },
{ text: '?' }
]
})
command = Spoken.recognizePhrase('write it hello friend', 'en-US')
expect(command).toMatchObject({
id: 'write',
path: ['write', 'it', null, { text: 'hello' }, { text: 'friend' }]
})
command = Spoken.recognizePhrase('write down hello friend', 'en-US')
expect(command).toMatchObject({
id: 'write',
path: ['write', 'down', { text: 'hello' }, { text: 'friend' }],
args: { text: 'hello friend'.split(' ') }
})
command = Spoken.recognizePhrase('string who are you string', 'en-US')
expect(command).toMatchObject({
id: 'string',
path: ['string', { string: 'who' }, { string: 'are' }, { string: 'you' }, 'string'],
args: { string: 'who are you'.split(' ') }
})
})
test('it can remove some stop words', async () => {
expect(
Spoken.recognizePhrase('write it down hello friend', 'en-US')
).toMatchObject({
path: ['write', 'it', 'down', { text: 'hello' }, { text: 'friend' }]
})
expect(
Spoken.recognizePhrase('create a constant called max equals the number 72', 'en-US')
).toMatchObject({
path: [
{ isNew: true },
{ memType: 1 },
'called',
{ varName: 'max' },
'equals',
{
expression: {
id: 'expressions',
path: [{
expression: {
id: 'number',
path: ['number', { number: '72' }]
}
}]
}
}
]
})
expect(
Spoken.recognizePhrase('por favor declare uma variável chamada valor', 'pt-BR')
).toMatchObject({
path: [
{ isNew: true },
{ memType: 0 },
'chamada',
{ varName: 'valor' }
]
})
expect(
Spoken.recognizePhrase('escreva por favor hello', 'pt-BR')
).toMatchObject({
path: ['escreva', { text: 'por' }, { text: 'favor' }, { text: 'hello' }]
})
expect(
Spoken.recognizePhrase('ponteiro vá para a letra a por favor', 'pt-BR')
).toMatchObject({
path: ['ponteiro', 'letra', { symbol: 'a' }]
})
})
test('it can organize the command arguments', async () => {
expect(
Spoken.recognizePhrase('write it down hello friend', 'en-US')
).toMatchObject({
args: { text: ['hello', 'friend'] }
})
expect(
Spoken.recognizePhrase('the variable phrase equals the string hello man string', 'en-US')!.args
).toMatchObject({
expression: {
id: 'expressions',
args: {
expression: {
id: 'string',
args: { string: ['hello', 'man'] },
path: ['string', { string: 'hello' }, { string: 'man' }, 'string']
}
}
},
varName: 'phrase'
})
expect(
Spoken.recognizePhrase('nova variável chamada * bola quadrada * igual número 42 por favor', 'pt-BR')!.args
).toMatchObject({
expression: {
id: 'expressions',
args: {
expression: {
id: 'number',
args: { number: '42' },
path: [
'número',
{ number: '42' }
]
}
}
},
varName: {
id: 'multi_word_token',
args: {
words: ['bola', 'quadrada']
}
}
})
expect(
Spoken.recognizePhrase('nova variável bola igual a string de da você por favor string por favor', 'pt-BR')!.args
).toMatchObject({
expression: {
id: 'expressions',
args: {
expression: {
id: 'string',
args: { string: 'de da você por favor'.split(' ') },
path: [
'string',
{ string: 'de' },
{ string: 'da' },
{ string: 'você' },
{ string: 'por' },
{ string: 'favor' },
'string'
]
}
}
},
isNew: true,
varName: 'bola'
})
expect(
Spoken.recognizePhrase('nova variável bola igual a gap por favor', 'pt-BR')!.args
).toMatchObject({
expression: {
id: 'expressions',
args: {
wildCard: 'gap'
}
},
isNew: true,
varName: 'bola'
})
expect(
Spoken.recognizePhrase('execute a função bola com um argumentos', 'pt-BR')!.args
).toMatchObject({
functionName: 'bola',
argsNumber: '1',
extra: { lang: 'pt-BR' }
})
expect(Spoken.recognizePhrase('execute a função bola com três argumentos', 'pt-BR')!.args).toMatchObject({ argsNumber: '3'})
expect(Spoken.recognizePhrase('call function log with one argument', 'en-US')!.args).toMatchObject({ argsNumber: '1'})
expect(Spoken.recognizePhrase('go to line one', 'en-US')!.args).toMatchObject({ line: '1' })
expect(Spoken.recognizePhrase('#45', 'en-US')!.args).toMatchObject({ number: '45' })
expect(Spoken.recognizePhrase('number 45', 'en-US')!.args).toMatchObject({ number: '45' })
expect(Spoken.recognizePhrase('number #45', 'en-US')!.args).toMatchObject({ number: '45' })
})