-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguageParser.cpp
More file actions
1129 lines (1099 loc) · 43.5 KB
/
Copy pathLanguageParser.cpp
File metadata and controls
1129 lines (1099 loc) · 43.5 KB
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdexcept>
#include <algorithm>
#include <cmath>
#include <iostream>
#include "LanguageParser.h"
/*
void LanguageParser::initCustomTargets(){
LanguageParser::customCommandTargets = std::vector<std::string>();
}
*/
//This does not support decimals in the form of "and xx hundredths". Only digit construction supported after decimal point.
long double LanguageParser::parseNumber(std::string number, const bool& isTime, size_t* _oldPos){
//This is gonna be a lot of ifs because English numbers are funky.
if(number.back()!=' '){
number.push_back(' ');
}
long double intNum=0, actingNum=0;
int negativeMultiplier=1;
/* usingDigitConstruction codes:
* -1: number has been detected to not use digit construction
* 0: unsure
* 1: primed (depending on what follows this number uses digit construction)
* 2: using digit construction (user is listing off digits)
* 3: Now working with decimals (only digit construction supported)
*/
int usingDigitConstruction=0;
bool x10Waiting=false;
bool consecutive100=false;
int currentMagnitude=40;
bool usedTimeWords=false;
bool beginsWithZero=false;
size_t pos=0, oldPos=(_oldPos!=nullptr? *_oldPos: 0);
std::string word;
bool error=false; //If this is true, then we found a word we couldn't parse or the number the user said doesn't make sense.
while((pos=number.find(' ', oldPos))!=std::string::npos){
word=number.substr(oldPos, pos - oldPos);
//std::cout <<" word: " << word << std::endl;
if(word=="zero" || word=="oh" || word=="o"){
if(intNum==0 && actingNum==0){ beginsWithZero=true; }
usingDigitConstruction=2; //"0" is only ever said when listing off digits
actingNum=actingNum * 10;
x10Waiting=false;
}else if(word=="one" || word=="a"){
if(usingDigitConstruction >= 2){
if(!x10Waiting){
actingNum=actingNum * 10 + 1;
if(usingDigitConstruction==3){ currentMagnitude-=1; }
}else{
actingNum+=1;
}
}else{
if(usingDigitConstruction==1){
usingDigitConstruction=2;
actingNum=actingNum * 10 + 1;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=1;
}
}
x10Waiting=false;
}else if(word=="two" || word=="to" || word=="too"){
if(usingDigitConstruction >= 2){
if(!x10Waiting){
actingNum=actingNum * 10 + 2;
if(usingDigitConstruction==3){ currentMagnitude-=1; }
}else{
actingNum+=2;
}
}else{
if(usingDigitConstruction==1){
usingDigitConstruction=2;
actingNum=actingNum * 10 + 2;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=2;
}
}
x10Waiting=false;
}else if(word=="three"){
if(usingDigitConstruction >= 2){
if(!x10Waiting){
actingNum=actingNum * 10 + 3;
if(usingDigitConstruction==3){ currentMagnitude-=1; }
}else{
actingNum+=3;
}
}else{
if(usingDigitConstruction==1){
usingDigitConstruction=2;
actingNum=actingNum * 10 + 3;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=3;
}
}
x10Waiting=false;
}else if(word=="four"){
if(usingDigitConstruction >= 2){
if(!x10Waiting){
actingNum=actingNum * 10 + 4;
if(usingDigitConstruction==3){ currentMagnitude-=1; }
}else{
actingNum+=4;
}
}else{
if(usingDigitConstruction==1){
usingDigitConstruction=2;
actingNum=actingNum * 10 + 4;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=4;
}
}
x10Waiting=false;
}else if(word=="five"){
if(usingDigitConstruction >= 2){
if(!x10Waiting){
actingNum=actingNum * 10 + 5;
if(usingDigitConstruction==3){ currentMagnitude-=1; }
}else{
actingNum+=5;
}
}else{
if(usingDigitConstruction==1){
usingDigitConstruction=2;
actingNum=actingNum * 10 + 5;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=5;
}
}
x10Waiting=false;
}else if(word=="six"){
if(usingDigitConstruction >= 2){
if(!x10Waiting){
actingNum=actingNum * 10 + 6;
if(usingDigitConstruction==3){ currentMagnitude-=1; }
}else{
actingNum+=6;
}
}else{
if(usingDigitConstruction==1){
usingDigitConstruction=2;
actingNum=actingNum * 10 + 6;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=6;
}
}
x10Waiting=false;
}else if(word=="seven"){
if(usingDigitConstruction >= 2){
if(!x10Waiting){
actingNum=actingNum * 10 + 7;
if(usingDigitConstruction==3){ currentMagnitude-=1; }
}else{
actingNum+=7;
}
}else{
if(usingDigitConstruction==1){
usingDigitConstruction=2;
actingNum=actingNum * 10 + 7;
if(usingDigitConstruction==3){ currentMagnitude-=1; }
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=7;
}
}
x10Waiting=false;
}else if(word=="eight"){
if(usingDigitConstruction >= 2){
if(!x10Waiting){
actingNum=actingNum * 10 + 8;
if(usingDigitConstruction==3){ currentMagnitude-=1; }
}else{
actingNum+=8;
}
}else{
if(usingDigitConstruction==1){
usingDigitConstruction=2;
actingNum=actingNum * 10 + 8;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=8;
}
}
x10Waiting=false;
}else if(word=="nine"){
if(usingDigitConstruction >= 2){
if(!x10Waiting){
actingNum=actingNum * 10 + 9;
if(usingDigitConstruction==3){ currentMagnitude-=1; }
}else{
actingNum+=9;
}
}else{
if(usingDigitConstruction==1){
usingDigitConstruction=2;
actingNum=actingNum * 10 + 9;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=9;
}
}
x10Waiting=false;
}else if(word=="ten"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 10;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 10;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=10;
}
}
x10Waiting=false;
}else if(word=="eleven"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 11;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 11;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=11;
}
}
x10Waiting=false;
}else if(word=="twelve"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 12;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 12;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=12;
}
}
x10Waiting=false;
}else if(word=="thirteen"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 13;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 13;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=13;
}
}
x10Waiting=false;
}else if(word=="fourteen"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 14;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 14;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=14;
}
}
x10Waiting=false;
}else if(word=="fifteen"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 15;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 15;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=15;
}
}
x10Waiting=false;
}else if(word=="sixteen"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 16;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 16;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=16;
}
}
x10Waiting=false;
}else if(word=="seventeen"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 17;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 17;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=17;
}
}
x10Waiting=false;
}else if(word=="eighteen"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 18;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 18;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=18;
}
}
x10Waiting=false;
}else if(word=="nineteen"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 19;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 19;
}else{
if(usingDigitConstruction==0){ usingDigitConstruction=1; }
actingNum+=19;
}
}
x10Waiting=false;
}else if(word=="twenty"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 20;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 20;
}else{
actingNum+=20;
}
}
x10Waiting=true;
}else if(word=="thirty"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 30;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 30;
}else{
actingNum+=30;
}
}
x10Waiting=true;
}else if(word=="forty"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 40;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 40;
}else{
actingNum+=40;
}
}
x10Waiting=true;
}else if(word=="fifty"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 50;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 50;
}else{
actingNum+=50;
}
}
x10Waiting=true;
}else if(word=="sixty"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 60;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 60;
}else{
actingNum+=60;
}
}
x10Waiting=true;
}else if(word=="seventy"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 70;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 70;
}else{
actingNum+=70;
}
}
x10Waiting=true;
}else if(word=="eighty"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 80;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 80;
}else{
actingNum+=80;
}
}
x10Waiting=true;
}else if(word=="ninety"){
if(usingDigitConstruction >= 2){
actingNum=actingNum * 100 + 90;
if(usingDigitConstruction==3){ currentMagnitude-=2; }
}else{
if(x10Waiting || usingDigitConstruction==1){
if(usingDigitConstruction==-1){ error=true; }
usingDigitConstruction=2;
actingNum=actingNum * 100 + 90;
}else{
actingNum+=90;
}
}
x10Waiting=true;
}else if(word=="hundred"){
if(x10Waiting || consecutive100 || usingDigitConstruction >= 2){
error=true;
}else{
if(actingNum >= 10 && intNum!=0){
error=true;
}else{
actingNum*=100;
}
}
x10Waiting=false;
consecutive100=true;
usingDigitConstruction=-1;
}else if(word=="thousand"){
if(usingDigitConstruction==3){
intNum+=actingNum * (std::pow(10, currentMagnitude));
actingNum=0;
}else if(currentMagnitude > 3){
intNum+=actingNum * std::pow(10, 3);
actingNum=0;
currentMagnitude=3;
}else{
error=true;
}
x10Waiting=false;
consecutive100=false;
usingDigitConstruction=-1;
}else if(word=="million"){
if(usingDigitConstruction==3){
intNum+=actingNum * (std::pow(10, currentMagnitude));
actingNum=0;
}else if(currentMagnitude > 6){
intNum+=actingNum * std::pow(10, 6);
actingNum=0;
currentMagnitude=6;
}else{
error=true;
}
x10Waiting=false;
consecutive100=false;
usingDigitConstruction=-1;
}else if(word=="billion"){
if(usingDigitConstruction==3){
intNum+=actingNum * (std::pow(10, currentMagnitude));
actingNum=0;
}else if(currentMagnitude > 9){
intNum+=actingNum * std::pow(10, 9);
actingNum=0;
currentMagnitude=9;
}else{
error=true;
}
x10Waiting=false;
consecutive100=false;
usingDigitConstruction=-1;
}else if(word=="trillion"){
if(usingDigitConstruction==3){
intNum+=actingNum * (std::pow(10, currentMagnitude));
actingNum=0;
}else if(currentMagnitude > 12){
intNum+=actingNum * std::pow(10, 12);
actingNum=0;
currentMagnitude=12;
}else{
error=true;
}
x10Waiting=false;
consecutive100=false;
usingDigitConstruction=-1;
}else if(word=="quadrillion"){
if(usingDigitConstruction==3){
intNum+=actingNum * (std::pow(10, currentMagnitude));
actingNum=0;
}else if(currentMagnitude > 15){
intNum+=actingNum * std::pow(10, 15);
actingNum=0;
currentMagnitude=15;
}else{
error=true;
}
x10Waiting=false;
consecutive100=false;
usingDigitConstruction=-1;
}else if(word=="quintillion"){
if(usingDigitConstruction==3){
intNum+=actingNum * (std::pow(10, currentMagnitude));
actingNum=0;
}else if(currentMagnitude > 18){
intNum+=actingNum * std::pow(10, 18);
actingNum=0;
currentMagnitude=18;
}else{
error=true;
}
x10Waiting=false;
consecutive100=false;
usingDigitConstruction=-1;
}else if(word=="sextillion"){
if(usingDigitConstruction==3){
intNum+=actingNum * (std::pow(10, currentMagnitude));
actingNum=0;
}else if(currentMagnitude > 21){
intNum+=actingNum * std::pow(10, 21);
actingNum=0;
currentMagnitude=21;
}else{
error=true;
}
x10Waiting=false;
consecutive100=false;
usingDigitConstruction=-1;
}else if(word=="septillion"){
if(usingDigitConstruction==3){
intNum+=actingNum * (std::pow(10, currentMagnitude));
actingNum=0;
}else if(currentMagnitude > 24){
intNum+=actingNum * std::pow(10, 24);
actingNum=0;
currentMagnitude=24;
}else{
error=true;
}
x10Waiting=false;
consecutive100=false;
usingDigitConstruction=-1;
}else if(word=="octillion"){
if(usingDigitConstruction==3){
intNum+=actingNum * (std::pow(10, currentMagnitude));
actingNum=0;
}else if(currentMagnitude > 27){
intNum+=actingNum * std::pow(10, 27);
actingNum=0;
currentMagnitude=27;
}else{
error=true;
}
x10Waiting=false;
consecutive100=false;
usingDigitConstruction=-1;
}else if(word=="nonillion"){
if(usingDigitConstruction==3){
intNum+=actingNum * (std::pow(10, currentMagnitude));
actingNum=0;
}else if(currentMagnitude > 30){
intNum+=actingNum * std::pow(10, 30);
actingNum=0;
currentMagnitude=30;
}else{
error=true;
}
x10Waiting=false;
consecutive100=false;
usingDigitConstruction=-1;
}else if(word=="decillion"){
if(usingDigitConstruction==3){
intNum+=actingNum * (std::pow(10, currentMagnitude));
actingNum=0;
}else if(currentMagnitude > 33){
intNum+=actingNum * std::pow(10, 33);
actingNum=0;
currentMagnitude=33;
}else{
error=true;
}
x10Waiting=false;
consecutive100=false;
usingDigitConstruction=-1;
}else if(word=="and" || word=="n" || word=="nd"){
//do nothing here, ignore it
}else if(word=="point"){
intNum+=actingNum;
actingNum=0;
usingDigitConstruction=3;
currentMagnitude=0;
x10Waiting=false;
consecutive100=false;
}else if(word=="negative"){
if(intNum==0 && actingNum==0 && negativeMultiplier==1){
negativeMultiplier=-1;
}else{
error=true;
}
}else if(isTime && (word=="hour" || word=="hours" || word=="minute" || word=="minutes" || word=="second" || word=="seconds")){
//std::cout << " actingNum= " << std::to_string(actingNum) << std::endl;
//std::cout << " intNum= " << std::to_string(intNum) << std::endl;
usedTimeWords=true;
if(word=="hour" || word=="hours"){
actingNum+=intNum;
if(actingNum >= 100 && actingNum <= 2400){
intNum=(floor(actingNum / 100) * 60 + fmod(actingNum, 100)) * 60; //--TODO: MAYBE THIS LINE IS A BIT WRONG--
}else if(actingNum > 2400){
//}else if(actingNum > 2400 || actingNum > 24){
//Guards against both setting over 24 hours and asking for a time over 24:00=00:00
error=true;
}else{
intNum=actingNum * 60 * 60;
}
}else if(word=="minute" || word=="minutes"){
actingNum+=intNum;
intNum=actingNum * 60;
}else if(word=="second" || word=="seconds"){
intNum+=actingNum;
}
actingNum=0;
}else if(isTime && (word=="AM" || word=="PM")){
//std::cout << " actingNum= " << std::to_string(actingNum) << std::endl;
//std::cout << " intNum= " << std::to_string(intNum) << std::endl;
usedTimeWords=true;
if(actingNum >= 0 && actingNum < 60){
if(beginsWithZero){
//It's minutes and the input was meant to be "00:xx"
intNum=actingNum * 60;
}else{
intNum=actingNum * 60 * 60;
}
actingNum = 0;
}else if(actingNum >= 100 && actingNum < 6060){
intNum=(floor(actingNum / 100) * 60 + fmod(actingNum, 100)) * 60;
}else if(actingNum >= 10000 && actingNum < 240000){
intNum=(floor(actingNum / 10000) * 60 + floor(fmod(actingNum, 10000) / 100)) * 60 + fmod(actingNum, 100);
}else{
error=true;
}
if(word=="AM"){
intNum+=actingNum;
}else if(word=="PM"){
intNum+=actingNum + (12 * 60 * 60);
}
actingNum=0;
break; //--TODO: Will this cause problems?--
}else{
//Specifically don't update oldPos so we can parse the word again after.
break;
}
//std::cout << " actingNum= " << std::to_string(actingNum) << std::endl;
//std::cout << " intNum= " << std::to_string(intNum) << std::endl;
if(error){ break; }
oldPos=pos + 1;
}
if(isTime && !usedTimeWords){
usedTimeWords=true;
//std::cout << " actingNum= " << std::to_string(actingNum) << std::endl;
//std::cout << " intNum= " << std::to_string(intNum) << std::endl;
if(actingNum >= 0 && actingNum < 60){
if(beginsWithZero){
//It's minutes and the input was meant to be "00:xx"
intNum=actingNum * 60;
}else{
intNum=actingNum * 60 * 60;
}
actingNum = 0;
}else if(actingNum >= 100 && actingNum < 6060){
intNum=(floor(actingNum / 100) * 60 + fmod(actingNum, 100)) * 60;
}else if(actingNum >= 10000 && actingNum < 240000){
intNum=(floor(actingNum / 10000) * 60 + floor(fmod(actingNum, 10000) / 100)) * 60 + fmod(actingNum, 100);
}else{
error=true;
}
intNum+=actingNum;
actingNum=0;
}
if(error){ throw std::invalid_argument("Number not understood"); }
if(currentMagnitude < 0){
actingNum*=(std::pow(10, currentMagnitude));
}
intNum+=actingNum;
if(_oldPos!=nullptr){ *_oldPos=oldPos; }
return intNum * negativeMultiplier;
}
LanguageParser::Command LanguageParser::parseCommand(std::string command){
//Once again a lot of if statements because English
//A laughable parody of natural language comprehension but I'm only one person and we never really promised natural language comprehension in the first place
/* KNOWN ISSUES:
* Will NOT behave correctly when complement of "to" or "that" phrases contains words checked for in wordMatchesComplementStart() when "to" or "that" is not the last complement.
* Using "o'clock" or a variant will cause an error.
*/
if(command.back()!=' '){
command.push_back(' ');
}
ACTIONS action;
std::string target;
long double complement1, complement2;
std::string complement3;
COMPLEMENT_TYPE complement1Type = COMPLEMENT_TYPE::EMPTY, complement2Type = COMPLEMENT_TYPE::EMPTY, complement3Type = COMPLEMENT_TYPE::EMPTY;
DAY_OF_WEEK dayOfWeek = DAY_OF_WEEK::NO_DATE;
bool lookingForNumber = false;
if(command.empty()){ throw std::invalid_argument("Empty command"); }
size_t pos=0, oldPos=0;
std::string word;
bool error=false; //If this is true, then we found a word we couldn't parse or the command the user said doesn't make sense.
std::string errorMessage="Command not understood or unsupported command";
if((pos=command.find(' ', oldPos))!=std::string::npos){ //The first word of a command is always the action.
word=command.substr(oldPos, pos - oldPos);
//std::cout << word << std::endl;
if(word=="make" || word=="set" || word=="create" || word=="add"){
action=ACTIONS::CREATE;
}else if(word=="delete" || word=="remove"){
//action=ACTIONS::DELETE; //Turns out this is a lot harder than we thought
action=ACTIONS::BADACTION;
error=true;
errorMessage = "Unsupported command";
}else if(word=="edit"){
action=ACTIONS::EDIT;
}else if(word=="start"){
action=ACTIONS::START;
}else if(word=="stop" || word=="cancel"){
//action=ACTIONS::STOP; //Also hard to implement
action=ACTIONS::BADACTION;
error=true;
errorMessage = "Unsupported command";
}else if(word=="remind"){
action=ACTIONS::REMIND;
}else{
action=ACTIONS::BADACTION;
error=true;
}
oldPos=pos + 1;
}else{
action=ACTIONS::BADACTION;
error=true;
}
//Next while loop to parse the target.
if(action!=ACTIONS::REMIND){ //"remind" action implies the target is a reminder.
while((pos=command.find(' ', oldPos))!=std::string::npos && !error){
word=command.substr(oldPos, pos - oldPos);
//std::cout << word << std::endl;
if(LanguageParser::wordMatchesSkippable(word)){
//This is the ignore list. To begin, we can ignore all determiners since a command system doesn't care about them.
oldPos=pos + 1;
}else{
try{
target=LanguageParser::matchCommandTarget(command, &oldPos);
}catch(const std::invalid_argument& e){
error=true;
errorMessage=e.what();
}
oldPos=pos + 1;
break;
}
}
}else{
target="reminder";
}
//std::cout << "Target set: " << target << std::endl;
//Next while loop to parse first complement.
while((pos=command.find(' ', oldPos))!=std::string::npos && !error){
word=command.substr(oldPos, pos - oldPos);
//std::cout << word << std::endl;
if(LanguageParser::wordMatchesSkippable(word) || word == "on"){
//This is the ignore list. To begin, we can ignore all determiners since a command system doesn't care about them.
//Turns out we also don't care about the word "on"
}else if(word=="at"){
//Parse for time
complement1Type = COMPLEMENT_TYPE::TIME_START;
oldPos=pos + 1;
try{
complement1=LanguageParser::parseNumber(command, true, &oldPos);
}catch(const std::invalid_argument& e){
error=true;
errorMessage=e.what();
}
continue; //! parseNumber sets oldPos to just before the next word, so we can parse it here next.
}else if(word=="from"){
//Parse for time
complement1Type = COMPLEMENT_TYPE::TIME_END;
oldPos=pos + 1;
try{
complement1=LanguageParser::parseNumber(command, true, &oldPos);
}catch(const std::invalid_argument& e){
error=true;
errorMessage=e.what();
}
continue;
}else if(word=="to"){
//Check target for context: either time or VP[+inf]
if(complement1Type == COMPLEMENT_TYPE::TIME_START && complement2Type != COMPLEMENT_TYPE::TIME_END){
complement2Type = COMPLEMENT_TYPE::TIME_END;
oldPos=pos + 1;
try{
complement2=LanguageParser::parseNumber(command, true, &oldPos);
}catch(const std::invalid_argument& e){
error=true;
errorMessage=e.what();
}
continue;
}else{
complement3Type = COMPLEMENT_TYPE::TEXT;
complement3 = command.substr(pos+1, command.length()-pos-1);
break;
}
}else if(word=="until" || word=="till"){
//Parse for time
complement2Type = COMPLEMENT_TYPE::TIME_END;
oldPos=pos + 1;
try{
complement2=LanguageParser::parseNumber(command, true, &oldPos);
}catch(const std::invalid_argument& e){
error=true;
errorMessage=e.what();
}
continue;
}else if(word=="for"){
//parse for time (relative)
if(complement1Type == COMPLEMENT_TYPE::EMPTY){
complement1Type=COMPLEMENT_TYPE::TIME_LEN;
oldPos=pos + 1;
try{
complement1=LanguageParser::parseNumber(command, true, &oldPos);
}catch(const std::invalid_argument& e){
error=true;
errorMessage=e.what();
}
}else{
//Allowing things like "create an appointment at 10PM for 3 hours"
complement2Type=COMPLEMENT_TYPE::TIME_LEN;
oldPos=pos + 1;
try{
complement2=LanguageParser::parseNumber(command, true, &oldPos);
}catch(const std::invalid_argument& e){
error=true;
errorMessage=e.what();
}
}
continue;
}else if(word=="in"){
//parse for time (relative)
complement1Type = COMPLEMENT_TYPE::TIME_REL;
oldPos=pos + 1;
try{
complement1=LanguageParser::parseNumber(command, true, &oldPos);
}catch(const std::invalid_argument& e){
error=true;
errorMessage=e.what();
}
continue;
//}else if(word=="on"){
//parse for date/time (half-absolute)
//We actually don't care about "on", so it's now checked for with the rest of the ignore list.
}else if(word=="that"){
//parse for TP[+fin]
complement3Type = COMPLEMENT_TYPE::TEXT;
complement3 = command.substr(pos+1, command.length()-pos-1);
break;
}else if(LanguageParser::wordMatchesDayOfWeek(word) != DAY_OF_WEEK::NO_DATE){
dayOfWeek = LanguageParser::wordMatchesDayOfWeek(word);
lookingForNumber = true;
}else if(lookingForNumber && !LanguageParser::wordMatchesComplementStart(word)){
//! There is a chance that we're looking for a time now
lookingForNumber = false;
complement1Type = COMPLEMENT_TYPE::TIME_START; //! If no complement start specified, then this is "[day of week] [time]" with implied "at"
oldPos=pos + 1;
try{
complement1=LanguageParser::parseNumber(command, true, &oldPos);
}catch(const std::invalid_argument& e){
error=true;
errorMessage=e.what();
}
continue;
}else{
error=true;
}
if(error){ break; }
oldPos=pos + 1;
}
if(error){ throw std::invalid_argument(errorMessage); }
return (LanguageParser::Command){
action,
target,
(int)std::lroundl(complement1),
(int)std::lroundl(complement2),
complement3,
dayOfWeek,
complement1Type,
complement2Type,
complement3Type
};
}
void LanguageParser::executeCommand(const LanguageParser::Command& command){
//--TODO: IMPLEMENT THIS!--
bool error = false;
if(command.target == "timer"){
switch(command.action){
case ACTIONS::CREATE:
case ACTIONS::START:
if(command.param1Type == COMPLEMENT_TYPE::TIME_LEN){
std::cout << "create a timer for " << std::to_string(command.param1) << " seconds" << std::endl;
//Create a timer for command.param1 seconds
}else{ error = true; }
break;
case ACTIONS::EDIT:
break;
default:
error = true;
}
}else if(command.target == "reminder"){
switch(command.action){
case ACTIONS::REMIND:
case ACTIONS::CREATE:
break;
case ACTIONS::EDIT:
break;
default:
error = true;
}
}else if(command.target == "appointment" || command.target == "event"){
switch(command.action){
case ACTIONS::CREATE:
break;
case ACTIONS::EDIT:
break;