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;
}
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;
}
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;
}
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);
}
}
}
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;
}
Aggregations