-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyVocabulary.java
88 lines (70 loc) · 1.51 KB
/
MyVocabulary.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package langModel;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
/**
* Class MyVocabulary: class implementing the interface Vocabulary.
*
* @author ... (2015)
*
*/
public class MyVocabulary implements Vocabulary {
/**
* The set of words corresponding to the vocabulary.
*/
protected Set<String> vocabulary;
/**
* Constructor.
*/
public MyVocabulary(){
this.vocabulary = new TreeSet<String>();
}
@Override
public int getSize() {
return this.vocabulary.size();
}
@Override
public Set<String> getWords() {
return this.vocabulary;
}
@Override
public boolean contains(String word) {
return this.vocabulary.contains(word);
}
@Override
public void addWord(String word) {
if(!this.contains(word)){
this.vocabulary.add(word);
}
}
@Override
public void removeWord(String word) {
if(this.contains(word)){
this.vocabulary.remove(word);
}
}
@Override
public void scanNgramSet(Set<String> ngramSet) {
for(String word : ngramSet){
this.addWord(word);
}
}
@Override
public void readVocabularyFile(String filePath) {
List<String> lignes = MiscUtil.readTextFileAsStringList(filePath);
for(String ngram : lignes){
String[] words = ngram.trim().split(" ");
for(String word : words){
this.addWord(word);
}
}
}
@Override
public void writeVocabularyFile(String filePath) {
String fileContent = "";
for(String word : this.vocabulary){
fileContent+=word+"\n";
}
MiscUtil.writeFile(fileContent, filePath, false);
}
}