예제 #1
0
 public IWord getIWordForPhrase(String phrase) {
   IWord word = null;
   IIndexWord idxWord = dict.getIndexWord(phrase, POS.NOUN);
   if (idxWord != null) {
     IWordID wordID = (IWordID) idxWord.getWordIDs().get(0);
     word = dict.getWord(wordID);
   }
   return word;
 }
예제 #2
0
  public List getSynonyms(String phrase) {
    List synonymsList = new ArrayList();
    if (dict == null) {
      System.out.println("Dictionary is null");
      System.exit(0);
    }
    IIndexWord idxWord = dict.getIndexWord(phrase, POS.NOUN);
    if (idxWord != null) {
      IWordID wordID = (IWordID) idxWord.getWordIDs().get(0);
      IWord word = dict.getWord(wordID);
      ISynset synset = word.getSynset();
      List words = new ArrayList();
      IWord w;
      for (Iterator iterator = synset.getWords().iterator(); iterator.hasNext(); words.add(w))
        w = (IWord) iterator.next();

      synonymsList = keepUniqueTerms(words);
    }
    return synonymsList;
  }
예제 #3
0
  private Set<String> getHypernyms(
      String word, POS posTag, boolean firstSenseOnly, int maxIterations) {

    Set<String> hypernyms = new HashSet<String>();

    IIndexWord iIndexWordRoot = dict.getIndexWord(word, posTag);
    if (iIndexWordRoot == null) {
      return hypernyms; // no senses found
    }

    List<IWordID> todo = iIndexWordRoot.getWordIDs();

    int iterations = 0;

    while (iterations++ < maxIterations && !todo.isEmpty()) {
      List<IWordID> tmp = Lists.newArrayList();
      // iterate over senses
      for (IWordID iWordId : todo) {
        IWord iWord1 = dict.getWord(iWordId);
        ISynset iSynset = iWord1.getSynset();

        // multiple hypernym chains are possible for a synset
        for (ISynsetID iSynsetId : iSynset.getRelatedSynsets(Pointer.HYPERNYM)) {
          List<IWord> iWords = dict.getSynset(iSynsetId).getWords();
          for (IWord iWord2 : iWords) {
            String lemma = iWord2.getLemma().replace('_', ' ');
            hypernyms.add(lemma);
            tmp.add(iWord2.getID());
          }
        }

        if (firstSenseOnly) {
          break;
        }
      }
      todo = tmp;
    }

    return hypernyms;
  }
예제 #4
0
  public double getSynsetDepth(String word, int senseno, String pos) {
    IIndexWord word1 = null;
    // get the WordNet words in *any* POS
    ArrayList<Integer> homehierarchies = null;
    // if(pos.equalsIgnoreCase("n"))
    // {
    try {
      word1 = dict.getIndexWord(word, POS.NOUN);
      homehierarchies = nounroots;
      System.out.println(word1.toString() + "||||");

      // }
      // if(pos.equalsIgnoreCase("v"))
      // {
      //	word1 = dict.getIndexWord(word, POS.VERB);
      //	homehierarchies	=	verbroots;
      // }
      // ...........................................................................................................................................
      IWordID word1ID = word1.getWordIDs().get(senseno - 1); // get the right sense of word 1
      ISynset synset1 = dict.getWord(word1ID).getSynset();
      // ...........................................................................................................................................
      // get a score
      TreeMap<Integer, HashSet<ISynsetID>> depths = new TreeMap<Integer, HashSet<ISynsetID>>();
      HashSet<ISynsetID> synsets = new HashSet<ISynsetID>();
      synsets.add(synset1.getID());
      treecreeper(0, synsets, depths, homehierarchies);
      if (depths.isEmpty()) {
        return (0.0); // i.e. is <root>, nothing 'above' it
      }
      return ((double) (depths.lastKey() + 2.0));

    } catch (Exception ex) {
    }

    return 0.0;
    // ??? node counting, so have to include start and end node
  }
예제 #5
0
  public List getHypernym(String phrase) {
    List hypernymsList = new ArrayList();
    if (dict == null) {
      System.out.println("Dictionary is null");
      System.exit(0);
    }
    IIndexWord idxWord = dict.getIndexWord(phrase, POS.NOUN);
    if (idxWord != null) {
      IWordID wordID = (IWordID) idxWord.getWordIDs().get(0);
      IWord word = dict.getWord(wordID);
      ISynset synset = word.getSynset();
      List hypernyms = synset.getRelatedSynsets(Pointer.HYPERNYM);
      List words;
      for (Iterator iterator = hypernyms.iterator();
          iterator.hasNext();
          hypernymsList.addAll(words)) {
        ISynsetID sid = (ISynsetID) iterator.next();
        words = dict.getSynset(sid).getWords();
      }

      hypernymsList = keepUniqueTerms(hypernymsList);
    }
    return hypernymsList;
  }
  public static void main(String[] args) throws IOException {

    String path = args[0];
    String sentence =
        "The bank can guarantee deposits will eventually cover future tuition costs because it invests in adjustable-rate mortgage securities";

    URL url = null;
    try {
      url = new URL("file", null, path);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }

    if (url == null) return;

    // construct the dictionary object and open it
    IDictionary dict = new Dictionary(url);
    dict.open();

    for (POS posVal : POS.values()) {
      IIndexWord idxWord = dict.getIndexWord("bank", posVal);

      if (dict.getIndexWord("bank", posVal) != null) {
        List<IWordID> wordSenses = idxWord.getWordIDs();

        for (IWordID sense : wordSenses) {
          IWord word = dict.getWord(sense);

          String gloss = word.getSynset().getGloss();
          calculateOverlap(gloss, sentence, posVal);
        }
      }
    }

    // printing the overlap map
    for (Entry<String, Integer> entry : overlapMap.entrySet()) {
      String key = entry.getKey().toString();
      int value = entry.getValue();

      System.out.println("------");

      System.out.println("SENSE: " + key + "\t\tOVERLAP: " + value);

      List<String> displayOverlap = overlapWords.get(key);

      for (String str : displayOverlap) System.out.println(str);
    }

    // calculating the most likely sense
    int maxVal = -1;
    String sense = "";

    for (Entry<String, Integer> entry : overlapMap.entrySet()) {

      if (entry.getValue() > maxVal) {
        maxVal = entry.getValue();
        sense = entry.getKey();
      }
    }

    System.out.println("------------------------");
    System.out.println("MOST LIKELY SENSE IS: " + sense);
    System.out.println("VALUE OF ITS OVERLAP IS: " + maxVal);
  }