Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java-word-count

import java.util.HashMap;
2
import java.util.Locale;
3
import java.util.Map;
4
5
public class WordCount {
6
7
    public WordCount() {
8
9
    }
10
11
    public Map<String, Integer> phrase(String text) {
12
        Map<String, Integer> map = new HashMap<>();
13
        String[] textParts = text.toLowerCase()
14
                .trim()
15
                .replaceAll("[.-_:;?!@^&%$§=]", "")
16
                .replaceAll("'([a-zA-Z0-9]+)'", "$1")
17
                .replaceAll("^[,s]+","")
18
                .split("[,s]+");
19
20
        for (String textPart : textParts) {
21
22
            if (map.containsKey(textPart)) {
23
                int counter = map.get(textPart) + 1;
24
                map.put(textPart, counter);
25
            } else {
26
                map.put(textPart, 1);
27
            }
28
29
        }
30
31
        return map;
32
    }
33
34
}
35
Source by exercism.org #
 
PREVIOUS NEXT
Tagged:
ADD COMMENT
Topic
Name
4+8 =