/** * 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); }
/** * calculate the similarity score between T and H based on VerbOcean relations * * @param tBag the bag of words of T * @param hBag the bag of words of H * @param volr the VerbOcean relations * @return the similarity score * @throws ScoringComponentException */ protected double calculateSingleLexScoreWithVORelations( HashMap<String, Integer> tBag, HashMap<String, Integer> hBag, VerbOceanLexicalResource volr) throws ScoringComponentException { double score = 0.0d; HashMap<String, Integer> tWordBag = new HashMap<String, Integer>(); for (final Iterator<Entry<String, Integer>> iter = tBag.entrySet().iterator(); iter.hasNext(); ) { Entry<String, Integer> entry = iter.next(); final String word = entry.getKey(); final int counts = entry.getValue().intValue(); try { tWordBag.put(word, counts); for (LexicalRule<? extends RuleInfo> rule : volr.getRulesForLeft(word, null)) { if (tWordBag.containsKey(rule.getRLemma())) { int tmp = tWordBag.get(rule.getRLemma()); tWordBag.put(rule.getRLemma(), tmp + counts); } else { tWordBag.put(rule.getRLemma(), counts); } } } catch (LexicalResourceException e) { throw new ScoringComponentException(e.getMessage()); } } score = calculateSimilarity(tWordBag, hBag).get(0); return score; }
/** * @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()); }
@Override protected ImmutableSet<LexicalRule> getRulesNotInCache(String lhsLemma, PartOfSpeech lhsPos) throws RuleBaseException { try { List< ? extends eu.excitementproject.eop.common.component.lexicalknowledge.LexicalRule< ? extends RuleInfo>> rulesFromResource = realLexicalResource.getRulesForLeft(lhsLemma, lhsPos); Set<LexicalRule> ret = new LinkedHashSet<LexicalRule>(); if (rulesFromResource != null) { for (eu.excitementproject.eop.common.component.lexicalknowledge.LexicalRule< ? extends RuleInfo> ruleFromResource : rulesFromResource) { double confidence = 0.0; if (Constants.LEXICAL_RESOURCES_USE_CONSTANT_SCORE_FOR_ALL_RULES) { confidence = Constants.LEXICAL_RESOURCE_CONSTANT_SCORE_WHEN_USING_CONSTANT_SCORE; } else { confidence = ruleFromResource.getConfidence(); } if ((confidence <= 0) || (confidence >= 1)) throw new RuleBaseException( "Bad confidence for rule from " + this.realLexicalResource.getClass().getSimpleName() + ". The confidene is: " + String.format("%-4.4f", confidence)); ret.add( new LexicalRule( ruleFromResource.getLLemma(), ruleFromResource.getLPos(), ruleFromResource.getRLemma(), ruleFromResource.getRPos(), confidence)); } } return new ImmutableSetWrapper<LexicalRule>(ret); } catch (LexicalResourceException e) { throw new RuleBaseException("Lexical resource failure. See nested exception.", e); } }