Sfoglia il codice sorgente

about to delete some methods

JaseG256 6 anni fa
parent
commit
0edbde6539

+ 12
- 0
pom.xml Vedi File

@@ -7,6 +7,18 @@
7 7
     <groupId>io.zipcoder</groupId>
8 8
     <artifactId>PainfullAfternoon</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 185
- 13
src/main/java/io/zipcoder/ItemParser.java Vedi File

@@ -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
+    }

+ 55
- 1
src/main/java/io/zipcoder/Main.java Vedi File

@@ -2,6 +2,12 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.util.ArrayList;
6
+import java.util.HashMap;
7
+import java.util.Map;
8
+import java.util.regex.Matcher;
9
+import java.util.regex.Pattern;
10
+
5 11
 
6 12
 public class Main {
7 13
 
@@ -13,7 +19,55 @@ public class Main {
13 19
 
14 20
     public static void main(String[] args) throws Exception{
15 21
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
22
+        ItemParser itemParser = new ItemParser();
23
+        ArrayList<String> whatever = itemParser.parseRawDataIntoStringArray(output);
24
+//        whatever.forEach(System.out::println);
25
+        HashMap<String, String> map = new HashMap<>();
26
+        ArrayList<ArrayList<String>> newList = itemParser.splitIntoPairs(whatever);
27
+//        ArrayList<String> parsed = itemParser.splitPairs2(newList);
28
+//        parsed.forEach(System.out::println);
29
+        System.out.println(newList.get(0).get(0));
30
+        System.out.println();
31
+        newList.forEach(System.out::println);
32
+
33
+
34
+//        map = itemParser.createMap(newList);
35
+//        map.entrySet().iterator().forEachRemaining(System.out::println);
36
+//        System.out.println(map.keySet());
37
+
38
+//        System.out.println(itemParser.splitIntoPairs(whatever));
39
+//        Pattern pattern = Pattern.compile("[:;@^*%]");
40
+//        Matcher matcher = pattern.matcher(output);
41
+//        HashMap<String, String> map = new HashMap<>();
42
+//        String[] temp = pattern.split(whatever.get(0));
43
+//        map.put(temp[0], temp[1]);
44
+//        map.put(temp[2], temp[3]);
45
+//        map.put(temp[4], temp[5]);
46
+//        map.put(temp[6], temp[7]);
47
+//        map.forEach((s, s2) -> System.out.println(s + ": " + s2));
48
+//        System.out.println();
49
+//        Pattern pattern2 = Pattern.compile("[;@^*%]");
50
+//        Matcher matcher2 = pattern.matcher(output);
51
+//        whatever.forEach(s -> { String[] temp2 = pattern2.split(whatever.get(whatever.indexOf(s)));
52
+//        for (String string : temp2) {
53
+//            map.put(temp2[0], temp2[1]);
54
+////            System.out.println(itemParser.correctSpelling(string));
55
+////            System.out.println();
56
+//        } });
57
+//        System.out.println(itemParser.correctSpelling(map.entrySet().toString()));
58
+//        for (int i = 0; i < temp.length; i++) {
59
+//
60
+//        }
61
+//        for (String string : temp) {
62
+//            System.out.println(string);
63
+//        }
64
+//        while (matcher.find()) {
65
+//            System.out.println(output.charAt(matcher.start()+1));
66
+//        }
67
+//        HashMap<String, String> pairs = itemParser.mapKeyValuePairs(output);
68
+//        pairs.forEach((s, s2) -> System.out.println(s + " " + s2));
69
+//        whatever.forEach(System.out::println);
70
+//        System.out.println(output);
17 71
         // TODO: parse the data in output into items, and display to console.
18 72
     }
19 73
 }

+ 108
- 0
src/test/java/io/zipcoder/ItemParserTest.java Vedi File

@@ -59,4 +59,112 @@ public class ItemParserTest {
59 59
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 60
         assertEquals(expected, actual);
61 61
     }
62
+
63
+    @Test
64
+    public void correctBreadTest1() {
65
+        ItemParser.SpellingCorrector spellingCorrector = itemParser.new SpellingCorrector();
66
+        String text = "NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016";
67
+        String expected = "NAMe:Bread;price:1.23;type:Food;expiration:1/25/2016";
68
+        String actual = spellingCorrector.correctBread(text);
69
+        assertEquals(expected,actual);
70
+    }
71
+
72
+    @Test
73
+    public void correctBreadTest2() {
74
+        ItemParser.SpellingCorrector spellingCorrector = itemParser.new SpellingCorrector();
75
+        String text = "BREAD";
76
+        String expected = "Bread";
77
+        String actual = spellingCorrector.correctBread(text);
78
+        assertEquals(expected,actual);
79
+    }
80
+
81
+    @Test
82
+    public void correctBreadNameTest1() {
83
+        ItemParser.SpellingCorrector spellingCorrector = itemParser.new SpellingCorrector();
84
+        String text = "NAME";
85
+        String expected = "Name";
86
+        String actual = spellingCorrector.correctName(text);
87
+        assertEquals(expected,actual);
88
+    }
89
+
90
+    @Test
91
+    public void correctBreadNameTest2() {
92
+        ItemParser.SpellingCorrector spellingCorrector = itemParser.new SpellingCorrector();
93
+        String text = "NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016";
94
+        String expected = "Name:BrEAD;price:1.23;type:Food;expiration:1/25/2016";
95
+        String actual = spellingCorrector.correctName(text);
96
+        assertEquals(expected,actual);
97
+    }
98
+
99
+    @Test
100
+    public void correctAllSpellingTest() {
101
+        String text = "NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##naMe:appLEs;" +
102
+                "PRICE:2.50;name:COOKIES;PRicE:2.25;nAMe:MIlK";
103
+        String expected = "Name:Bread;Price:1.23;type:Food;expiration:1/25/2016##Name:Apples;" +
104
+                "Price:2.50;Name:Cookies;Price:2.25;Name:Milk";
105
+        String actual = itemParser.correctSpelling(text);
106
+        assertEquals(expected, actual);
107
+    }
108
+
109
+    @Test
110
+    public void countOccurencesOfMilkTest() {
111
+        ItemParser.OccurenceCounter occurenceCounter = itemParser.new OccurenceCounter();
112
+        String text = "NAMe:milk;price:1.23;type:Food;expiration:1/25/2016##naMe:appLEs;" +
113
+                "PRICE:2.50;name:COOKIES;PRicE:2.25;nAMe:MIlK";
114
+        int expected = 2;
115
+        int actual = occurenceCounter.countOccurencesOfMilk(text);
116
+        assertEquals(expected, actual);
117
+    }
118
+
119
+    @Test
120
+    public void countOccurencesOfBreadTest() {
121
+        ItemParser.OccurenceCounter occurenceCounter = itemParser.new OccurenceCounter();
122
+        String text = "NAMe:Bread;price:1.23;type:Food;expiration:1/25/2016##naMe:appLEs;" +
123
+                "PRICE:2.50;name:COOKIES;PRicE:2.25;nAMe:MIlK;name:BREAD;name:BrEAd";
124
+        int expected = 3;
125
+        int actual = occurenceCounter.countOccurencesOfBread(text);
126
+        assertEquals(expected, actual);
127
+    }
128
+
129
+    @Test
130
+    public void countOccurencesOfApplesTest() {
131
+        ItemParser.OccurenceCounter occurenceCounter = itemParser.new OccurenceCounter();
132
+        String text = "NAMe:Bread;price:1.23;type:Food;expiration:1/25/2016##naMe:appLEs;" +
133
+                "PRICE:2.50;name:COOKIES;PRicE:2.25;nAMe:MIlK;name:BREAD;name:BrEAd";
134
+        int expected = 1;
135
+        int actual = occurenceCounter.countOccurencesOfApples(text);
136
+        assertEquals(expected, actual);
137
+    }
138
+
139
+    @Test
140
+    public void countOccurencesOfCookiesTest() {
141
+        ItemParser.OccurenceCounter occurenceCounter = itemParser.new OccurenceCounter();
142
+        String text = "NAMe:Bread;price:1.23;type:Food;expiration:1/25/2016##naMe:appLEs;" +
143
+                "PRICE:2.50;name:COOKIES;PRicE:2.25;nAMe:MIlK;name:BREAD;name:cookies;name:c00kies";
144
+        int expected = 3;
145
+        int actual = occurenceCounter.countOccurencesOfCookies(text);
146
+        assertEquals(expected, actual);
147
+    }
148
+
149
+    @Test
150
+    public void countOccurencesOfAllItemsTest1() {
151
+        ItemParser.OccurenceCounter occurenceCounter = itemParser.new OccurenceCounter();
152
+        String text = "NAMe:Bread;price:1.23;type:Food;expiration:1/25/2016##naMe:appLEs;" +
153
+                "PRICE:2.50;name:COOKIES;PRicE:2.25;nAMe:MIlK;name:BREAD;name:cookies;name:c00kies";
154
+        int expected = 3;
155
+        int actual = occurenceCounter.countOccurencesOfAllItemsAlphabeticalOrder(text)[2];
156
+        assertEquals(expected, actual);
157
+    }
158
+
159
+    @Test
160
+    public void countOccurencesOfAllItemsTest2() {
161
+        ItemParser.OccurenceCounter occurenceCounter = itemParser.new OccurenceCounter();
162
+        String text = "NAMe:Bread;price:1.23;type:Food;expiration:1/25/2016##naMe:appLEs;" +
163
+                "PRICE:2.50;name:COOKIES;PRicE:2.25;nAMe:MIlK;name:BREAD;name:cookies;name:c00kies";
164
+        int expected = 2;
165
+        int actual = occurenceCounter.countOccurencesOfAllItemsAlphabeticalOrder(text)[1];
166
+        assertEquals(expected, actual);
167
+    }
168
+
169
+
62 170
 }