-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parse.java
1337 lines (1132 loc) · 49.1 KB
/
Parse.java
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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
public class Parse {
Indexer indexer;
HashMap<String, Term> namesAndEntities;
HashSet<String> stopWords;
//HashSet<String> allNumbersInCorpus;
/**
* this is a constructor
* @throws FileNotFoundException
*/
public Parse() throws FileNotFoundException {
indexer = new Indexer();
namesAndEntities = new HashMap<>();
stopWords = new HashSet<>();
// makeStopWordsHashSet();
}
/**
* this function make a hash map for the stop words from path
* @param path - list of stop words
* @throws FileNotFoundException
*/
public void makeStopWordsHashSet (String path)throws FileNotFoundException {
//"d:\\documents\\users\\shamaid\\Downloads\\posting with past indexer\\noPosting\\resource\\stop_words.txt"
Scanner in = new Scanner(new FileReader(path +"\\stop_words.txt"));
while (in.hasNextLine()){
stopWords.add(in.nextLine());
}
}
//public boolean isNumber(String word){
// return true;
//}
/* public String parseANumber (String number){
//return the number value after parsing without indexing
String newNumber = removeDotOrCommaFromLastChar(number);
return newNumber;
}*/
/**
* this function parse numbers
* @param termObject
* @param words
* @param i - index
* @param docID
* @param tagType
* @return
* @throws IOException
*/
public String parseANumber (Term termObject, String[] words , int i , String docID , String tagType) throws IOException {
//return the number value after parsing without indexing
words[i] = removeDotOrCommaFromLastChar(words[i]);
if(!words[i].equals("") && !words[i].equals("-")) {
if (isNumber(words[i])) {
removePunctuation(words, i);
if (isExpression(words, i)) {
termObject.word = typeOfExpression(words, i);
if (!termObject.isAlreadyParse) {
termObject.isAlreadyParse = true;
indexer.termToIndex(termObject);
}
return termObject.word;
} else if (isPrice(words, i)) {
termObject.word = typeOfPrice(words, i);
if (!termObject.isAlreadyParse) {
termObject.isAlreadyParse = true;
indexer.termToIndex(termObject);
}
return termObject.word;
} else if (isPercent(words, i)) {
termObject.word = typeOfPercent(words, i);
if (!termObject.isAlreadyParse) {
termObject.isAlreadyParse = true;
indexer.termToIndex(termObject);
}
return termObject.word;
} else if (isKgsOrKm(words, i)) {
termObject.word = typeOfKgs(words, i);
if (!termObject.isAlreadyParse) {
termObject.isAlreadyParse = true;
indexer.termToIndex(termObject);
}
return termObject.word;
} /*else if (isKms(words, i)) {
termObject.word = typeOfKms(words, i);
if (!termObject.isAlreadyParse) {
termObject.isAlreadyParse = true;
indexer.termToIndex(termObject);
}
return termObject.word;
}*/ else if (isGMT(words, i)) {
termObject.word = typeOfGMT(words, i);
if (!termObject.isAlreadyParse) {
termObject.isAlreadyParse = true;
indexer.termToIndex(termObject);
}
return termObject.word;
} else if (isDate(words, i)) {
termObject.word = typeOfDate(words, i);
if (!termObject.isAlreadyParse) {
termObject.isAlreadyParse = true;
indexer.termToIndex(termObject);
}
return termObject.word;
} else if (isRegularNumber(words, i)) {
termObject.word = typeOfRegularNumber(words, i);
if (!termObject.isAlreadyParse) {
termObject.isAlreadyParse = true;
indexer.termToIndex(termObject);
}
return termObject.word;
}
}//else it is a word
}
return "";
}
/**
* Separate text tab from doc file and return it
* @param doc
* @return array of separate words
*/
public String[] extractTextFromDoc(String doc) {
String[] separateWords = doc.split("\\s+");
//find the start index of TEXT tab (i) and final index of TEXT tab (j)
for (int i = 0 ; i<separateWords.length ; i++){
if (separateWords[i].equals("<TEXT>")){
for (int j=i+1 ; j<separateWords.length ; j++){
if (separateWords[j].equals("</TEXT>")){
String[] text = Arrays.copyOfRange(separateWords, i+1, j); //copy to new words array
return text;
}
}
}
}
return new String[0];
}
/**
* this function extract the document ID form document
* @param doc
* @return doc ID
*/
public String extractDocIDFromDoc(String doc){
String[] separateWords = doc.split("\\s+");
//find the start index of DOC tab (i) and final index of DOC tab (j)
for (int i = 0 ; i<separateWords.length ; i++){
if (separateWords[i].equals("<DOCNO>")){
String docNo = separateWords[i+1]; //put the next word into wordID
return docNo;
}
if (separateWords[i].contains("<DOCNO>")){
String docNo = separateWords[i].replaceAll("<DOCNO>" ,"");
docNo = docNo.replaceAll("</DOCNO>" ,"");
return docNo;
}
}
return "";
}
/**
* return each word of string on array
* @param doc
* @return each word
*/
public String [] putEachWordOnArray(String doc){
String [] separateWords = doc.split("\\s+");
return separateWords;
}
/**
* this function sent the doc to parse
* @param doc to be parse
* @throws IOException
*/
public void sendDocToParse (String doc) throws IOException {
//this.doc = doc;
//all the words between TEXT tab.
// (maybe in part B we will implement more function like it for more tabs)
String [] textWords = extractTextFromDoc(doc);
String docID = extractDocIDFromDoc(doc);
parser(textWords, docID,"TEXT"); //parse only TEXT tab
}
/**
* this function removes dots and commas from the last char if they exist
* @param term - to remove form
* @
*/
public String removeDotOrCommaFromLastChar (String term){
while(term.length() >= 2) {
String startOfTerm = term.substring(0, 2);
if (startOfTerm.equals("..") || startOfTerm.equals("//") || startOfTerm.equals("--") || startOfTerm.equals(",,") || startOfTerm.equals("$$") || startOfTerm.equals("%%")) {
term = term.substring(2);
}else {
break;
}
}
while(term.length() >= 1){
String startOfTerm = term.substring(0, 1);
if((startOfTerm.equals("-") && !isNumber(term)) || (startOfTerm.equals("$") && !isNumber(term)) || (startOfTerm.equals("%") && !isNumber(term))) {
term = term.substring(1);
}
else if (startOfTerm.equals(".") || startOfTerm.equals("/") || startOfTerm.equals(",")) {
term = term.substring(1);
}
else{
break;
}
}
//end of term
while(term.length() >=2){
String startOfTerm = term.substring(term.length()-2);
if(startOfTerm.equals("..") || startOfTerm.equals("//") || startOfTerm.equals("--") || startOfTerm.equals(",,") || startOfTerm.equals("$$") || startOfTerm.equals("%%")){
term=term.substring(0,term.length() - 2);
}
if(term.length() >=1) {
startOfTerm = term.substring(term.length() - 1);
if (startOfTerm.equals(".") || startOfTerm.equals("/") || startOfTerm.equals("-") || startOfTerm.equals(",") || startOfTerm.equals("$") || startOfTerm.equals("%")) {
term = term.substring(0, term.length() - 1);
} else {
break;
}
}
}
if(term.length() >=1) {
char lastChar = term.charAt(term.length() - 1);
String lastNot = term.substring(term.length() - 1);
// if (lastNot.equals("\'")) {
// words[i] = words[i].substring(0, words[i].length() - 1);
//}
if (lastChar == '.' || lastChar == ',' || lastChar == '"' || lastChar == '*' || lastChar == '\'' || lastChar == '-') {
term = term.substring(0, term.length() - 1);
if (term.contains("\"")) {
term = term.replaceAll("\"", "");
}
}
}
//System.out.println(words[i]);
if(term.equals("-") || term.equals("/") || term.equals("%") || term.equals("$")){
term="";
return term;
}
return term;
}
/**
* this function is combine all parse function here
* @param words
* @param docID
* @param tagType
* @throws IOException
*/
public void parser (String[] words,String docID, String tagType) throws IOException {
for (int i =0 ; i <words.length ; i ++) {
if (!words[i].equals("") && !stopWords.contains(words[i]) && !words[i].equals("-") || words[i].equals("between")) {
words[i] = removeDotOrCommaFromLastChar(words[i]); //remove dot or comma from the last char
if (!words[i].equals("") && !stopWords.contains(words[i]) && !words[i].equals("-") || words[i].equals("between")) {
if (!words[i].equals("-")) {
Term term = new Term(words[i], i, docID, tagType);
//if the string is a number
if (isNumber(words[i])) {
if (!words[i].equals("")) {
parseANumber(term, words, i, docID, tagType);
}
}//else it is a word
else {
//Check if it "-" expression
String lowerCaseWord = words[i].toLowerCase();
if (!parseRange(term, words, i, docID, tagType) && !stopWords.contains(lowerCaseWord)) {
term.isAlreadyParse = true;
indexer.termToIndex(term);
}
//Check if the first letter is capital letter
char firstLetter = words[i].charAt(0);
if (firstLetter >= 'A' && firstLetter <= 'Z') {
////////////call to parseNamesOrEntities function////////////////////
parseNamesOrEntities(term, words, i, docID, tagType);
///////////call to capitalOrSmallLetter function//////////////////////
}
}
}
else {
System.out.println("-");
}
}
}
}
}
/**
* check if it 'Names or entities'
* @return int - next i index to parse - if we find Entity - dont check the next indexes
*/
public int parseNamesOrEntities (Term term, String[] words, int wordIndex, String docID, String tagType) throws IOException {
//finding the next words with capital letter
int nextWordIndex = wordIndex + 1;
if (nextWordIndex >= words.length){ //there is no next word
return wordIndex;
}
int firstLetter = words[nextWordIndex].charAt(0);
//check if the next word start with capital letter
if (!(firstLetter >= 'A' && firstLetter <= 'Z')) {
return wordIndex;
}
//Make String with all the words in the expression
words[nextWordIndex] = removeDotOrCommaFromLastChar(words[nextWordIndex]);
String nameOrEntitie = words[wordIndex] + " " + words[nextWordIndex];
//putNameOrEntitieIntoHashMapOrIndexer(nameOrEntitie, wordIndex,docID,tagType); //put subString into indexer
//looking for the wordIndex of the last word with capital letter
nextWordIndex = nextWordIndex + 1;
if (nextWordIndex < words.length) { //there are more words in the array
firstLetter = words[nextWordIndex].charAt(0);
while (firstLetter >= 'A' && firstLetter <= 'Z') {
words[nextWordIndex] = removeDotOrCommaFromLastChar(words[nextWordIndex]);
nameOrEntitie = nameOrEntitie + " " + words[nextWordIndex]; //put on string
// putNameOrEntitieIntoHashMapOrIndexer(nameOrEntitie, wordIndex, docID, tagType); //put subString into indexer
nextWordIndex = nextWordIndex + 1;
if (nextWordIndex < words.length) { //there are more words in the array
firstLetter = words[nextWordIndex].charAt(0);
}
else{
firstLetter = 'a'; //no more words so get out from 'while loop'
}
}
}
Term entity = new Term(nameOrEntitie,wordIndex,docID,tagType);
entity.isEntity = true;
putNameOrEntitieIntoHashMapOrIndexer(entity, nameOrEntitie, wordIndex, docID, tagType);
return nextWordIndex-1;
}
/**
* this function put names and entities on hash map
* @param term
* @param nameOrEntitie
* @param wordIndex
* @param docID
* @param tagType
* @return
* @throws IOException
*/
public boolean putNameOrEntitieIntoHashMapOrIndexer (Term term, String nameOrEntitie, int wordIndex, String docID, String tagType) throws IOException {
//check if the expression is contains in the hashMap
//if not - put the expression into the hashMap and put also all details
//if yes - check if there are details also.
// if yes - send the old expression and the new expression to Indexer
// - remove the old expression details from the hashMap
// if not - send the expression to Indexer
if (!(namesAndEntities.containsKey(nameOrEntitie))){ //if not contain
namesAndEntities.put(nameOrEntitie, term);
}
else{ // if it contain
if (namesAndEntities.get(nameOrEntitie) == null){ // if no details
term.isAlreadyParse = true;
indexer.termToIndex(term);
return true;
}
else{
int oldWordIndex = namesAndEntities.get(nameOrEntitie).wordIndex;
String oldDocID = namesAndEntities.get(nameOrEntitie).docID;
String oldTagType = namesAndEntities.get(nameOrEntitie).tagType;
namesAndEntities.get(nameOrEntitie).isAlreadyParse = true;
/////////////////////////////////////// indexer.termToIndex(namesAndEntities.get(nameOrEntitie)); //insert the old term
term.isAlreadyParse = true;
indexer.termToIndex(term); //insert the new term
//remove details from the hashMap
namesAndEntities.replace(nameOrEntitie, null);
return true;
}
}
return true;
}
//Check if it a-z or A-Z words
/**
* this function Check if it "-" expression and parse it
* @param term
* @param words
* @param wordIndex
* @param docID
* @param tagType
* @return true - if it is, false if not
* @throws IOException
*/
public boolean parseRange (Term term, String[] words, int wordIndex, String docID, String tagType) throws IOException {
if (words[wordIndex].contains("-")){
if (words[wordIndex].charAt(0)!='-' && words[wordIndex].charAt(words[wordIndex].length()-1)!= '-') {
term.isAlreadyParse = true;
indexer.termToIndex(term);
return true;
}
}
else{
if (words[wordIndex].contains("between") || words[wordIndex].contains("Between")){ //The first word is 'between'
if (wordIndex+3 < words.length && isNumber(words[wordIndex+1])&&isNumber(words[wordIndex+3])){ //the second and the 4th word is a number
if (words [wordIndex+2].contains("and")){ //the third word is "and"
// String s = parseANumber(words[wordIndex+1]) + "-" + parseANumber(words[wordIndex+3]); //combine to "-" expression and parse numbers
String s = parseANumber(term , words,wordIndex+1 , docID , tagType) + "-" + parseANumber(term , words,wordIndex+3 , docID , tagType); //combine to "-" expression and parse numbers
term.isAlreadyParse = true;
indexer.termToIndex(term);
return true;
}
}
}
}
return false;
}
/**
* this function remove Punctuation
* @param words
* @param i - index
*/
public void removePunctuation(String[] words , int i){
words[i] = words[i].replaceAll("!","");
words[i] = words[i].replaceAll("\\?","");
words[i] = words[i].replaceAll("@","");
words[i] = words[i].replaceAll("#","");
words[i] = words[i].replaceAll("^","");
words[i] = words[i].replaceAll("&","");
words[i] = words[i].replaceAll("\\+","");
words[i] = words[i].replaceAll("=","");
words[i] = words[i].replaceAll("\\*","");
words[i] = words[i].replaceAll("\\(","");
words[i] = words[i].replaceAll("\\)","");
words[i] = words[i].replaceAll("\\|","");
words[i] = words[i].replaceAll("'","");
words[i] = words[i].replaceAll("`","");
if(words[i].contains("\\") && words[i].length()<3){
words[i] = words[i].replaceAll("\\\\","");
}
}
/**
* this function is checking if it is a number and parse it
* @param word
* @return true - if it is a number, false - if not
*/
public boolean isNumber(String word){
// if(word.matches("^((\\$)?[-]?)?+[0-9]+,?+[0-9]*+,?+[0-9]*+,?+[0-9]*+.?+[0-9]*+%?+$")){
/* if(word.matches("^([$%]?(.)?)[0-9]++$")){
if(!word.matches(".*[a-zA-Z].*")){
return true;
}
}*/
//^((\$)?[-]?)?.?+[0-9]*+,?+[0-9]*+,?+[0-9]*+,?+[0-9]*+.?+[0-9]*+%?+$
//^((\\$)?[-]?)?+[0-9]+,?+[0-9]*+,?+[0-9]*+,?+[0-9]*+.?+[0-9]*+%?+$"
//^([\\$%]?[-]?)?[0-9]*+,?+[0-9]*+,?+[0-9]*+,?+[0-9]*+.?+[0-9]*+%?+$
//^([$%]?[-]?)?[0-9]+,?+[0-9]*+,?+[0-9]*+,?+[0-9]*+.?+[0-9]*+%?+$" ////////OK
///^[0-9]*[$%]?[-]?[.]?+[0-9]+,?+[0-9]*+,?+[0-9]*+,?+[0-9]*+.?+[0-9]*+%?+$
//^[0-9]*(\$)?[-]?[0-9]+,?+[0-9]*+,?+[0-9]*+,?+[0-9]*+.?+[0-9]*+%?+$
//^[0-9]*(\$)?[-]?[0-9]+,?+[0-9]*+,?+[0-9]*+,?+[0-9]*+.?+[0-9]*+%?+$
//^(\$)?[-]?[0-9]+,?+[0-9]*+,?+[0-9]*+,?+[0-9]*+.?+[0-9]*+%?+$ //try after run
if(word.matches("^(\\$)?[-]?[0-9]+,?+[0-9]*+,?+[0-9]*+,?+[0-9]*+.?+[0-9]*+%?")){
if(!word.matches(".*[a-zA-Z].*") || word.contains("bn") || word.contains("m")){
return true;
}
}
return false ;
}
/**
* this function is checking if it is percent
* @param words
* @param i
* @return true - if it is, false - if not
*/
public boolean isPercent (String[] words, int i){
if(i+1 < words.length){
if(words[i+1].matches("^percent+$") || words[i+1].matches("^percentage+$")){
return true;
}
}
if(words[i].matches(".*%.*")){
return true;
}
return false;
}
/**
* this function is chrcking if it is a date
* @param words
* @param i
* @return true - if it is, false - if not
*/
public boolean isDate (String[] words, int i){
String []monthPart = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
String []monthFull = {"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
if(words[i].contains("-") || words[i].contains("/") || words[i].contains("%") || words[i].contains("$") || words[i].contains(".") || words[i].contains(",")){
return false;
}
for(int j=0 ; j<monthFull.length ; j++) {
if(i+1 < words.length) {
if ( (words[i + 1].toUpperCase().equals(monthFull[j]) && isNumber(words[i])) || (words[i + 1].toUpperCase().startsWith(monthPart[j]) && isNumber(words[i])))
return true;
}
if(i != 0) {
if ( (words[i - 1].toUpperCase().equals(monthFull[j]) && isNumber(words[i])) || (words[i-1].toUpperCase().startsWith(monthPart[j]) && isNumber(words[i])))
return true;
}
}
return false;
}
/**
* this function is checking if it is a price
* @param words
* @param i
* @return true - if it is, false - if not
*/
public boolean isPrice(String[] words , int i) {
if(words[i].contains("%")){
return false;
}
if(i+1 < words.length) {
if (words[i+1].matches("^Dollars+$")) {
return true;
}
}
if(i+2 < words.length) {
if (words[i+1].contains("/") && words[i+2].matches("^Dollars+$")) {
return true;
}
}
if(i+4 < words.length){
if(words[i+4].matches("^Dollars+$")) {
return true;
}
}
if( words[i].matches(".*[$].*"))
{
return true;
}
if(words[i].contains("m") || words[i].contains("bn")){
words[i] = words[i].replaceAll("m", "");
words[i] = words[i].replaceAll("bn", "");
return false;
}
return false;
}
/**
* this function is checking if it is a Kgs or kn
* @param words
* @param i
* @return true - if it is, false - if not
*/
public boolean isKgsOrKm(String[] words , int i){
if(i+1 < words.length){
if(words[i + 1].matches("^kgs+$") || words[i + 1].matches("^ton+$")
|| words[i + 1].matches("^tons+$") || words[i + 1].matches("^Kilograms+$")
|| words[i + 1].matches("^grams+$") || words[i + 1].matches("^km+$") || words[i + 1].matches("^meter+$")){
return true;
}
}
if(i+2 < words.length) {
if ((words[i + 2].matches("^kgs+$") || words[i + 2].matches("^ton+$")
|| words[i + 2].matches("^tons+$") || words[i + 2].matches("^Kilograms+$")
|| words[i + 2].matches("^grams+$") || words[i + 2].matches("^km+$") || words[i + 2].matches("^meter+$")) && words[i+1].contains("/")) {
return true;
}
}
return false;
}
public boolean isKms(String[] words , int i){
if(i+1 < words.length){
if(words[i + 1].matches("meter") || words[i + 1].matches("km")
|| words[i + 1].matches("kilometer") || words[i + 1].matches("centimeter")){
return true;
}
}
if(i+2 < words.length){
if((words[i + 2].matches("meter") || words[i + 2].matches("km")
|| words[i + 2].matches("kilometer") || words[i + 2].matches("centimeter")) && words[1].contains("/")){
return true;
}
}
return false;
}
public boolean isGMT(String[] words , int i){
if(i+1 < words.length){
if(words[i+1].equals("GMT"))
return true;
}
return false;
}
public boolean isRegularNumber(String[] words , int i){
if(words[i].contains("\\$") || words[i].contains("%") )
return false;
return true;
}
public boolean isExpression(String[] words , int i){
//if( ( words[i].contains("$") || words[i].contains("%") ) && words[i].contains("-"))
if(words[i].matches("^(-)?[$%]?[0-9]+-(-)?[$%]?[0-9]++$")) {
return true;
}
if( words[i].contains("-"))
return true;
else if( words[i].matches("^-?[0-9]*--?[0-9]*+$"))
return true;
return false;
}
public String typeOfExpression(String[] words , int i){
if(words[i].matches("^(-)?[$%]?[0-9]+-(-)?[$%]?[0-9]++$")) {
String firstWord = "";
String secondWord = "";
char[] charsOfExpression;
charsOfExpression = words[i].toCharArray();
for (int j = 0; j < charsOfExpression.length; j++) {
if (charsOfExpression[0] == '-') {
continue;
}
if (charsOfExpression[j] == '-') {
firstWord = words[i].substring(0, j);
secondWord = words[i].substring(j + 1);
break;
}
}
if (!firstWord.contains("$") && !firstWord.equals("")) {
if (!firstWord.contains("%")) {
firstWord = ExpressionToRegularNumber(firstWord);
}
}
if (!secondWord.contains("$") && !secondWord.equals("")) {
if (!secondWord.contains("%")) {
secondWord = ExpressionToRegularNumber(secondWord);
}
}
if (firstWord.contains("$") && firstWord.length() > 1) {
firstWord = ExpressionToPrice(firstWord);
}
if (secondWord.contains("$") && secondWord.length() > 1) {
secondWord = ExpressionToPrice(secondWord);
}
words[i] = firstWord + "-" + secondWord;
return words[i];
}
return words[i];
}
public String ExpressionToRegularNumber(String word){
if(word.contains("/")){
return word;
}
String toNumber;
toNumber = word.replaceAll(",", "");
toNumber = toNumber.replaceAll("'", "");
double number;
number = Double.parseDouble(toNumber);
if(number < 1000 && number >-1000){
if(word.contains("/")){
return word;
}
return numberType(number);
}
else if((number >= 1000 && number <1000000) || (number<=-1000 && number>-1000000)){
return numberType(number/1000)+ "K";
}
else if((number >= 1000000 && number < 1000000000) || (number<=-1000000 && number>-1000000000)){
return numberType(number/1000000)+ "M";
}
else if(number >= 1000000000 || number<=-1000000000){
return numberType(number/1000000000)+ "B";
}
return word;
}
public String ExpressionToPrice(String word){
double number;
String wordToNumber=word;
wordToNumber=wordToNumber.replaceAll("\\$","");
wordToNumber=wordToNumber.replaceAll(",","");
number = Double.parseDouble(wordToNumber);
if(number >1000000){
word = numberType(number/1000000) +"M Dollars";
}
else{
word = word +" Dollars";
}
return word;
}
public String typeOfRegularNumber(String[] words , int i){
String toNumber;
toNumber = words[i].replaceAll(",", "");
toNumber = toNumber.replaceAll("'", "");
//toNumber = toNumber.replaceAll(" " , "");
if(toNumber.contains("/")){
if(words[i].contains("/")){
return words[i];
}
if(i+1 < words.length) {
if (words[i + 1].contains("/")) {
return words[i] + " " + words[i + 1];
}
}
}
double number;
number = Double.parseDouble(toNumber);
if(i+1 < words.length) {
if (words[i + 1].matches("^Thousand+$")) {
return numberType(number) + "K";
}
if (words[i + 1].matches("^Million+$")) {
return numberType(number) + "M";
}
if (words[i + 1].matches("^Billion+$")) {
return numberType(number) + "B";
}
}
if(number < 1000 && number >-1000){
if(words[i].contains("/")){
return words[i];
}
if(i+1 < words.length) {
if (words[i + 1].contains("/") && isNumber(words[i+1])) {
return words[i] + " " + words[i + 1];
}
}
return numberType(number);
}
else if((number >= 1000 && number <1000000) || (number<=-1000 && number>-1000000)){
return numberType(number/1000)+ "K";
}
else if((number >= 1000000 && number < 1000000000) || (number<=-1000000 && number>-1000000000)){
return numberType(number/1000000)+ "M";
}
else if(number >= 1000000000 || number<=-1000000000){
return numberType(number/1000000000)+ "B";
}
return "NOT A REGULAR NUMBER";
}
public String numberType (double number){
DecimalFormat df = new DecimalFormat("#.###");
return df.format(number);
}
public String typeOfPrice(String[] words , int i) {
String toNumber;
toNumber = words[i].replaceAll("\\$", "");
double number;
if(overMillion(words,i)) {
toNumber = toNumber.replaceAll(",", "");
toNumber = toNumber.replaceAll("m", "");
toNumber = toNumber.replaceAll("bn", "");
if(words[i].contains("/")){
return words[i] ;
}
number = Double.parseDouble(toNumber);
if(i+1 < words.length) {
if (words[i].matches(".*m.*") && words[i + 1].matches("^Dollars+$"))
{
return numberType(number) + " M Dollars";
}
else if (words[i].matches(".*bn.*") && words[i + 1].matches("^Dollars+$"))
{
return numberType(number * 1000) + " M Dollars";
}
else if (words[i].matches(".*[$].*") && words[i + 1].matches(".*million.*"))
{
return numberType(number) + " M Dollars";
}
else if (words[i].matches(".*[$].*") && words[i + 1].matches(".*billion.*"))
{
return numberType(number * 1000) + " M Dollars";
}
else if (words[i + 1].matches("^million+$"))
{
return numberType(number) + " M Dollars";
}
else if (words[i + 1].matches("^billion+$"))
{
return numberType(number * 1000) + " M Dollars";
}
else if (words[i + 1].matches("^trillion+$"))
{
return numberType(number * 1000000) + " M Dollars";
}
else if (words[i + 1].matches("^Dollars+$"))
{
return numberType(number / 1000000) + " M Dollars";
}
}
else if(words[i].matches(".*[$].*"))
{
return numberType(number/1000000) +" M Dollars";
}
}
//less then million
else{
// number = Double.parseDouble(toNumber);
if(words[i].matches(".*[$].*"))
{
return toNumber + " Dollars";
}
//if there is a fraction
if(i+1 < words.length) {
if (words[i + 1].contains("/") && words[i + 2].matches("^Dollars+$"))
{
return toNumber + " " + words[i + 1] + " Dollars";
}
else if (words[i + 1].matches("^Dollars+$"))
{
return toNumber + " Dollars";
}
}
}//end else
return words[i];
}
public String typeOfPercent(String[] words , int i){
if(words[i].matches(".*%.*")){
return words[i];
}
if(i+1 < words.length) {
if (words[i + 1].matches("^percent+$") || words[i + 1].matches("^percentage+$")) {
return words[i] + "%";
}
}
return "NOT A PERCENT !";
}
public String typeOfDate (String[] words , int i){
String []month = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
int numOfMonth=0;
int number;
if(isNumber(words[i])){
for(int j=0 ; j<month.length ; j++){
if(i+1 < words.length) {
if (words[i + 1].toUpperCase().contains(month[j])) {
numOfMonth = j + 1;
//words[i + 1] = "";
break;
}
}
if(i != 0){
if(words[i-1].toUpperCase().contains(month[j]) ){
numOfMonth=j+1;
// words[i-1]="";
break;
}
}
}
number = Integer.parseInt(words[i]);
if(number>31){//years
if(numOfMonth <10) {
return words[i] + "-0" + numOfMonth;
}else{
return words[i] + "-" + numOfMonth;