-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.java
76 lines (54 loc) · 1.53 KB
/
Dictionary.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
package laboration5;
import java.io.*;
import java.util.*;
public class Dictionary {
Map<Word,Set<Word>> map = new HashMap<Word,Set<Word>>();
public void add(Word key, Word value) {
if(map.containsKey(key)) {
Set<Word> nyttM = map.get(key);
nyttM.add(key);
}
else {
Set<Word> newSet = new HashSet<>();
newSet.add(value);
map.put(key, newSet);
}
}
public void add(String key, String value) {
add(new Word(key), new Word(value));
}
// Returnerar en icke-null mängd med ordlistans alla termer.
public Set<Word> terms() {
return map.keySet();
}
// Slår upp och returnerar en mängd av betydelser till t
public Set<Word> lookup(Word key) {
return map.get(key);
}
public Dictionary inverse() {
Dictionary inverseDictionary = new Dictionary();
for(Word key : map.keySet()) {
for (Word value : lookup(key)) {
inverseDictionary.add(value, key);
}
}
return inverseDictionary;
}
public void load(InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while (reader.ready()) {
String line = reader.readLine();
String[] words = line.split(",");
add(words[0],words[1]);
}
}
public void save(OutputStream os) throws IOException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(os);
for(Word key : map.keySet()) {
for(Word value : map.get(key)) {
outputStreamWriter.append(key + "," + value + "\n");
}
}
outputStreamWriter.close();
}
}