Lauren Green 8 år sedan
förälder
incheckning
517cb46cbb
3 ändrade filer med 20652 tillägg och 23 borttagningar
  1. 59
    20
      src/main/java/io/zipcoder/WC.java
  2. 20568
    0
      src/main/resources/LittleWomen.txt
  3. 25
    3
      src/test/java/io/zipcoder/WCTest.java

+ 59
- 20
src/main/java/io/zipcoder/WC.java Visa fil

@@ -20,33 +20,34 @@ public class WC {
20 20
         }
21 21
     }
22 22
 
23
-    public WC(Iterator<String> si) {
24
-        this.si = si;
25
-
26
-    }
27
-
28
-    public Iterator<String> getSi() {
29
-
30
-        return si;
31
-    }
32
-
33
-
34
-    private HashMap<String, Integer> readFile() {
23
+//    public WC(Iterator<String> si) {
24
+//        this.si = si;
25
+//
26
+//    }
27
+//
28
+//    public Iterator<String> getSi() {
29
+//
30
+//        return si;
31
+//    }
32
+
33
+
34
+    public HashMap<String, Integer> readFile() {
35 35
         HashMap<String, Integer> wordCount = new HashMap<String, Integer>();
36 36
 
37
-        while(getSi().hasNext()) {
38
-            if(wordCount.containsKey(si.next())) {
39
-                int value = wordCount.get(si.next());
37
+        while(si.hasNext()) {
38
+            String key = si.next().toLowerCase();
39
+            if(wordCount.containsKey(key)) {
40
+                int value = wordCount.get(key);
40 41
                 value++;
41
-                wordCount.put(si.next(), value);
42
+                wordCount.put(key, value);
42 43
             } else {
43
-                wordCount.put(si.next(), 1);
44
+                wordCount.put(key, 1);
44 45
             }
45 46
         }
46 47
         return wordCount;
47 48
     }
48 49
 
49
-    public void printWordCount(HashMap<String, Integer> myMap) {
50
+    public static void printWordCount(HashMap<String, Integer> myMap) {
50 51
         for (String word: myMap.keySet()){
51 52
 
52 53
             String key = word;
@@ -57,11 +58,49 @@ public class WC {
57 58
         }
58 59
     }
59 60
 
61
+    public static LinkedHashMap<String, Integer> sortList(HashMap<String, Integer> myMap) {
62
+        List<String> mapKeys = new ArrayList<String>(myMap.keySet());
63
+        List<Integer> mapValues = new ArrayList<Integer>(myMap.values());
64
+        Collections.sort(mapValues);
65
+        Collections.sort(mapKeys);
66
+
67
+        LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
68
+        Iterator<Integer> valueIt = mapValues.iterator();
69
+        while (valueIt.hasNext()) {
70
+            Integer val = valueIt.next();
71
+            Iterator<String> keyIt = mapKeys.iterator();
72
+
73
+            while (keyIt.hasNext()) {
74
+                String key = keyIt.next();
75
+                Integer comp1 = myMap.get(key);
76
+                Integer comp2 = val;
77
+
78
+                if (comp1.equals(comp2)) {
79
+                    keyIt.remove();
80
+                    sortedMap.put(key, val);
81
+                    break;
82
+                }
83
+            }
84
+        }
85
+        return sortedMap;
86
+
87
+
88
+//        Map<String, Integer> sorted = myMap
89
+//                .entrySet()
90
+//                .stream()
91
+//                .sorted(comparingByValue())
92
+//                .collect(
93
+//                        toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2,
94
+//                                LinkedHashMap::new));
95
+
96
+    }
97
+
60 98
     public static void main(String[] args) {
61
-        String fileName = "./../../someTextFile.txt";
99
+        String fileName = "./../../LittleWomen.txt";
62 100
         String fullPath = WC.class.getResource(fileName).getFile();
63 101
         WC wc = new WC(fullPath);
64
-        wc.printWordCount(wc.readFile());
102
+        HashMap<String, Integer> result = wc.readFile();
103
+        printWordCount(sortList(result));
65 104
 
66 105
 
67 106
 

+ 20568
- 0
src/main/resources/LittleWomen.txt
Filskillnaden har hållits tillbaka eftersom den är för stor
Visa fil


+ 25
- 3
src/test/java/io/zipcoder/WCTest.java Visa fil

@@ -5,20 +5,42 @@ import org.junit.Test;
5 5
 
6 6
 import java.util.ArrayList;
7 7
 import java.util.Arrays;
8
+import java.util.HashMap;
9
+import java.util.LinkedHashMap;
8 10
 
9 11
 public class WCTest {
10 12
 
11 13
     @Test
12 14
     public void testCountTheWords() {
13 15
         //Given
14
-        WC test = new WC(WC.class.getResource("/someTextFile.txt").getFile());
15
-        System.out.println(test.getSi().next());
16
-
16
+        String fileName = "./../../someTextFile.txt";
17
+        String fullPath = WC.class.getResource(fileName).getFile();
18
+        WC wc = new WC(fullPath);
19
+        Integer expected = 3;
17 20
 
18 21
         //When
22
+        HashMap<String, Integer> result = wc.readFile();
23
+        Integer actual = result.get("cow");
19 24
 
20 25
         //Then
26
+        Assert.assertEquals(expected, actual);
27
+    }
28
+
29
+    @Test
30
+    public void testSortedList() {
31
+        //Given
32
+        String fileName = "./../../someTextFile.txt";
33
+        String fullPath = WC.class.getResource(fileName).getFile();
34
+        WC wc = new WC(fullPath);
35
+        Integer expected = 3;
36
+
37
+        //When
38
+        HashMap<String, Integer> result = wc.readFile();
39
+        LinkedHashMap<String, Integer> sort = WC.sortList(result);
40
+        Integer actual = result.get("cow");
21 41
 
42
+        //Then
43
+        Assert.assertEquals(expected, actual);
22 44
     }
23 45
 
24 46
 }