-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssml tokenizer.js
238 lines (221 loc) · 7.43 KB
/
ssml tokenizer.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
import { Reader } from "./reader.js"
export class SSMLTokenzier{
constructor(txt = ""){
this.reader = new Reader(txt)
this.text = txt
this.token_array = []
this.accaptable_modifiers = ["p", "t", "r"]
this.startedSSML = false
this.char = this.reader.read()
}
initializeTokenzier(txt){
this.reader.initializeText(txt)
this.text = txt
this.token_array = []
this.char = this.reader.read()
}
generateTokenArray(){
this.reader.read()
let sub_string = ""
loop:
while(this.reader.char !== undefined){
////console.log("CHAR", this.reader.char
switch(this.reader.char){
case "#":{
if(sub_string.length){
this.token_array.push(sub_string)
sub_string = ""
}
let start_i = this.reader.i
if(!this._SSMLSwitch()){
sub_string = sub_string + "#"
this.reader.moveHeadTo(start_i)
}
this.reader.readnext()
break;
}
default:{
sub_string = sub_string + this.reader.char
this.reader.readnext()
}
}
}
if(sub_string.length){
this.token_array.push(sub_string)
}
return this.token_array
}
_SSMLSwitch(){
let test_array = ["%%%"]
let temp = []
let has_data = false
while(this.reader.char){
this.reader.readnext()
if(this.reader.char === undefined){
return false
}
switch(this.reader.char.toLowerCase()){
case "b":{
temp = this._getModAndFloatScale("b")
if(temp){
test_array = test_array.concat(temp)
has_data = true
}
break
}
case "i":{
temp = this._getModAndEncap("i")
if(temp){
test_array = test_array.concat(temp)
has_data = true
}
break
}
case "l":{
temp = this._getModAndEncap("l")
if(temp){
test_array = test_array.concat(temp)
has_data = true
}
break
}
case "p":{
temp = this._getModAndScale("p")
if(temp){
test_array = test_array.concat(temp)
has_data = true
}
break
}
case "m":{
temp = this._getModAndScale("m")
if(temp){
test_array = test_array.concat(temp)
has_data = true
}
break
}
case "r":{
temp = this._getModAndScale("r")
if(temp){
test_array = test_array.concat(temp)
has_data = true
}
break
}
case "v":{
temp = this._getModAndScale("v")
if(temp){
test_array = test_array.concat(temp)
has_data = true
}
break
}
case "t":{
temp = this._getModAndScale("t")
if(temp){
test_array = test_array.concat(temp)
has_data = true
}
break
}
case "w":{
test_array = test_array.concat("%w%")
has_data = true
break
}
case "s":{
test_array = test_array.concat("%s%")
has_data = true
break
}
case "e":{
test_array = test_array.concat("%e%")
has_data = true
break
}
case "d":{
temp = this._getModAndFloatScale("d")
if(temp){
test_array = test_array.concat(temp)
has_data = true
}
break
}
case "[":{
temp = this._switchEncapsulation()
if(temp && temp.length && has_data){
has_data = true
test_array = test_array.concat(temp)
this.token_array = this.token_array.concat(test_array)
return true;
}else{
return false;
}
}
case " ":{
return false
}
}
}
}
_switchEncapsulation(){
let test = this.reader.isEncapsulating("[", "]")
if(test !== false){
let sub = this.reader.getSubstring(test.open + 1, test.close - 1)
let sub_tokenizer = new SSMLTokenzier(sub)
let sub_array = ["%[%"]
sub_array = sub_array.concat(sub_tokenizer.generateTokenArray())
sub_array.push("%]%")
this.reader.moveHeadTo(test.close)
return sub_array
}else{
return false
}
}
_getModAndEncap(letter){
let data = "%" + letter
if(this.reader.peak() === "("){
this.reader.readnext()
let encap = this.reader.getSuperGreedyEncapsulation("(", ")", true)
if(encap !== false){
return `${data}${encap}%`
}else{
return false
}
}else{
return false
}
}
_getModAndScale(letter){
let data = "%" + letter
let complete_data = false
let temp_array = []
//console.log(letter, "peeking", this.reader.peak())
while(this._isCharNumber(this.reader.peak()) || this.reader.peak() === "+" || this.reader.peak() === "-"){
this.reader.readnext()
//console.log(letter, "found", this.reader.char)
data = data + this.reader.char
complete_data = true
}
temp_array.push(data + "%")
return temp_array
}
_getModAndFloatScale(letter){
let data = "%" + letter
let complete_data = false
let temp_array = []
//console.log("P peeking", this.reader.peak())
while(this._isCharNumber(this.reader.peak()) || this.reader.peak() === "." || this.reader.peak() === "+" || this.reader.peak() === "-"){
this.reader.readnext()
//console.log("P found NUMBER", this.reader.char)
data = data + this.reader.char
complete_data = true
}
temp_array.push(data + "%")
return temp_array
}
_isCharNumber(c) {
return c >= '0' && c <= '9';
}
}