⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordnetwrapper.java

📁 一个关于CHATBOT的程序(人式智能方面)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @return A list containing all the direct hypernyms of all senses of the 
     * specified word, assuming the given POS.
     */
    
    public List getHypernyms(POS pos, String e) {
        Set hypernymSet = new HashSet();
        if (pos == POS.ADJECTIVE)
            System.err.println("Sorry, adjectives don't have hypernyms, " +
                "will return empty list");
        else {
            Word[] words;

            try {
                IndexWord word = d.lookupIndexWord(pos, e);
                if (word != null) {
                    //iterate over each sense of this word
                    for (int j = 1; j < word.getSenseCount() + 1; j++) {

                        Synset syn = word.getSense(j);

                        PointerTargetNodeList hypernyms = PointerUtils.getDirectHypernyms(syn);

                        if (hypernyms != null) {
                            //if this sense of the word has hypernyms, 
                            //iterate over them
                            Iterator i = hypernyms.iterator();
                            while (i.hasNext()) {

                                PointerTargetNode targetNode = (PointerTargetNode) i.next();
                                //if this hypernym is just a string (lexical) 
                                //add it to the hypernym list
                                if (targetNode.isLexical()) {
                                    hypernymSet.add(targetNode.getWord().getLemma());

                                } else {
                                    // It's a synset rather than a word, 
                                    //so retrieve all the words and add them 
                                    //to the hypernym list
                                    words = targetNode.getSynset().getWords();
                                    for (int t = 0; t < words.length; t++) {
                                        hypernymSet.add(words[t].getLemma());

                                    }

                                }

                            }

                        }
                    }

                }

            } catch (JWNLException err) {
                err.printStackTrace();
            }
        }
        return new ArrayList(hypernymSet);
    }

    /**
     * Retrieve a list of words representing the meronyms for every sense of the
     * specified word and POS.
     * Meronym - The name of a constituent part of, the substance of, 
     * or a member of something. X is a meronym of Y if X is a part of Y . 
     * <p> Note: Adjectives and verbs don't have hypernyms in WordNet; 
     * this method with an adjective or verb part of speech 
     * will return an empty list </p>
     * 
     * @param pos The part of speech for the word
     * 
     * @param e The word to be looked up 
     * 
     * @return A list containing all the direct meronyms of all senses of the 
     * specified word, assuming the given POS.
     */
    
    public List getMeronyms(POS pos, String e) {
        Set meronymSet = new HashSet();
        if (pos == POS.ADJECTIVE || pos == POS.VERB)
            System.err.println("Sorry, adjectives and verbs don't have meronyms," +
                " will return empty set");
        else {
            Word[] words;

            try {
                IndexWord word = d.lookupIndexWord(pos, e);
                if (word != null) {
                    //iterate over each sense of this word
                    for (int j = 1; j < word.getSenseCount() + 1; j++) {

                        Synset syn = word.getSense(j);

                        PointerTargetNodeList meronyms = PointerUtils.getMeronyms(syn);

                        if (meronyms != null) {
                            //if this sense of the word has meronyms, iterate over them
                            Iterator i = meronyms.iterator();
                            while (i.hasNext()) {

                                PointerTargetNode targetNode = (PointerTargetNode) i.next();
                                //if this meronym is just a string (lexical) 
                                //add it to the meronym list
                                if (targetNode.isLexical()) {
                                    meronymSet.add(targetNode.getWord().getLemma());

                                } else {
                                    // It's a synset rather than a word, 
                                    //so retrieve all the words and 
                                    //add them to the meronym list
                                    words = targetNode.getSynset().getWords();
                                    for (int t = 0; t < words.length; t++) {
                                        meronymSet.add(words[t].getLemma());

                                    }

                                }

                            }

                        }
                    }

                }

            } catch (JWNLException err) {
                err.printStackTrace();
            }
        }
        return new ArrayList(meronymSet);
    }

    /**
     * Retrieve a list of words representing the hyponyms for every sense of the
     *  specified word and POS.
     * Hyponym is the specific term used to designate a member of a class. 
     * X is a hyponym of Y if X is a (kind of) Y . 
     *
     * @param pos The part of speech for the word
     * 
     * @param e The word to be looked up 
     * 
     * @return A list containing all the direct hyponyms of all senses of the 
     * specified word, assuming the given POS.
     */
    
    public List getHyponyms(POS pos, String e) {
        Set hyponymSet = new HashSet();
        if (pos == POS.ADJECTIVE)
            System.err.println("Sorry, adjectives don't have hyponyms, " +
                "will return empty set");
        else {
            Word[] words;

            try {
                IndexWord word = d.lookupIndexWord(pos, e);
                if (word != null) {
                    //iterate over each sense of this word
                    for (int j = 1; j < word.getSenseCount() + 1; j++) {

                        Synset syn = word.getSense(j);

                        PointerTargetNodeList hyponyms = PointerUtils.getDirectHyponyms(syn);

                        if (hyponyms != null) {
                            //if this sense of the word has hyponyms, iterate over them
                            Iterator i = hyponyms.iterator();
                            while (i.hasNext()) {

                                PointerTargetNode targetNode = (PointerTargetNode) i.next();
                                //if this hyponym is just a string (lexical) 
                                //add it to the hyponym list
                                if (targetNode.isLexical()) {
                                    hyponymSet.add(targetNode.getWord().getLemma());

                                } else {
                                    // It's a synset rather than a word, 
                                    //so retrieve all the words and add them to the hyponym list
                                    words = targetNode.getSynset().getWords();
                                    for (int t = 0; t < words.length; t++) {
                                        hyponymSet.add(words[t].getLemma());

                                    }

                                }

                            }

                        }
                    }

                }

            } catch (JWNLException err) {
                err.printStackTrace();
            }
        }
        return new ArrayList(hyponymSet);
    }

    /**
     * Look up all synonyms of this word, for all parts of speech
     * 
     * @param word The word for which synonyms are required
     * 
     * @return A list containing strings representing synonyms of the specified word
     */
    
    public List getAllSynonyms(String word) {

        Set answers = new HashSet();
        answers.addAll(getSynonyms(POS.VERB, word));
        answers.addAll(getSynonyms(POS.ADJECTIVE, word));
        answers.addAll(getSynonyms(POS.ADVERB, word));
        answers.addAll(getSynonyms(POS.NOUN, word));

        return new ArrayList(answers);

    }
    
    /**
     * Used to test the generation of random words from WordNet
     *
     */
    
    public void testRandomWords() {
        for (int i = 0; i < 100; i++) {

            System.out.println("Here is the verb " + getVerbAtRandom());
            System.out.println("Here is the adj " + getAdjectiveAtRandom());
            System.out.println("Here is the noun " + getNounAtRandom());
        }
    }
    
    /**
     * Used to test the code for generating synonyms for each part of speech
     *
     */
    
    public void testSynonyms() {
        for (int i = 0; i < 100; i++) {

            String noun = getNounAtRandom();
            String verb = getVerbAtRandom();
            String adj = getAdjectiveAtRandom();
            List nounsyns = getSynonyms(POS.NOUN, noun);
            Iterator it = nounsyns.iterator();
            System.out.println("\n Here are the synonyms for the randomly " +
                "generated noun " + noun);
            while (it.hasNext()) {
                System.out.print((String) it.next() + ", ");
            }
            List verbsyns = getSynonyms(POS.VERB, verb);
            Iterator vit = verbsyns.iterator();
            System.out.println("\n Here are the synonyms for the randomly " +
                "generated verb " + verb);
            while (vit.hasNext()) {
                System.out.print((String) vit.next() + ", ");
            }

            List adjsyns = getSynonyms(POS.ADJECTIVE, adj);
            Iterator ait = adjsyns.iterator();
            System.out.println("\n Here are the synonyms for the randomly " +
                "generated adjective " + adj);
            while (ait.hasNext()) {
                System.out.print((String) ait.next() + ", ");
            }
        }

    }

    /**
     * Used to test the code for generating synonyms for nouns and verbs
     *
     */
    
    public void testHyponyms() {
        for (int i = 0; i < 100; i++) {

            String noun = getNounAtRandom();
            String verb = getVerbAtRandom();

            List nounsyns = getHyponyms(POS.NOUN, noun);
            Iterator it = nounsyns.iterator();
            System.out.println("\n Here are the hyponyms for the randomly " +
                "generated noun " + noun);
            while (it.hasNext()) {
                System.out.print((String) it.next() + ", ");
            }
            List verbsyns = getHyponyms(POS.VERB, verb);
            Iterator vit = verbsyns.iterator();
            System.out.println("\n Here are the hyponyms for the randomly " +
                "generated verb " + verb);
            while (vit.hasNext()) {
                System.out.print((String) vit.next() + ", ");
            }

        }

    }
    
    /**
     * Used to test the code for generating hypernyms for nouns and verbs
     *
     */

    public void testHypernyms() {
        for (int i = 0; i < 100; i++) {

            String noun = getNounAtRandom();
            String verb = getVerbAtRandom();

            List nounsyns = getHypernyms(POS.NOUN, noun);
            Iterator it = nounsyns.iterator();
            System.out.println("\n Here are the hypernyms for the randomly " +
                "generated noun " + noun);
            while (it.hasNext()) {
                System.out.print((String) it.next() + ", ");
            }
            List verbsyns = getHypernyms(POS.VERB, verb);
            Iterator vit = verbsyns.iterator();
            System.out.println("\n Here are the hypernyms for the randomly " +
                "generated verb " + verb);
            while (vit.hasNext()) {
                System.out.print((String) vit.next() + ", ");
            }

        }

    }

    /**
     * Used to test the code for generating meronyms for nouns 
     *
     */
    public void testMeronyms() {
        for (int i = 0; i < 100; i++) {

            String noun = getNounAtRandom();
            String verb = getVerbAtRandom();

           List nounsyns = getMeronyms(POS.NOUN, noun);
            Iterator it = nounsyns.iterator();
            System.out.println("\n Here are the meronyms for the randomly " +
                "generated noun " + noun);
            while (it.hasNext()) {
                System.out.print((String) it.next() + ", ");
            }

        }

    }

    /**
     * Creates a new interface to WordNet and tests methods
     * @param args None necessary
     */
    public static void main(String args[]) {

        WordNetWrapper w = new WordNetWrapper();
        //w.testRandomWords();
        w.testSynonyms();
        //w.testHypernyms();
        //w.testHyponyms();
        //w.testMeronyms();

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -