|
@@ -2,30 +2,202 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.HashMap;
|
|
6
|
+import java.util.regex.Matcher;
|
|
7
|
+import java.util.regex.Pattern;
|
5
|
8
|
|
6
|
9
|
public class ItemParser {
|
7
|
10
|
|
|
11
|
+ private Pattern pattern;
|
|
12
|
+ private Matcher matcher;
|
8
|
13
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
14
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
10
|
15
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
16
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
12
|
17
|
return response;
|
13
|
18
|
}
|
14
|
19
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
20
|
+ public ArrayList<ArrayList<String>> splitIntoPairs(ArrayList<String> list) {
|
|
21
|
+ ArrayList<ArrayList<String>> finalArray = new ArrayList<>();
|
|
22
|
+ for (int i = 0; i < list.size(); i++) {
|
|
23
|
+ ArrayList<String> tempArrayList = new ArrayList<>();
|
|
24
|
+ String[] tempArray = list.get(i).split("[;^!@%]");
|
|
25
|
+ for (int j = 0; j < tempArray.length; j++){
|
|
26
|
+ tempArrayList.add(tempArray[j]);
|
|
27
|
+ }
|
|
28
|
+ finalArray.add(tempArrayList);
|
|
29
|
+// tempArrayList.addAll(Arrays.asList(tempArray));
|
|
30
|
+// finalArray.get(i).addAll(tempArrayList);
|
|
31
|
+ }
|
|
32
|
+ return finalArray;
|
17
|
33
|
}
|
18
|
34
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
|
- return response;
|
23
|
|
- }
|
24
|
35
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
|
- return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
|
- }
|
|
36
|
+// public ArrayList<String> splitPairs(ArrayList<String> textList) {
|
|
37
|
+// ArrayList<String> tempList = new ArrayList<>();
|
|
38
|
+// pattern = Pattern.compile("$[a-zA-Z0-9]");
|
|
39
|
+//
|
|
40
|
+// for (String string : textList) {
|
|
41
|
+// tempList.add(string.replace("$[a-zA-Z0-9]", ""));
|
|
42
|
+// }
|
|
43
|
+// return tempList;
|
|
44
|
+// }
|
|
45
|
+
|
|
46
|
+ public ArrayList<String> splitPairs2 (ArrayList < String > textList) {
|
|
47
|
+ ArrayList<String> tempList = new ArrayList<>();
|
|
48
|
+ for (String string : textList) {
|
|
49
|
+ String[] tempArray = string.split(":");
|
|
50
|
+ tempList.addAll(Arrays.asList(tempArray));
|
|
51
|
+ }
|
|
52
|
+ return tempList;
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+ public HashMap<String, String> createMap (ArrayList < String > list) {
|
|
57
|
+ HashMap<String, String> map = new HashMap<>();
|
|
58
|
+ pattern = Pattern.compile(":");
|
|
59
|
+ for (String item : list) {
|
|
60
|
+ correctSpelling(item);
|
|
61
|
+ String[] tempArray = pattern.split(item);
|
|
62
|
+ map.put(tempArray[0], tempArray[1]);
|
|
63
|
+ }
|
|
64
|
+ return map;
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ public Item parseStringIntoItem (String rawItem) throws ItemParseException {
|
|
68
|
+ return null;
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ public ArrayList<String> findKeyValuePairsInRawItemData (String rawItem){
|
|
72
|
+ String stringPattern = "[;|^]";
|
|
73
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
|
74
|
+ return response;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ public HashMap<String, String> mapKeyValuePairs (String rawItem){
|
|
78
|
+ ArrayList<String> groceryArrayList = parseRawDataIntoStringArray(rawItem);
|
|
79
|
+ pattern = Pattern.compile(":");
|
|
80
|
+// matcher = pattern.matcher(rawItem);
|
|
81
|
+ HashMap<String, String> groceryListValues = new HashMap<>();
|
|
82
|
+ for (String item : groceryArrayList) {
|
|
83
|
+ matcher = pattern.matcher(item);
|
|
84
|
+ try {
|
|
85
|
+ groceryListValues.put(matcher.group(), matcher.group());
|
|
86
|
+ } catch (IllegalStateException e) {
|
|
87
|
+ e.printStackTrace();
|
|
88
|
+ }
|
|
89
|
+ }
|
|
90
|
+ return groceryListValues;
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ private ArrayList<String> splitStringWithRegexPattern (String stringPattern, String inputString){
|
|
94
|
+ return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ public String correctSpelling (String text){
|
|
98
|
+ SpellingCorrector spellingCorrector = new SpellingCorrector();
|
|
99
|
+ return spellingCorrector.correctAllSpelling(text);
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ class SpellingCorrector {
|
28
|
103
|
|
|
104
|
+ String correctMilk(String text) {
|
|
105
|
+ pattern = Pattern.compile("Milk", Pattern.CASE_INSENSITIVE);
|
|
106
|
+ matcher = pattern.matcher(text);
|
|
107
|
+ return matcher.replaceAll("Milk");
|
|
108
|
+ }
|
29
|
109
|
|
|
110
|
+ String correctBread(String text) {
|
|
111
|
+ pattern = Pattern.compile("Bread", Pattern.CASE_INSENSITIVE);
|
|
112
|
+ matcher = pattern.matcher(text);
|
|
113
|
+ return matcher.replaceAll("Bread");
|
|
114
|
+ }
|
30
|
115
|
|
31
|
|
-}
|
|
116
|
+ String correctCookies(String text) {
|
|
117
|
+ pattern = Pattern.compile("Cookies|Co0kies", Pattern.CASE_INSENSITIVE);
|
|
118
|
+ matcher = pattern.matcher(text);
|
|
119
|
+ return matcher.replaceAll("Cookies");
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ String correctApples(String text) {
|
|
123
|
+ pattern = Pattern.compile("Apples", Pattern.CASE_INSENSITIVE);
|
|
124
|
+ matcher = pattern.matcher(text);
|
|
125
|
+ return matcher.replaceAll("Apples");
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ String correctName(String text) {
|
|
129
|
+ pattern = Pattern.compile("Name", Pattern.CASE_INSENSITIVE);
|
|
130
|
+ matcher = pattern.matcher(text);
|
|
131
|
+ return matcher.replaceAll("Name");
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ String correctPrice(String text) {
|
|
135
|
+ pattern = Pattern.compile("Price", Pattern.CASE_INSENSITIVE);
|
|
136
|
+ matcher = pattern.matcher(text);
|
|
137
|
+ return matcher.replaceAll("Price");
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ String correctAllSpelling(String text) {
|
|
141
|
+ String corrected = correctBread(text);
|
|
142
|
+ corrected = correctName(corrected);
|
|
143
|
+ corrected = correctApples(corrected);
|
|
144
|
+ corrected = correctCookies(corrected);
|
|
145
|
+ corrected = correctMilk(corrected);
|
|
146
|
+ corrected = correctPrice(corrected);
|
|
147
|
+ return corrected;
|
|
148
|
+ }
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ class OccurenceCounter {
|
|
152
|
+
|
|
153
|
+ int countOccurencesOfMilk(String text) {
|
|
154
|
+ int count = 0;
|
|
155
|
+ pattern = Pattern.compile("Milk", Pattern.CASE_INSENSITIVE);
|
|
156
|
+ matcher = pattern.matcher(text);
|
|
157
|
+ while (matcher.find()) {
|
|
158
|
+ count++;
|
|
159
|
+ }
|
|
160
|
+ return count;
|
|
161
|
+ }
|
|
162
|
+
|
|
163
|
+ int countOccurencesOfBread(String text) {
|
|
164
|
+ int count = 0;
|
|
165
|
+ pattern = Pattern.compile("Bread", Pattern.CASE_INSENSITIVE);
|
|
166
|
+ matcher = pattern.matcher(text);
|
|
167
|
+ while (matcher.find()) {
|
|
168
|
+ count++;
|
|
169
|
+ }
|
|
170
|
+ return count;
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ int countOccurencesOfApples(String text) {
|
|
174
|
+ int count = 0;
|
|
175
|
+ pattern = Pattern.compile("Apples", Pattern.CASE_INSENSITIVE);
|
|
176
|
+ matcher = pattern.matcher(text);
|
|
177
|
+ while (matcher.find()) {
|
|
178
|
+ count++;
|
|
179
|
+ }
|
|
180
|
+ return count;
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+ int countOccurencesOfCookies(String text) {
|
|
184
|
+ int count = 0;
|
|
185
|
+ pattern = Pattern.compile("Cookies| Co0kies|C00kies|C0okies", Pattern.CASE_INSENSITIVE);
|
|
186
|
+ matcher = pattern.matcher(text);
|
|
187
|
+ while (matcher.find()) {
|
|
188
|
+ count++;
|
|
189
|
+ }
|
|
190
|
+ return count;
|
|
191
|
+ }
|
|
192
|
+
|
|
193
|
+ int[] countOccurencesOfAllItemsAlphabeticalOrder(String text) {
|
|
194
|
+ int[] itemArray = new int[4];
|
|
195
|
+ itemArray[0] = countOccurencesOfApples(text);
|
|
196
|
+ itemArray[1] = countOccurencesOfBread(text);
|
|
197
|
+ itemArray[2] = countOccurencesOfCookies(text);
|
|
198
|
+ itemArray[3] = countOccurencesOfMilk(text);
|
|
199
|
+ return itemArray;
|
|
200
|
+ }
|
|
201
|
+ }
|
|
202
|
+
|
|
203
|
+ }
|