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

📄 wordnetwrapper.java

📁 一个关于CHATBOT的程序(人式智能方面)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package ijp.assignment1.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import java.util.Random;

import net.didion.jwnl.JWNL;
import net.didion.jwnl.JWNLException;
import net.didion.jwnl.data.IndexWord;
import net.didion.jwnl.data.POS;
import net.didion.jwnl.data.PointerUtils;
import net.didion.jwnl.data.Synset;
import net.didion.jwnl.data.Word;
import net.didion.jwnl.data.list.PointerTargetNode;
import net.didion.jwnl.data.list.PointerTargetNodeList;

import net.didion.jwnl.dictionary.Dictionary;

/**
 * A wrapper which provides easy access to selected WordNet operations.
 * 
 * <p>Used to provide random words and word lookup for the AIPJ ChatBot
 * assignment. Lookup is provided through the JWNL dictionary operations.</p>
 *   
 * <p>Note: Generation of random words is accomplished through use of additional data
 * structures containing nouns, verbs and adjectives. These additional structures
 * speed up the process of retrieving a random word, as the JWNL library has
 * no suitable random access methods.</p>
 * 
 * <p>This code need not be changed for the assignement. It may be useful for
 * msc summer projects.</p> 
 * 
 * @author Judy Robertson
 */
public class WordNetWrapper {

    /**
     * The WordNet dictionary structure
     */
    private Dictionary d;
    
    /**
     * A list of the verbs in WordNet
     */
    private List verbList = new ArrayList();
    
    /**
     * A list of the nouns in WordNet
     */
    private List nounList = new ArrayList();
    
    /**
     * A list of the adjectives in WorNet
     */
    private List adjectiveList = new ArrayList();

    /**
     * A Pseudo-random generator used to find words at random
     */
    private Random randomGenerator;

    /**
     * Creates a new WordNetWrapper, initialises the WordNet
     * dictionary and reads in the pre-generated additional nouns, verbs and adjectives
     * lists from disc.
     *
     */
    public WordNetWrapper() {
        try {
            FileInputStream fi =
                new FileInputStream(System.getProperty("user.dir") + File.separator + "jwnl" +
                 File.separator + "file_properties.xml");
            JWNL.initialize(fi);
            d = Dictionary.getInstance();
            readInNouns();
            readInAdjectives();
            readInVerbs();
            randomGenerator = new Random();
        } catch (IOException e) {
            System.err.println("An IO error when loading wordnet");
            //e.printStackTrace();
        } catch (JWNLException err) {
            System.err.println("A JWNL exception occured during intialisation");
            err.printStackTrace();
        }

    }

    /**
     * Reads in the pre-generated list of adjectives from disc into a data 
     * structure which can be accessed in constant time to find random adjectives
     *
     */
    private void readInAdjectives() {
        File f =
            new File(System.getProperty("user.dir") + File.separator + "data" + 
            File.separator + "adjective" + ".dat");

        try {

            FileInputStream fin = new FileInputStream(f);

            ObjectInputStream istrm = new ObjectInputStream(fin);

            adjectiveList = (ArrayList) istrm.readObject();

        } catch (IOException e) {

            System.out.println("Trouble processing adjective file");

        } catch (ClassNotFoundException c) {

            System.err.println("Can't find class when trying to read serialised " 
            + "adjective words");

        }

    }

    /**
     * Reads in the pre-generated list of nouns from disc into a data structure
     * which can be accessed in constant time to find random nouns
     *
     */
    private void readInNouns() {
        File f = new File(System.getProperty("user.dir") + File.separator + 
        "data" + File.separator + "noun" + ".dat");

        try {

            FileInputStream fin = new FileInputStream(f);

            ObjectInputStream istrm = new ObjectInputStream(fin);

            nounList = (ArrayList) istrm.readObject();

        } catch (IOException e) {

            System.out.println("Trouble processing noun file");

        } catch (ClassNotFoundException c) {

            System.err.println("Can't find class when trying to read serialised " 
            + "noun words");

        }

    }

    /**
    * Reads in the pre-generated list of verbs from disc into a data structure
    * which can be accessed in constant time to find random verbs
    *
    */
    private void readInVerbs() {
        File f = new File(System.getProperty("user.dir") + File.separator + 
            "data" + File.separator + "verb" + ".dat");

        try {

            FileInputStream fin = new FileInputStream(f);

            ObjectInputStream istrm = new ObjectInputStream(fin);

            verbList = (ArrayList) istrm.readObject();

        } catch (IOException e) {

            System.out.println("Trouble processing verb file");

        } catch (ClassNotFoundException c) {

            System.err.println("Can't find class when trying to read serialised " 
            + "verb words");

        }

    }

    /**
     * Retrieve a verb at random from WordNet
     * 
     * @return A string containing a randomly generated verb
     */
    public String getVerbAtRandom() {
        String verb = "";

        int target = randomGenerator.nextInt(verbList.size());
        IndexWord word = (IndexWord) verbList.get(target);
        verb = word.getLemma();
        return verb;
    }

       /**
       * Retrieve an adjective at random from WordNet
       * 
       * @return A string containing a randomly generated adjective
       */
    public String getAdjectiveAtRandom() {
        String adj = "";

        int target = randomGenerator.nextInt(adjectiveList.size());

        IndexWord word = (IndexWord) adjectiveList.get(target);
        adj = word.getLemma();
        return adj;

    }

       /**
       * Retrieve a noun at random from WordNet
       * 
       * @return A string containing a randomly generated noun
       */
    public String getNounAtRandom() {
        String noun = "";

        int target = randomGenerator.nextInt(nounList.size());
        IndexWord word = (IndexWord) nounList.get(target);
        noun = word.getLemma();
        return noun;

    }
    
    /**
     * Find the synonyms of the specified word for the specified part of speech
     * 
     * @param pos The part of speech category for the word
     * 
     * @param lookupWord The word for which synonyms are required
     * 
     * @return A list containing strings representing synonyms of the specified 
     * word with the specified part of speech
     */
    public List getSynonyms(POS pos, String lookupWord) {

        Word[] words;
        Set synonymSet = new HashSet();
        try {
            IndexWord word = d.lookupIndexWord(pos, lookupWord);
            if (word != null) {
                //iterate over each sense of this word
                for (int j = 1; j < word.getSenseCount() + 1; j++) {

                    Synset syn = word.getSense(j);

                    //now add all the associated words from this synset to the 
                    //synonyms set
                    for (int k = 0; k < syn.getWords().length; k++) {
                        synonymSet.add(syn.getWords()[k].getLemma());

                    }
                    PointerTargetNodeList synonyms = PointerUtils.getSynonyms(syn);

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

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

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

                                }

                            }

                        }

                    }
                }

            }

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

    /**
     * Checks whether the specified word is a noun or not
     * 
     * @param s The word to be checked
     * 
     * @return True if the specified string is a noun in WordNet, false otherwise
     */
    public boolean isNoun(String s) {
        try {
            if (d.lookupIndexWord(POS.NOUN, s) != null){
                return true;
            }
        } catch (JWNLException e) {

            e.printStackTrace();
        }
        return false;
    }
    
    /**
    * Checks whether the specified word is a verb or not
    *
    *  @param s The word to be checked
    * 
    * @return True if the specified string is a verb in WordNet, false otherwise
    */
   
    public boolean isVerb(String s) {
        try {
            if (d.lookupIndexWord(POS.VERB, s) != null){
                return true;
            }
        } catch (JWNLException e) {

            e.printStackTrace();
        }
        return false;
    }
    
    /**
     * Checks whether the specified word is an adjective or not
     * 
     * @param s The word to be checked
     * 
     * @return True if the specified string is an adjective in WordNet, 
     * false otherwise
     */
    
    public boolean isAdjective(String s) {
        try {
            if (d.lookupIndexWord(POS.ADJECTIVE, s) != null){
                return true;
            }
        } catch (JWNLException e) {

            e.printStackTrace();
        }
        return false;
    }

    /**
     * Retrieve a set of words representing the hypernyms for every sense of the
     *  specified word and part of speech.
     * A hypernym is a superordinate word -- (a word that is more generic than 
     * a given word)
     * <p> Note: Adjectives don't have hypernyms in WordNet; this method with an
     *  adjective 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 
     * 

⌨️ 快捷键说明

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