diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-11-21 18:18:55 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2013-12-12 14:11:14 +0100 |
commit | a60ba769f451259caba14886fdb206593b92df76 (patch) | |
tree | 1c633722971e79bdbadecb4c1da3b677592c7d84 /Algorithms/Part-II/1-WordNet/Outcast.java | |
parent | 4020858e2a7ca3557d6ddfa40ceec0a191ef2605 (diff) | |
download | coursera-a60ba769f451259caba14886fdb206593b92df76.zip coursera-a60ba769f451259caba14886fdb206593b92df76.tar.gz |
Algorithms-II : 1-WordNet: implementation
Diffstat (limited to 'Algorithms/Part-II/1-WordNet/Outcast.java')
-rw-r--r-- | Algorithms/Part-II/1-WordNet/Outcast.java | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/Algorithms/Part-II/1-WordNet/Outcast.java b/Algorithms/Part-II/1-WordNet/Outcast.java index 296fd79..af2b6f3 100644 --- a/Algorithms/Part-II/1-WordNet/Outcast.java +++ b/Algorithms/Part-II/1-WordNet/Outcast.java @@ -2,26 +2,50 @@ public class Outcast { + private WordNet wordNet; + // constructor takes a WordNet object public Outcast(WordNet wordnet) { + wordNet = wordnet; } // given an array of WordNet nouns, return an outcast public String outcast(String[] nouns) { - return ""; + int maxTotal = -1; + String outcast = null; + + for (int i = 0; i < nouns.length; i++) { + int total = 0; + for (int j = 0; j < nouns.length; j++) { + total = total + wordNet.distance(nouns[i], nouns[j]); + } + + if (total > maxTotal) { + maxTotal = total; + outcast = nouns[i]; + } + } + + return outcast; } // for unit testing of this class (such as the one below) public static void main(String[] args) { - WordNet wordnet = new WordNet(args[0], args[1]); + WordNet wordnet = new WordNet("./data/synsets.txt", "./data/hypernyms.txt"); Outcast outcast = new Outcast(wordnet); - for (int t = 2; t < args.length; t++) { - String[] nouns = In.readStrings(args[t]); - StdOut.println(args[t] + ": " + outcast.outcast(nouns)); - } + String[] nouns; + + nouns = new In("./data/outcast5.txt").readAllStrings(); + StdOut.println(outcast.outcast(nouns)); + + nouns = new In("./data/outcast8.txt").readAllStrings(); + StdOut.println(outcast.outcast(nouns)); + + nouns = new In("./data/outcast11.txt").readAllStrings(); + StdOut.println(outcast.outcast(nouns)); } } |