Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import edu.princeton.cs.algs4.In;
- import edu.princeton.cs.algs4.StdOut;
- public class Outcast {
- private final WordNet wn;
- // constructor takes a WordNet object
- public Outcast(WordNet wordnet) {
- this.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];
- for (int i = 0; i < nouns.length; i++) {
- for (int j = 0; j < n; j++) {
- distances[i] += wn.distance(nouns[i], nouns[j]);
- }
- }
- // max distance search
- int max = -1;
- int id = -1;
- for (int i = 0; i < n; i++) {
- if (max < distances[i]) {
- max = distances[i];
- id = i;
- }
- }
- return nouns[id];
- }
- 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));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement