-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech.page.ts
517 lines (480 loc) · 22.1 KB
/
speech.page.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { SpeechRecognition } from '@ionic-native/speech-recognition/ngx';
import { Platform } from '@ionic/angular';
import { TextToSpeech } from '@ionic-native/text-to-speech/ngx';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { Router } from '@angular/router';
import { UserInfoService } from '../user-info.service';
import { MyurlsService } from '../myurls.service';
import { Storage } from '@ionic/storage';
@Component({
selector: 'app-speech',
templateUrl: './speech.page.html',
styleUrls: ['./speech.page.scss'],
})
export class SpeechPage implements OnInit {
isSpeechAvailable: boolean;
isListening = false;
matches: Array<string> = [];
stopGreating = false;
townsArray = ['okra', 'newyork', 'denver', 'houston', 'dallas', 'paris',
'toulouse', 'marseille', 'abidjan', 'aboisso', 'bassam', 'daloa', 'abobo', 'riviera',
'yopougon', 'bondoukou', 'bondoucou', 'accra', 'koumassi', 'kumasi', 'marcory', 'angre', 'II-plateau', 'plateau'];
postRequestsArray = ['publier', 'proposer', 'poster', 'publication',
'proposition', 'publish', 'post'];
exitsArray = ['merci', 'au revoir', 'thanks', 'thank', 'bon', 'bye', 'ok', 'bye-bye', 'goodBye', 'good-bye'];
monthsArray = ['janvier', 'fevrier', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août',
'septembre', 'octobre', 'novembre', 'decembre', 'january', 'february',
'march', 'april', 'may', 'june', 'july', 'august', 'september', 'november', 'december'];
digit = '1234567890';
travelObject = {
towns: {
departure: '',
destination: ''
},
place: {
Rencontre: '',
Depot: ''
},
date: {
dayName: '',
dayRank: '',
month: {
monthName: '',
monthRank: ''
}
},
hour: ''
};
dayNamesArray = ['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi',
'dimanche', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
researchRequestsArray = ['recherche', 'rechercher', 'recherché', 'cherche',
'cherché', 'chercher', 'research', 'searching', 'search', 'researching'];
language = 'fr-FR';
speechObj = {
greating: ['bonjour yannick. que puis-je faire pour vous', 'hi ! what can I do for you'],
didNotCatch: ['desolé je ne vous ai pas saisi', 'sorry. I didn\'t catch you'],
holdOn: ['patientez s\'il vous plait', 'please wait...'],
listening: ['je vous écoute', 'I am listening to you'],
postConfirmation: ['vous voulez publier un trajet. c\'est bien cela?', 'you want to publish a travel. aren\'t you ?'],
researchConfirmation: ['vous rechercher un trajet. c\'est cela ?', 'you are searching for a travel. aren\'t you ?'],
checkTowns_p: ['vous voulez publier un trajet de ', 'you want to publish a travel from'],
postOrResearch: ['vous voulez publier ou rechercher un trajet', 'you want to publish or research a travel'],
giveTowns: ['repétez votre requête en précisant les villes de depart et d\'arrivée',
'repeat your request precising the departure and destination towns'],
giveDate: ['repétez votre requête en précisant la date',
'repeat your request precising the date please'],
checkTowns_r: ['vous rechercher un trajet de ', 'you are searching for a travel from'],
to: ['à', 'to'],
confirmTowns: ['veuillez confirmer si c\'est bien le cas', 'is it correct ?'],
yesOrNo: ['veuillez repondre par oui ou non', 'please answer by yes or no'],
travelFound: ['voyages repondent a votre requête. le choix optimal est celui-ci',
'travels match to your request. let me choose the best one for you : this one is the best one !'],
noGreating: ['je vous écoute', 'i am listening to you'],
notification: ['voulez-vous recevoir une notification dès qu\' un trajet semblable est disponible ?',
'do you want to receive notifications as soon as any travel of the same kind is available ?'],
forward: ['ok c\'est fait. avez vous d\'autres preoccupations ?', 'ok done. anything more ?'],
sayThanks: ['si vous etes satisfait dites merci pour quitter l\'assistance vocale', 'say thanks or goodbye to exit'],
goodBye: ['au revoir. et merci de faire confiance à Ivoiturage', 'goodBye. have a nice trip '],
noTravel: ['désolé. aucun voyage ne répond a votre requête', 'sorry. no travel matches to your request'],
processing: ['nous recherchons un voyage correspondant a votre requête', 'searching for a travel matching your request'],
confirmTravel: ['vous souhaitez voyager de ', 'so you would make a travel from'],
reservation: ['voulez vous reserver ce trajet', 'do I make a reservation for you ?'],
on: ['ce', 'on']
};
options = {
language: 'fr-FR',
prompt: '',
matches: 1,
showPopup: true,
showPartial: false,
};
bestTravel: any;
rate: number;
travelObj: {
Depart: any,
Destination: any,
Rencontre: any,
Depot: any; Date: any,
NbPlace: any; Prix: any,
LoginConducteur: any,
Heure: any
};
price: any;
idTrajet: any;
constructor(private speechRecognition: SpeechRecognition,
private plf: Platform,
private changeDetectorRef: ChangeDetectorRef,
private tts: TextToSpeech,
public http: HttpClient,
private router: Router,
public userInfo: UserInfoService,
private url: MyurlsService,
private storage: Storage) {
this.plf.ready().then(() => {
this.speechRecognition.isRecognitionAvailable()
.then((available: boolean) => this.isSpeechAvailable = available);
});
}
ngOnInit() {}
speech(statementType: string, params ?: any[]) {
let index = 0;
this.rate = 2;
let statement: string;
if (this.language === 'en-US') {
index = 1;
this.rate = 1;
}
if (statementType === 'greating') {
if (!this.stopGreating) {
statement = this.speechObj.greating[index];
this.stopGreating = true;
} else {
statement = this.speechObj.noGreating[index];
}
} else if (statementType === 'didNotCatch') {
statement = this.speechObj.didNotCatch[index];
} else if (statementType === 'giveDate') {
statement = this.speechObj.giveDate[index];
} else if (statementType === 'confirmTravel') {
statement = this.speechObj.confirmTravel[index] +
this.travelObject.towns.departure + ' ' +
this.speechObj.to[index] + ' ' +
this.travelObject.towns.destination + ' ' +
this.speechObj.on[index] + ' ' +
this.travelObject.date.dayName + ' ' +
this.travelObject.date.dayRank + ' ' + this.travelObject.date.month.monthName;
} else if (statementType === 'giveTowns') {
statement = this.speechObj.giveTowns[index];
} else if (statementType === 'didNotCatch') {
statement = this.speechObj.didNotCatch[index];
} else if (statementType === 'holdOn') {
statement = this.speechObj.holdOn[index];
} else if (statementType === 'listening') {
statement = this.speechObj.listening[index];
} else if (statementType === 'postConfirmation') {
statement = this.speechObj.postConfirmation[index];
} else if (statementType === 'researchConfirmation') {
statement = this.speechObj.researchConfirmation[index];
} else if (statementType === 'postOrResearch') {
statement = this.speechObj.postOrResearch[index];
} else if (statementType === 'checkTowns_r') {
statement = this.speechObj.checkTowns_r[index];
} else if (statementType === 'checkTowns_p') {
statement = this.speechObj.checkTowns_p[index];
} else if (statementType === 'to') {
statement = this.speechObj.to[index];
} else if (statementType === 'confirmTowns') {
statement = this.speechObj.confirmTowns[index];
} else if (statementType === 'yesOrNo') {
statement = this.speechObj.yesOrNo[index];
} else if (statementType === 'travelFound') {
statement = this.speechObj.travelFound[index];
} else if (statementType === 'notification') {
statement = this.speechObj.notification[index];
} else if (statementType === 'forward') {
statement = this.speechObj.forward[index];
} else if (statementType === 'sayThanks') {
statement = this.speechObj.sayThanks[index];
} else if (statementType === 'noTravel') {
statement = this.speechObj.noTravel[index];
} else if (statementType === 'processing') {
statement = this.speechObj.processing[index];
} else if (statementType === 'reservation') {
statement = this.speechObj.reservation[index];
} else if (statementType === 'goodBye') {
statement = this.speechObj.goodBye[index];
}
return statement;
}
searchRequestType(request: string) {
let requestType: string;
request.split(' ').forEach(elm => {
if (this.postRequestsArray.includes(elm.toLowerCase())) {
requestType = 'postRequest';
} else if (this.researchRequestsArray.includes(elm.toLowerCase())) {
requestType = 'researchRequest';
} else if (this.exitsArray.includes(elm.toLowerCase())) {
requestType = 'exit';
}
});
return requestType;
}
startTravelListening_fr() {
this.startTravelListening('fr-FR');
}
startTravelListening_en() {
this.startTravelListening('en-US');
}
startTravelListening(language: string) {
this.language = language;
this.options = {
language: this.language,
prompt: this.speech('listening'),
matches: 1,
showPopup: true,
showPartial: false,
};
this.tts.speak({text: this.speech('greating'), locale: this.language, rate: 2}).then(() => {});
this.matches = [];
this.speechRecognition.startListening(this.options)
.subscribe(
(matches: string[]) => {
this.matches = matches;
this.treatment(matches[0]);
this.changeDetectorRef.detectChanges();
},
() => {
this.tts.speak({text: this.speech('didNotCatch'), locale: this.language, rate: 2}).then(() => {});
this.changeDetectorRef.detectChanges();
}
);
}
researchTowns(request: string) {
request.split(' ').forEach(elm => {
if (this.townsArray.includes(elm.toLowerCase())) {
if (( this.travelObject.towns.departure === undefined) ||
( this.travelObject.towns.departure === '')) {
this.travelObject.towns.departure = elm.toLowerCase();
if (this.travelObject.towns.departure === 'okra') {
this.travelObject.towns.departure = 'accra';
}
} else {
this.travelObject.towns.destination = elm.toLowerCase();
}
}
});
}
searchDate(statement: string) {
statement.split(' ').forEach((elm: string) => {
if (this.dayNamesArray.includes(elm.toLowerCase())) {
this.travelObject.date.dayName = elm.toLowerCase();
}
if ( (elm.length < 3 ) && !(this.digit.indexOf(elm[0]) === -1)) {
this.travelObject.date.dayRank = elm;
} else if (!(elm.indexOf('th') === -1)) {
if (elm.length === 3) {
this.travelObject.date.dayRank = elm[0];
} else {
this.travelObject.date.dayRank = elm[0] + elm[1];
}
}
for (let i = 0; i < this.monthsArray.length; i++) {
if (this.monthsArray[i] === elm.toLowerCase()) {
this.travelObject.date.month.monthName = elm.toLowerCase();
this.travelObject.date.month.monthRank = (i + 1) + '';
}
}
});
}
treatment(request: string) {
const requestType = this.searchRequestType(request);
if (requestType === 'postRequest' ||
requestType === 'researchRequest') {
this.researchTowns(request);
this.searchDate(request);
if (requestType === 'researchRequest') {
this.tts.speak({text: this.speech('confirmTravel'),
locale: this.language, rate: this.rate}).then(() => {});
let response: boolean;
this.matches = [];
this.speechRecognition.startListening(this.options)
.subscribe(
(matches: string[]) => {
this.matches = matches;
response = this.confirmation(matches[0]);
if (response) {
this.researchTravel();
} else {
this.startTravelListening(this.language);
}
this.changeDetectorRef.detectChanges();
},
() => {
this.tts.speak({text: this.speech('didNotCatch'), locale: this.language, rate: 2}).then(() => {});
this.changeDetectorRef.detectChanges();
}
);
}
} else if (requestType === 'exit') {
this.tts.speak({text: this.speech('goodBye'), locale: this.language, rate: 2}).then(() => {});
this.router.navigateByUrl('/home');
} else if (requestType === 'postRequest') {
this.tts.speak({text: this.speech('confirmTravel'),
locale: this.language, rate: this.rate}).then(() => {});
let response: boolean;
this.matches = [];
this.speechRecognition.startListening(this.options)
.subscribe(
(matches: string[]) => {
this.matches = matches;
response = this.confirmation(matches[0]);
if (response) {
this.postTravel();
} else {
this.startTravelListening(this.language);
}
this.changeDetectorRef.detectChanges();
},
() => {
this.tts.speak({text: this.speech('didNotCatch'), locale: this.language, rate: 2}).then(() => {});
this.changeDetectorRef.detectChanges();
}
);
}
}
confirmation(request: string) {
let response: boolean;
request.split(' ').forEach(elm => {
if ((elm.toLowerCase() === 'oui') || (elm.toLowerCase() === 'yes')) {
response = true;
} else if ((elm.toLowerCase() === 'non') || (elm.toLowerCase() === 'no')) {
response = false;
}
});
return response;
}
postTravel() {
this.tts.speak({text: this.speech('holdOn'), locale: this.language, rate: 2}).then(() => {
this.travelObj = {
Depart : this.travelObject.towns.departure,
Destination : this.travelObject.towns.destination,
Rencontre: this.travelObject.place.Rencontre,
Depot: this.travelObject.place.Depot,
Date: this.travelObject.date.dayName + ' ' + this.travelObject.date.dayRank + ' ' + this.travelObject.date.month,
NbPlace: 1,
Prix: this.price,
LoginConducteur: this.userInfo.info.login,
Heure: this.travelObject.hour,
};
// this.socket.emit('insertion', this.travelObj);
});
}
researchTravel() {
this.tts.speak({text: this.speech('holdOn'), locale: this.language, rate: 2}).then(() => {});
const headers: any = new HttpHeaders({ 'Content-Type': 'application/json' }),
options: any = {key: 'trajet', params: {}};
this.http.post(this.url.retrieveUrl, JSON.stringify(options), headers)
.subscribe((data: any) => {
let travels = [];
this.tts.speak({text: this.speech('processing'),
locale: this.language, rate: 2}).then(() => {
data.forEach((elm, index) => {
if (elm.Depart.toLowerCase() === this.travelObject.towns.departure.toLowerCase() &&
elm.Destination.toLowerCase() === this.travelObject.towns.destination.toLowerCase()) {
travels.push(elm);
this.bestTravel = elm;
}
if (index === data.length - 1 || data.length === 0) {
let nbTravelsSpeech = travels.length + ' ' + this.speech('travelFound');
if (travels.length === 0) {
nbTravelsSpeech = this.speech('noTravels');
}
this.tts.speak({text: nbTravelsSpeech,
locale: this.language, rate: 2}).then(() => {
this.bestTravel = this.bestChoice(travels);
this.tts.speak({text: this.speech('reservation'), locale: this.language, rate: 2}).then(() => {
this.matches = [];
this.speechRecognition.startListening(this.options)
.subscribe(
(matches: string[]) => {
this.matches = matches;
const response = this.confirmation(matches[0]);
if (response) {
const headers: any = new HttpHeaders({ 'Content-Type': 'application/json' }),
options: any = {
key: 'reservation',
params: {
loginUser: this.userInfo.info.login,
idTrajet: this.idTrajet,
}
};
this.http.post(this.url.manageUrl, JSON.stringify(options), headers)
.subscribe(() => {
this.storage.get('travels').then( (array: any[]) => {
if (!(array === null)) {
array.push(this.bestTravel);
} else {
array = [this.bestTravel];
}
this.storage.set('travels', array).then( () => {
});
});
},
(error: any) => {
console.log(error);
});
this.tts.speak({text: this.speech('forward'), locale: this.language, rate: 2}).then(() => {});
this.matches = [];
this.speechRecognition.startListening(this.options)
.subscribe(
(matches: string[]) => {
this.matches = matches;
const response = this.confirmation(matches[0]);
if (response) {
this.startTravelListening(this.language);
} else {
this.tts.speak({text: this.speech('sayThanks'), locale: this.language, rate: 2}).then(() => {
this.startTravelListening(this.language);
});
}
});
} else {
this.tts.speak({text: this.speech('sayThanks'), locale: this.language, rate: 2}).then(() => {
this.startTravelListening('fr-FR');
this.matches = [];
});
}
});
});
});
}
});
});
});
}
bestChoice(travels: any[]) {
if (travels === []) {
this.tts.speak({text: this.speech('notification'), locale: this.language, rate: 2}).then(() => {
this.matches = [];
this.speechRecognition.startListening(this.options)
.subscribe(
(matches: string[]) => {
this.matches = matches;
const response = this.confirmation(matches[0]);
if (response) {
this.tts.speak({text: this.speech('forward'), locale: this.language, rate: 2}).then(() => {
this.matches = [];
this.speechRecognition.startListening(this.options)
.subscribe(
(matches: string[]) => {
this.matches = matches;
const response = this.confirmation(matches[0]);
if (response) {
this.startTravelListening(this.language);
} else {
this.tts.speak({text: this.speech('sayThanks'), locale: this.language, rate: 2}).then(() => {
this.startTravelListening(this.language);
});
}
});
});
} else {
this.tts.speak({text: this.speech('sayThanks'), locale: this.language, rate: 2}).then(() => {
this.startTravelListening(this.language);
this.matches = [];
});
}
});
});
}
let passengers = [];
for (let index = 0; index < travels.length; index++) {
passengers.push(travels[index].NbPlace);
if (travels[index].NbPassagers === Math.max(...passengers)) {
this.idTrajet = travels[index];
return travels[index];
}
}
}
}