forked from gzc/MOOC-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutcast.java
More file actions
52 lines (39 loc) · 1.03 KB
/
Outcast.java
File metadata and controls
52 lines (39 loc) · 1.03 KB
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
//di = dist(Ai, A1) + dist(Ai, A2) + ... + dist(Ai, An)
public class Outcast {
private final WordNet wordnet;
public Outcast(WordNet wordnet) // constructor takes a WordNet object
{
this.wordnet = wordnet;
}
public String outcast(String[] nouns) // given an array of WordNet nouns, return an outcast
{
int max = 0;
String noun = null;
for(int i = 0;i < nouns.length;i++)
{
int distance = 0;
for(int j = 0;j < nouns.length;j++)
{
if(i == j)
continue;
distance += wordnet.distance(nouns[i], nouns[j]);
}
if(distance > max)
{
max = distance;
noun = nouns[i];
}
}
return noun;
}
public static void main(String[] args) // see test client below
{
WordNet wordnet = new WordNet(args[0], args[1]);
Outcast outcast = new Outcast(wordnet);
for (int t = 2; t < args.length; t++) {
In in = new In(args[t]);
String[] nouns = in.readAllStrings();
StdOut.println(args[t] + ": " + outcast.outcast(nouns));
}
}
}