Beispiel #1
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;
  }
Beispiel #2
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;
  }
Beispiel #3
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);
  }
Beispiel #5
0
  public Pair<List<String>, List<String>> cookLexicalItem(
      String text, ItemType textType, boolean discardStopwords) {
    try {

      List<String> cookedSentence = new ArrayList<String>();
      Pair<List<String>, List<String>> out = new Pair<List<String>, List<String>>(null, null);

      for (String item : Arrays.asList(text.split(" "))) {
        if (item.trim().length() == 0) continue;

        switch (textType) {
          case SENSE_OFFSETS:
            cookedSentence.add(item);
            break;

          case SENSE_KEYS:
            IWord sense = WordNetUtils.getInstance().getSenseFromSenseKey(item);
            cookedSentence.add(
                GeneralUtils.fixOffset(sense.getSynset().getOffset(), sense.getPOS()));
            break;

          case WORD_SENSE:
            IWord snse = WordNetUtils.getInstance().mapWordSenseToIWord(item);
            cookedSentence.add(GeneralUtils.fixOffset(snse.getSynset().getOffset(), snse.getPOS()));
            break;

          case SURFACE_TAGGED:
            cookedSentence.add(item);
            break;
        }
      }

      if (textType.equals(ItemType.SURFACE)) {
        out = TextualSimilarity.getInstance().cookSentence(text);
        cookedSentence = out.first;
      }

      if (cookedSentence == null) cookedSentence = new ArrayList<String>();

      List<String> newCS = new ArrayList<String>();

      for (String s : cookedSentence) {
        // if it is a synset
        if (s.matches("[0-9]*\\-[anvr]")) {
          newCS.add(s);
          continue;
        }

        String comps[] = s.split("#");
        String word = comps[0];
        String ps = comps[1];

        // otherwise check word exists in WordNet
        if (!TextualSimilarity.getInstance().isOOV(word, ps)) newCS.add(word + "#" + ps);
      }

      cookedSentence = newCS;

      return new Pair<List<String>, List<String>>(cookedSentence, out.second);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }