|
@@ -1,8 +1,6 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
5
|
|
-import java.util.HashMap;
|
|
3
|
+import java.util.*;
|
6
|
4
|
import java.util.regex.Matcher;
|
7
|
5
|
import java.util.regex.Pattern;
|
8
|
6
|
|
|
@@ -17,35 +15,95 @@ public class ItemParser {
|
17
|
15
|
return response;
|
18
|
16
|
}
|
19
|
17
|
|
20
|
|
- public ArrayList<ArrayList<String>> splitIntoPairs(ArrayList<String> list) {
|
|
18
|
+ public ArrayList<ArrayList<String>> splitIntoPairs(ArrayList<String> list) throws Exception{
|
|
19
|
+ int errorCount = 0; pattern = Pattern.compile(":;");
|
21
|
20
|
ArrayList<ArrayList<String>> finalArray = new ArrayList<>();
|
22
|
|
- for (int i = 0; i < list.size(); i++) {
|
|
21
|
+ for (String aList : list) {
|
23
|
22
|
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
|
|
- }
|
|
23
|
+ String[] tempArray = aList.split("[;^*!@%]");
|
|
24
|
+ Collections.addAll(tempArrayList, tempArray);
|
28
|
25
|
finalArray.add(tempArrayList);
|
29
|
|
-// tempArrayList.addAll(Arrays.asList(tempArray));
|
30
|
|
-// finalArray.get(i).addAll(tempArrayList);
|
31
|
26
|
}
|
32
|
27
|
return finalArray;
|
33
|
28
|
}
|
34
|
29
|
|
35
|
|
- public Item parse(ArrayList<String> arrayList) {
|
36
|
|
- String name; double price; String type; String expiration;
|
37
|
|
- for (String field : arrayList) {
|
38
|
|
- if (field.contains("Name")) {
|
39
|
|
- name = field.substring(field.indexOf(":"));
|
40
|
|
- } else if (field.contains("Price")) {
|
41
|
|
- price = field.ss
|
|
30
|
+ public HashSet<String> UniqueItems(ArrayList<Item> itemList) {
|
|
31
|
+ HashSet<String> itemNames = new HashSet<>();
|
|
32
|
+ for (Item item : itemList) {
|
|
33
|
+ itemNames.add(item.getName());
|
|
34
|
+ }
|
|
35
|
+ return itemNames;
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ public HashMap<Double, Integer> storePrices(ArrayList<Item> itemList, String name) {
|
|
39
|
+ HashMap<Double, Integer> priceMap = new HashMap<>();
|
|
40
|
+ for(Item item: itemList){
|
|
41
|
+ if(item.getName().equals(name)){
|
|
42
|
+ int val = priceMap.getOrDefault(item.getPrice(), 0);
|
|
43
|
+ priceMap.put(item.getPrice(), ++val);
|
42
|
44
|
}
|
43
|
45
|
}
|
|
46
|
+ return priceMap;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ public void printItemsPrices(HashSet<String> nameSet, ArrayList<Item> itemList) {
|
|
50
|
+//
|
|
51
|
+ for (String name : nameSet) {
|
|
52
|
+ HashMap<Double, Integer> priceMap = storePrices(itemList, name);
|
|
53
|
+ System.out.println("name: " + name + " seen: " + count(itemList, name) + " times");
|
|
54
|
+ System.out.println("============= =============");
|
|
55
|
+ for (Map.Entry<Double, Integer> e : priceMap.entrySet()) {
|
|
56
|
+ System.out.println("Price: " + e.getKey() + " seen: " + e.getValue() + " times");
|
|
57
|
+ System.out.println();
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ public int countErrors(ArrayList<Item> itemList, ArrayList<ArrayList<String>> newList) {
|
|
63
|
+ int count = 0;
|
|
64
|
+ Item item;
|
|
65
|
+ for (ArrayList<String> arrayList : newList) {
|
|
66
|
+ try {
|
|
67
|
+ item = parseStringIntoItem(arrayList);
|
|
68
|
+ if (item.getName() == (null) || item.getPrice() == 0 || item.getExpiration() == (null) || item.getType() == (null)) {
|
|
69
|
+ count++;
|
|
70
|
+ continue;
|
|
71
|
+ } else {
|
|
72
|
+ itemList.add(parseStringIntoItem(arrayList));
|
|
73
|
+ }
|
|
74
|
+ } catch (ItemParseException e) {
|
|
75
|
+ e.printStackTrace();
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ }
|
|
79
|
+ return count;
|
44
|
80
|
}
|
45
|
81
|
|
46
|
82
|
|
47
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
48
|
|
- return null;
|
|
83
|
+ public Item parseStringIntoItem(ArrayList<String> listOfFields) throws ItemParseException {
|
|
84
|
+ String name = null; double price = 0; String type = null; String expiration = null;
|
|
85
|
+ for (String field : listOfFields) {
|
|
86
|
+ field = correctSpelling(field);
|
|
87
|
+
|
|
88
|
+ if (field.contains("Name")) {
|
|
89
|
+ if (field.substring(field.indexOf(":")+1).equals("")){
|
|
90
|
+ continue;
|
|
91
|
+ } else {
|
|
92
|
+ name = field.substring(field.indexOf(":")+1);
|
|
93
|
+ }
|
|
94
|
+ } else if (field.contains("Price")) {
|
|
95
|
+ if (field.substring(field.indexOf(":")+1).equals("")) {
|
|
96
|
+ continue;
|
|
97
|
+ } else {
|
|
98
|
+ price = Double.valueOf(field.substring(field.indexOf(":")+1));
|
|
99
|
+ }
|
|
100
|
+ } else if (field.contains("Type")) {
|
|
101
|
+ type = field.substring(field.indexOf(":")+1);
|
|
102
|
+ } else if (field.contains("Expiration")) {
|
|
103
|
+ expiration = field.substring(field.indexOf(":")+1);
|
|
104
|
+ }
|
|
105
|
+ }
|
|
106
|
+ return new Item(name, price, type, expiration);
|
49
|
107
|
}
|
50
|
108
|
|
51
|
109
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
|
@@ -79,6 +137,21 @@ public class ItemParser {
|
79
|
137
|
return spellingCorrector.correctAllSpelling(text);
|
80
|
138
|
}
|
81
|
139
|
|
|
140
|
+ public int count(ArrayList<Item> itemList, String name) {
|
|
141
|
+ int count = 0;
|
|
142
|
+ for (Item item : itemList) {
|
|
143
|
+ if (item.getName().equals(name)) {
|
|
144
|
+ count ++;
|
|
145
|
+ }
|
|
146
|
+ }
|
|
147
|
+ return count;
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ public int[] countAllOccurences(String text) {
|
|
151
|
+ OccurenceCounter occurenceCounter = new OccurenceCounter();
|
|
152
|
+ return occurenceCounter.countOccurencesOfAllItemsAlphabeticalOrder(text);
|
|
153
|
+ }
|
|
154
|
+
|
82
|
155
|
class SpellingCorrector {
|
83
|
156
|
|
84
|
157
|
String correctMilk(String text) {
|
|
@@ -117,6 +190,18 @@ public class ItemParser {
|
117
|
190
|
return matcher.replaceAll("Price");
|
118
|
191
|
}
|
119
|
192
|
|
|
193
|
+ String correctType(String text) {
|
|
194
|
+ pattern = Pattern.compile("Type", Pattern.CASE_INSENSITIVE);
|
|
195
|
+ matcher = pattern.matcher(text);
|
|
196
|
+ return matcher.replaceAll("Type");
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ String correctExpiration(String text) {
|
|
200
|
+ pattern = Pattern.compile("Expiration", Pattern.CASE_INSENSITIVE);
|
|
201
|
+ matcher = pattern.matcher(text);
|
|
202
|
+ return matcher.replaceAll("Expiration");
|
|
203
|
+ }
|
|
204
|
+
|
120
|
205
|
String correctAllSpelling(String text) {
|
121
|
206
|
String corrected = correctBread(text);
|
122
|
207
|
corrected = correctName(corrected);
|
|
@@ -124,6 +209,8 @@ public class ItemParser {
|
124
|
209
|
corrected = correctCookies(corrected);
|
125
|
210
|
corrected = correctMilk(corrected);
|
126
|
211
|
corrected = correctPrice(corrected);
|
|
212
|
+ corrected = correctType(corrected);
|
|
213
|
+ corrected = correctExpiration(corrected);
|
127
|
214
|
return corrected;
|
128
|
215
|
}
|
129
|
216
|
}
|