Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

import dsa.DiGraph;

import dsa.SeparateChainingHashST;

import dsa.Set;

import stdlib.In;

import stdlib.StdOut;

public class WordNet {

    //...

    private SeparateChainingHashST<String, Set<Integer>> st; // Maps a synset noun to set of

    // synset ID

    private SeparateChainingHashST<Integer, String> rst; // Maps a synset ID to the

    // corresponding synset string.

    private DiGraph G;

    private ShortestCommonAncestor sca;     // Computations of shortest common ancestor

    // Constructs a WordNet object given the names of the input (synset and hypernym) files.

    public WordNet(String synsets, String hypernyms) {

        //...

        if (synsets == null || hypernyms == null) {

            throw new IllegalArgumentException("Input files cannot be null");

        }

        st = new SeparateChaningHashST<>();

        rst = new SeparateChainingHashST<>();

        int vertices = 0;

        In in = new In(synsets);

        while (in.hasNextLine()) {

            vertices++;

            String[] line = in.readLine().split(",");

            String[] word = line[1].split(" ");

            int id = Integer.parseInt(line[0]);

            rst.put(id, line[1]);

            for (int i = 0; i < word.length; i++) {

                if (st.contains(word[i])) {

                    st.get(word[i]).add(id);

                } else {

                    st.put(word[i], new Set<Integer>());

                    st.get(word[i]).add(id);

                }

            }

        }

        DiGraph G = new DiGraph(vertices);

        in = new In(hypernyms);

        while (in.hasNextLine()) {

            String[] line = in.readLine().split(",");

            for (int i = 1; i < line.length; i++) {

                G.addEdge(Integer.parseInt(line[0]), Integer.parseInt(line[i]));

            }

        }

        sca = new ShortestCommonAncestor ();

        if (!sca.isRootedDAG()) {

            throw new IllegalArgumentException("hypernyms not rooted DAG");

        }

    }

    // Returns all WordNet nouns.

    public Iterable<String> nouns() {

        //...

        return st.keys();

    }

    // Returns true if the given word is a WordNet noun, and false otherwise.

    public boolean isNoun(String word) {

        //...

        if (word == null) {

            throw new NullPointerException();

            return st.contains(word);

        }

    }

    // Returns a synset that is a shortest common ancestor of noun1 and noun2.

    public String sca(String noun1, String noun2) {

        //...

        if (noun1 == null || noun2 == null) {

            throw new NullPointerException();

        }

        if (st.get(noun1) == null || st.get(noun2) == null) {

            throw new NullPointerException();

        }

        Iterable<Integer> integer1 = st.get(noun1);

        Iterable<Integer> integer2 = st.get(noun2);

        return rst.get(sca.ancestor(integer1, integer2));

    }

    // Returns the length of the shortest ancestral path between noun1 and noun2.

    public int distance(String noun1, String noun2) {

        //...

        if (noun1 == null || noun2 == null || st.get(noun2) == null) {

            throw new NullPointerException();

        }

        Iterable<Integer> integer1 = st.get(noun1);

        Iterable<Integer> integer2 = st.get(noun2);

        return sca.length(integer1, integer2);

    }

    // Unit tests the data type. [DO NOT EDIT]

    public static void main(String[] args) {

        WordNet wordnet = new WordNet(args[0], args[1]);

        String word1 = args[2];

        String word2 = args[3];

        int nouns = 0;

        for (String noun : wordnet.nouns()) {

            nouns++;

        }

        StdOut.printf("# of nouns = %d\n", nouns);

        StdOut.printf("isNoun(%s)? %s\n", word1, wordnet.isNoun(word1));

        StdOut.printf("isNoun(%s)? %s\n", word2, wordnet.isNoun(word2));

        StdOut.printf("isNoun(%s %s)? %s\n", word1, word2, wordnet.isNoun(word1 + " " + word2));

        StdOut.printf("sca(%s, %s) = %s\n", word1, word2, wordnet.sca(word1, word2));

        StdOut.printf("distance(%s, %s) = %s\n", word1, word2, wordnet.distance(word1, word2));

    }

}
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.8k views
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...