-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordDict.java
More file actions
33 lines (28 loc) · 994 Bytes
/
WordDict.java
File metadata and controls
33 lines (28 loc) · 994 Bytes
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
package src.rake;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class WordDict implements Iterable<String>{
HashMap<String, Integer> frequencyDict = new HashMap<>();
public int size(){
return frequencyDict.size();
}
public Iterator<String> iterator(){
return frequencyDict.keySet().iterator();
}
public void add(String word){
if (frequencyDict.putIfAbsent(word, 1) != null)
frequencyDict.replace(word, frequencyDict.get(word) + 1);
}
public void add(String word, Integer val){
if (frequencyDict.putIfAbsent(word, val) != null)
frequencyDict.replace(word, frequencyDict.get(word) + val);
}
public void addDict(WordDict wordDict){
for (String word: wordDict)
add(word, wordDict.getFrequency(word));
}
public Integer getFrequency(String word){
return frequencyDict.get(word);
}
}