Exemplo n.º 1
0
  /**
   * Receives a rule and return the type of the rule, such as "synonym" or "hypernym" for WordNet,
   * "redirect" for Wikipedia, etc. The default value is "local-entailment".<br>
   * A better solution is to add an abstract class implementing RuleInfo, that all the concrete
   * RuleInfos will extend. This class will contain a field "relation" with a default of
   * "local-entailment". Then we can call: rule.getInfo().getRelation() without having to know which
   * resource the rule belongs to.
   *
   * @param rule
   * @return The type of the rule
   */
  private String getLinkInfo(LexicalRule<? extends RuleInfo> rule) {

    String type = "local-entailment";

    // WordNet
    if (rule.getResourceName().equals("WORDNET")) {
      type = ((WordnetRuleInfo) rule.getInfo()).getTypedRelation().name();
    }

    // VerbOcean
    else if (rule.getResourceName().equals("VerbOcean")) {
      type = ((VerbOceanRuleInfo) rule.getInfo()).getRelationType().name();
    }

    return type;
  }
  /**
   * Fill up the one sided rule maps
   *
   * @param mapRulesByUnorderedPair
   * @param verbPairs
   * @throws LexicalResourceException
   */
  private void fillTheRuleMaps(
      PairMap<String, LexicalRule<? extends VerbOceanRuleInfo>> mapRulesByUnorderedPair,
      Set<Pair<String>> verbPairs)
      throws LexicalResourceException {
    for (Pair<String> verbPair : verbPairs) {
      LexicalRule<? extends VerbOceanRuleInfo> rule = mapRulesByUnorderedPair.getValueOf(verbPair);
      addToMappedList(mapRulesByEntailingVerb, rule.getLLemma(), rule);
      addToMappedList(mapRulesByEntailedVerb, rule.getRLemma(), rule);

      mapRulesByEntailmentPair.put(new EntailmentPair(rule.getLLemma(), rule.getRLemma()), rule);

      if (rule.getInfo()
          .getRelationType()
          .isBidirectional()) // bidirectional rules are symmetrically duplicated
      {
        LexicalRule<VerbOceanRuleInfo> inverseRule = invertRule(rule);
        addToMappedList(mapRulesByEntailedVerb, inverseRule.getRLemma(), inverseRule);
        mapRulesByEntailmentPair.put(
            new EntailmentPair(inverseRule.getLLemma(), inverseRule.getRLemma()), inverseRule);
      }
    }

    // sort each little list according to score
    sortMappedLists(mapRulesByEntailingVerb);
    sortMappedLists(mapRulesByEntailedVerb);
  }
 /**
  * @param rule
  * @return
  * @throws LexicalResourceException
  */
 private LexicalRule<VerbOceanRuleInfo> invertRule(LexicalRule<? extends VerbOceanRuleInfo> rule)
     throws LexicalResourceException {
   return new LexicalRule<VerbOceanRuleInfo>(
       rule.getRLemma(),
       VERB,
       rule.getLLemma(),
       VERB,
       rule.getConfidence(),
       rule.getRelation(),
       RESOURCE_NAME,
       rule.getInfo());
 }
Exemplo n.º 4
0
  /**
   * Get rules of type leftSide -> rightSide, using the given lexical resource
   *
   * @param resource The lexical resource to use
   * @param leftSide The phrase that will be looked for as lhs of a rule
   * @param rightSide The phrase that will be looked for as rhs of a rule
   * @param partOfSpeech2
   * @param partOfSpeech
   * @return The list of rules leftSide -> rightSide
   * @throws LexicalResourceException
   */
  private List<LexicalRule<? extends RuleInfo>> getRules(
      LexicalResource<? extends RuleInfo> resource,
      String leftSide,
      String rightSide,
      PartOfSpeech leftSidePOS,
      PartOfSpeech rightSidePOS)
      throws LexicalResourceException {

    List<LexicalRule<? extends RuleInfo>> rules = new ArrayList<LexicalRule<? extends RuleInfo>>();

    try {

      // WordNet workaround:
      // Make sure the synsets of the right and left sides of the rule
      // are equal to the right and left phrases.
      // (WN returns rules associated with any of the words in the phrase)
      if (resource.getClass().getName().toLowerCase().contains(WORDNET)) {

        for (LexicalRule<? extends RuleInfo> rule :
            resource.getRules(leftSide, leftSidePOS, rightSide, rightSidePOS)) {

          WordnetRuleInfo ruleInfo = (WordnetRuleInfo) rule.getInfo();

          if ((ruleInfo.getLeftSense().getWords().contains(leftSide))
              && (ruleInfo.getRightSense().getWords().contains(rightSide))) {

            addRuleToList(rules, rule);
          }
        }

      } else {

        // Get rules from t to h
        for (LexicalRule<? extends RuleInfo> rule :
            resource.getRules(leftSide, leftSidePOS, rightSide, rightSidePOS)) {

          addRuleToList(rules, rule);
        }
      }

    } catch (Exception e) {
      logger.warn(
          "Could not add rules from "
              + resource.getClass().getSimpleName()
              + " for "
              + leftSide
              + "->"
              + rightSide,
          e);
    }

    return rules;
  }