Search in sources :

Example 16 with EdisonException

use of edu.illinois.cs.cogcomp.edison.utilities.EdisonException in project cogcomp-nlp by CogComp.

the class FeatureManifest method processTransform.

private FeatureExtractor processTransform(Tree<String> tree, Map<String, FeatureExtractor> cf) throws EdisonException {
    String uniqueLabel = uniquify(tree);
    if (cf.containsKey(uniqueLabel))
        return cf.get(uniqueLabel);
    if (tree.getNumberOfChildren() != 2)
        throw new EdisonException("transform-input requires two arguments.\n" + tree);
    String transformer = tree.getChild(0).getLabel();
    if (!KnownTransformers.transformers.containsKey(transformer))
        throw new EdisonException("Unknown input transformer '" + transformer + "'. Expecting one of " + KnownTransformers.transformers.keySet());
    FeatureInputTransformer fit = KnownTransformers.transformers.get(transformer);
    CachedFeatureCollection cfx = new CachedFeatureCollection("", fit, createFex(tree.getChild(1), cf));
    cf.put(uniqueLabel, cfx);
    return cfx;
}
Also used : EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException)

Example 17 with EdisonException

use of edu.illinois.cs.cogcomp.edison.utilities.EdisonException in project cogcomp-nlp by CogComp.

the class FeatureManifest method bigrams.

private FeatureExtractor bigrams(Tree<String> tree, Map<String, FeatureExtractor> cf) throws EdisonException {
    String uniqueLabel = uniquify(tree);
    if (cf.containsKey(uniqueLabel))
        return cf.get(uniqueLabel);
    if (tree.getNumberOfChildren() != 1) {
        throw new EdisonException("bigrams takes exactly one argument\n" + tree);
    }
    FeatureExtractor fex = NgramFeatureExtractor.bigrams(getWordFex(createFex(tree.getChild(0), cf)));
    CachedFeatureCollection cfx = new CachedFeatureCollection("", fex);
    cf.put(uniquify(tree), cfx);
    return cfx;
}
Also used : WordNetFeatureExtractor(edu.illinois.cs.cogcomp.edison.features.factory.WordNetFeatureExtractor) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException)

Example 18 with EdisonException

use of edu.illinois.cs.cogcomp.edison.utilities.EdisonException in project cogcomp-nlp by CogComp.

the class FeatureManifest method getLeafFeature.

/**
     * Given a leaf name, find the corresponding FeatureExtractor, as defined in
     * {@link KnownFexes#fexes}
     * 
     * @param label string, needs to be in {@link KnownFexes#fexes}
     * @param cf used for memoization, maps label to FeatureExtractor
     * @return the corresponding FeatureExtractor
     * @throws EdisonException
     */
private FeatureExtractor getLeafFeature(String label, Map<String, FeatureExtractor> cf) throws EdisonException {
    String uniqueLabel = uniquify(label);
    if (cf.containsKey(uniqueLabel))
        return cf.get(uniqueLabel);
    if (!KnownFexes.fexes.containsKey(label))
        throw new EdisonException("Unknown feature extractor '" + label + "', expecting one of " + KnownFexes.fexes.keySet());
    FeatureExtractor featureExtractor = KnownFexes.fexes.get(label);
    cf.put(uniqueLabel, featureExtractor);
    return featureExtractor;
}
Also used : WordNetFeatureExtractor(edu.illinois.cs.cogcomp.edison.features.factory.WordNetFeatureExtractor) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException)

Example 19 with EdisonException

use of edu.illinois.cs.cogcomp.edison.utilities.EdisonException in project cogcomp-nlp by CogComp.

the class ManifestParser method initialize.

/**
     * Given the string content of a manifest file (no comments allowed), parse it. If the file has
     * comments, pass it through the constructor instead.
     * 
     * @param contents contents of the manifest file, without comments.
     * @throws EdisonException
     */
private void initialize(String contents) throws EdisonException {
    this.contents = contents;
    Tree<String> parse = TreeParserFactory.getStringTreeParser().parse(contents);
    if (!parse.getLabel().equals("define")) {
        throw new EdisonException("Invalid feature manifest. Expecting keyword 'define', found " + parse.getLabel() + " instead");
    }
    if (parse.getNumberOfChildren() == 0) {
        throw new EdisonException("A feature set needs a name and a list of features!");
    }
    // first element is label
    featureSetName = parse.getChild(0).getLabel();
    // last element is featuresList
    featuresList = parse.getChild(parse.getNumberOfChildren() - 1);
    definitions = new ArrayList<>();
    variables = new HashMap<>();
    // everything in the middle is define or defvar statements.
    for (int i = 1; i < parse.getNumberOfChildren() - 1; i++) {
        Tree<String> child = parse.getChild(i);
        String label = child.getLabel();
        if (!label.equals("define") && !label.equals("defvar")) {
            throw new EdisonException("Invalid feature definition. Only 'define' statements allowed here, found " + label + "\n" + child);
        }
        if (label.equals("define"))
            definitions.add(child);
        else if (label.equals("defvar")) {
            if (child.getNumberOfChildren() != 2) {
                throw new EdisonException("defvar needs exactly two parameters (defvar variable value)\n" + child);
            }
            String varName = child.getChild(0).getLabel();
            String value = child.getChild(1).getLabel();
            variables.put(varName, value);
        }
    }
}
Also used : EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException)

Example 20 with EdisonException

use of edu.illinois.cs.cogcomp.edison.utilities.EdisonException in project cogcomp-nlp by CogComp.

the class FeatureManifest method populateFex.

/**
     * This adds a FeatureExtractor to the input FeatureCollection. Typically the FeatureCollection
     * is empty, having only a name.
     * 
     * @param fex
     * @return
     * @throws EdisonException
     */
private FeatureExtractor populateFex(FeatureCollection fex) throws EdisonException {
    // cached features.
    Map<String, FeatureExtractor> cf = new HashMap<>();
    // first manage all define statements
    for (Tree<String> defn : parser.getDefinitions()) {
        if (defn.getNumberOfChildren() != 2) {
            throw new EdisonException("Invalid definition. Expecting (define name body)\n" + defn);
        }
        String name = defn.getChild(0).getLabel();
        FeatureExtractor body = this.createFex(defn.getChild(1), cf);
        cf.put(definition(name), new CachedFeatureCollection("", body));
    }
    fex.addFeatureExtractor(this.createFex(parser.getFeatureDescriptor(), cf));
    return fex;
}
Also used : WordNetFeatureExtractor(edu.illinois.cs.cogcomp.edison.features.factory.WordNetFeatureExtractor) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException)

Aggregations

EdisonException (edu.illinois.cs.cogcomp.edison.utilities.EdisonException)41 Constituent (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)22 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)22 Feature (edu.illinois.cs.cogcomp.edison.features.Feature)17 DiscreteFeature (edu.illinois.cs.cogcomp.edison.features.DiscreteFeature)15 TreeView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView)13 LinkedHashSet (java.util.LinkedHashSet)12 Relation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation)8 WordNetFeatureExtractor (edu.illinois.cs.cogcomp.edison.features.factory.WordNetFeatureExtractor)8 HashSet (java.util.HashSet)7 Test (org.junit.Test)6 Set (java.util.Set)5 View (edu.illinois.cs.cogcomp.core.datastructures.textannotation.View)4 ArrayList (java.util.ArrayList)4 RealFeature (edu.illinois.cs.cogcomp.edison.features.RealFeature)3 SpanLabelView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 AnnotatorException (edu.illinois.cs.cogcomp.annotation.AnnotatorException)1 BrownClusterFeatureExtractor (edu.illinois.cs.cogcomp.edison.features.factory.BrownClusterFeatureExtractor)1