-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFrequencyCounter.java
42 lines (40 loc) · 999 Bytes
/
FrequencyCounter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.*;
/**
* Counts the frequencies of k-length substrings of input text.
* @author Daniel Friedman
*
*/
public class FrequencyCounter {
public static void main(String[] args) {
String text;
int k = 0;
if (args.length == 1) {
k = Integer.parseInt(args[0]);
}
System.out.print("Enter string: ");
Scanner s = new Scanner(System.in);
text = s.nextLine();
MyHashMap<String, Integer> hash = new MyHashMap<String, Integer>();
int distinct = 0;
for (int i=0; i<=text.length()-k; i++) {
String sub = text.substring(i,i+k);
Markov m = new Markov(sub);
m.add();
if (hash.containsKey(sub)) {
Integer count = hash.get(sub);
hash.put(sub, count+1);
}
else {
hash.put(sub, m.count);
distinct++;
}
}
System.out.println(distinct+" distinct keys");
Iterator<String> keys = hash.keys();
while (keys.hasNext()) {
String key = keys.next();
Integer value = hash.get(key);
System.out.println(value+" "+key);
}
}
}