Browse Source

Right before the kiss of death

JaseG256 6 years ago
parent
commit
086377c1b5
1 changed files with 118 additions and 138 deletions
  1. 118
    138
      src/main/java/io/zipcoder/ItemParser.java

+ 118
- 138
src/main/java/io/zipcoder/ItemParser.java View File

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