Search in sources :

Example 6 with POS

use of net.didion.jwnl.data.POS in project lucida by claritylab.

the class TermExpander method expandTerm.

/**
	 * Expands a term by looking up related terms in ontologies.
	 * 
	 * @param term a term
	 * @param ps predicates in the same sentence
	 * @param ontologies ontologies used to expand the term
	 */
public static void expandTerm(Term term, Predicate[] ps, Ontology[] ontologies) {
    String text = term.getText();
    String pos = term.getPos();
    Map<String, Double> lemmas = new Hashtable<String, Double>();
    Map<String, Double> expansions = new Hashtable<String, Double>();
    // expand events, entities and modifiers
    if (isTarget(term, ps) || pos.startsWith("VB")) {
        // lemmatize verbs that are in WordNet
        String lemma = WordNet.getLemma(text, POS.VERB);
        if (lemma == null)
            lemma = text;
        // set lemma if the POS was misleading
        if (!pos.startsWith("VB"))
            term.setLemma(lemma);
        // expand event
        for (Ontology ontology : ontologies) {
            Map<String, Double> expanded = ontology.expandEvent(lemma);
            lemmas.putAll(expanded);
        }
        // ensure that there are at most MAX_EXPANSIONS expansions with
        // weights of at least MIN_EXPANSION_WEIGHT
        cutOffExpansions(lemmas, true);
        // restore verb form
        if (pos.equals("VBZ")) {
            // third person singular
            for (String exp : lemmas.keySet()) {
                double weight = lemmas.get(exp);
                String form = VerbFormConverter.infinitiveToThirdPersonS(exp);
                expansions.put(form, weight);
            }
        } else if (pos.equals("VBG")) {
            // gerund
            for (String exp : lemmas.keySet()) {
                double weight = lemmas.get(exp);
                String[] forms = VerbFormConverter.infinitiveToGerund(exp);
                for (String form : forms) expansions.put(form, weight);
            }
        } else if (pos.equals("VBD")) {
            // simple past
            for (String exp : lemmas.keySet()) {
                double weight = lemmas.get(exp);
                String[] forms = VerbFormConverter.infinitiveToSimplePast(exp);
                for (String form : forms) expansions.put(form, weight);
            }
        } else if (pos.equals("VBN")) {
            // past participle
            for (String exp : lemmas.keySet()) {
                double weight = lemmas.get(exp);
                String[] forms = VerbFormConverter.infinitiveToPastParticiple(exp);
                for (String form : forms) expansions.put(form, weight);
            }
        }
    } else if (pos.startsWith("JJ") || pos.startsWith("RB")) {
        // get modifier type
        POS modType = (pos.startsWith("JJ")) ? POS.ADJECTIVE : POS.ADVERB;
        // lemmatize adjectives and adverbs that are in WordNet
        String lemma = WordNet.getLemma(text, modType);
        if (lemma == null)
            lemma = text;
        // expand modifier
        for (Ontology ontology : ontologies) {
            Map<String, Double> expanded = ontology.expandModifier(lemma, modType);
            lemmas.putAll(expanded);
        }
        // ensure that there are at most MAX_EXPANSIONS expansions with
        // weights of at least MIN_EXPANSION_WEIGHT
        cutOffExpansions(lemmas, true);
    } else {
        // lemmatize nouns that are in WordNet
        String lemma;
        if (pos.startsWith("COMPOUND"))
            // compound
            lemma = WordNet.getCompoundLemma(text, POS.NOUN);
        else
            // single token
            lemma = WordNet.getLemma(text, POS.NOUN);
        if (lemma == null)
            lemma = text;
        // expand entity
        for (Ontology ontology : ontologies) {
            Map<String, Double> expanded = ontology.expandEntity(lemma);
            lemmas.putAll(expanded);
        }
        // ensure that there are at most MAX_EXPANSIONS expansions with
        // weights of at least MIN_EXPANSION_WEIGHT
        cutOffExpansions(lemmas, true);
    // TODO restore plural forms if possible
    }
    term.setExpansionLemmas(lemmas);
    term.setExpansions((expansions.size() > 0) ? expansions : lemmas);
}
Also used : Ontology(info.ephyra.nlp.semantics.ontologies.Ontology) POS(net.didion.jwnl.data.POS) Hashtable(java.util.Hashtable) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with POS

use of net.didion.jwnl.data.POS in project cogcomp-nlp by CogComp.

the class WordHelpers method getSynset.

public static List<String> getSynset(TextAnnotation ta, int tokenId, WordNetManager wnManager) throws JWNLException {
    String word = getWord(ta, tokenId).toLowerCase();
    String wordPOS = getPOS(ta, tokenId);
    if (POSUtils.isPOSOpenSet(wordPOS)) {
        POS wnPos = getWNPOS(wordPOS);
        return wnManager.getSynonyms(word, wnPos, false);
    } else {
        return new ArrayList<>();
    }
}
Also used : POS(net.didion.jwnl.data.POS) ArrayList(java.util.ArrayList)

Aggregations

POS (net.didion.jwnl.data.POS)7 ArrayList (java.util.ArrayList)4 TokenLabelView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView)2 JWNLException (net.didion.jwnl.JWNLException)2 Ontology (info.ephyra.nlp.semantics.ontologies.Ontology)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1