-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutcast.java
53 lines (46 loc) · 1.58 KB
/
Outcast.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
/* *****************************************************************************
* Name: Devin Plumb
* NetID: dplumb
* Precept: P06
*
* Description: Finds the least similar word to the others, based on a
* WordNet.
*
**************************************************************************** */
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
public class Outcast {
private final WordNet wn; // stores the wordnet as an instance variable
// constructor takes a WordNet object
public Outcast(WordNet wordnet) {
wn = wordnet;
}
// given an array of WordNet nouns, return an outcast
public String outcast(String[] nouns) {
int n = nouns.length;
int[] distances = new int[n];
int maximum = 0;
int maximumIndex = 0;
for (int i = 0; i < n; i++) {
distances[i] = 0;
for (int j = 0; j < n; j++) {
distances[i] += wn.distance(nouns[i], nouns[j]);
}
if (distances[i] > maximum) {
maximum = distances[i];
maximumIndex = i;
}
}
return nouns[maximumIndex];
}
// test client (see below)
public static void main(String[] args) {
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));
}
}
}