Search in sources :

Example 31 with TalismaneException

use of com.joliciel.talismane.TalismaneException in project talismane by joliciel-informatique.

the class PosTagEvaluationObserver method getObservers.

/**
 * Collect the observers specified in the configuration key
 * talismane.core.[sessionId].pos-tagger.evaluate.observers.<br>
 * <br>
 * Each processor must implement this interface and must have a constructor
 * matching one of the following signatures:<br>
 * - ( {@link File} outputDir, {@link String} sessionId)<br>
 * - ( {@link String} sessionId)<br>
 * <br>
 *
 * @param outDir
 *          directory in which to write the various outputs
 * @return
 * @throws IOException
 * @throws TalismaneException
 *           if an observer does not implement this interface, or if no
 *           constructor is found with the correct signature
 */
public static List<PosTagEvaluationObserver> getObservers(File outDir, String sessionId) throws IOException, ClassNotFoundException, ReflectiveOperationException, TalismaneException {
    if (outDir != null)
        outDir.mkdirs();
    Config config = ConfigFactory.load();
    Config posTaggerConfig = config.getConfig("talismane.core." + sessionId + ".pos-tagger");
    Config evalConfig = posTaggerConfig.getConfig("evaluate");
    List<PosTagEvaluationObserver> observers = new ArrayList<>();
    List<PosTagSequenceProcessor> processors = PosTagSequenceProcessor.getProcessors(null, outDir, sessionId);
    for (PosTagSequenceProcessor processor : processors) {
        PosTagSequenceProcessorWrapper wrapper = new PosTagSequenceProcessorWrapper(processor);
        observers.add(wrapper);
    }
    List<String> classes = evalConfig.getStringList("observers");
    if (outDir != null)
        outDir.mkdirs();
    for (String className : classes) {
        @SuppressWarnings("rawtypes") Class untypedClass = Class.forName(className);
        if (!PosTagEvaluationObserver.class.isAssignableFrom(untypedClass))
            throw new TalismaneException("Class " + className + " does not implement interface " + PosTagEvaluationObserver.class.getSimpleName());
        @SuppressWarnings("unchecked") Class<? extends PosTagEvaluationObserver> clazz = untypedClass;
        Constructor<? extends PosTagEvaluationObserver> cons = null;
        PosTagEvaluationObserver observer = null;
        if (cons == null) {
            try {
                cons = clazz.getConstructor(File.class, String.class);
            } catch (NoSuchMethodException e) {
            // do nothing
            }
            if (cons != null) {
                observer = cons.newInstance(outDir, sessionId);
            }
        }
        if (cons == null) {
            try {
                cons = clazz.getConstructor(String.class);
            } catch (NoSuchMethodException e) {
            // do nothing
            }
            if (cons != null) {
                observer = cons.newInstance(sessionId);
            } else {
                throw new TalismaneException("No constructor found with correct signature for: " + className);
            }
        }
        observers.add(observer);
    }
    return observers;
}
Also used : TalismaneException(com.joliciel.talismane.TalismaneException) Config(com.typesafe.config.Config) ArrayList(java.util.ArrayList) PosTagSequenceProcessor(com.joliciel.talismane.posTagger.output.PosTagSequenceProcessor) File(java.io.File)

Example 32 with TalismaneException

use of com.joliciel.talismane.TalismaneException in project talismane by joliciel-informatique.

the class ParserFeatureParser method getRules.

public List<ParserRule> getRules(List<String> ruleDescriptors) throws TalismaneException {
    List<ParserRule> rules = new ArrayList<ParserRule>();
    FunctionDescriptorParser descriptorParser = new FunctionDescriptorParser();
    for (String ruleDescriptor : ruleDescriptors) {
        LOG.debug(ruleDescriptor);
        if (ruleDescriptor.trim().length() > 0 && !ruleDescriptor.startsWith("#")) {
            String[] ruleParts = ruleDescriptor.split("\t");
            String transitionCode = ruleParts[0];
            Transition transition = null;
            Set<Transition> transitions = null;
            boolean negative = false;
            String descriptor = null;
            String descriptorName = null;
            if (ruleParts.length > 2) {
                descriptor = ruleParts[2];
                descriptorName = ruleParts[1];
            } else {
                descriptor = ruleParts[1];
            }
            if (transitionCode.length() == 0) {
                if (descriptorName == null) {
                    throw new TalismaneException("Rule without Transition must have a name.");
                }
            } else {
                if (transitionCode.startsWith("!")) {
                    negative = true;
                    String[] transitionCodes = transitionCode.substring(1).split(";");
                    transitions = new HashSet<Transition>();
                    for (String code : transitionCodes) {
                        Transition oneTransition = TalismaneSession.get(sessionId).getTransitionSystem().getTransitionForCode(code);
                        transitions.add(oneTransition);
                    }
                    transition = transitions.iterator().next();
                } else {
                    transition = TalismaneSession.get(sessionId).getTransitionSystem().getTransitionForCode(transitionCode);
                }
            }
            FunctionDescriptor functionDescriptor = descriptorParser.parseDescriptor(descriptor);
            if (descriptorName != null)
                functionDescriptor.setDescriptorName(descriptorName);
            List<ParseConfigurationFeature<?>> myFeatures = this.parseDescriptor(functionDescriptor);
            if (transition != null) {
                for (ParseConfigurationFeature<?> feature : myFeatures) {
                    if (feature instanceof BooleanFeature) {
                        @SuppressWarnings("unchecked") BooleanFeature<ParseConfigurationWrapper> condition = (BooleanFeature<ParseConfigurationWrapper>) feature;
                        if (negative) {
                            ParserRule rule = new ParserRule(condition, transitions, true);
                            rules.add(rule);
                        } else {
                            ParserRule rule = new ParserRule(condition, transition, false);
                            rules.add(rule);
                        }
                    } else {
                        throw new TalismaneException("Rule must be based on a boolean feature.");
                    }
                }
            // next feature
            }
        // is it a rule, or just a descriptor
        }
    // proper rule descriptor
    }
    // next rule descriptor
    return rules;
}
Also used : TalismaneException(com.joliciel.talismane.TalismaneException) ArrayList(java.util.ArrayList) FunctionDescriptorParser(com.joliciel.talismane.machineLearning.features.FunctionDescriptorParser) FunctionDescriptor(com.joliciel.talismane.machineLearning.features.FunctionDescriptor) BooleanFeature(com.joliciel.talismane.machineLearning.features.BooleanFeature) Transition(com.joliciel.talismane.parser.Transition)

Example 33 with TalismaneException

use of com.joliciel.talismane.TalismaneException in project talismane by joliciel-informatique.

the class ParseEvaluationObserver method getObservers.

/**
 * Collect the observers specified in the configuration key
 * talismane.core.[sessionId].parser.evaluate.observers.<br>
 * <br>
 * Each processor must implement this interface and must have a constructor
 * matching one of the following signatures:<br>
 * - ( {@link File} outputDir, {@link String} sessionId)<br>
 * - ( {@link String} sessionId)<br>
 * <br>
 *
 * @param outDir
 *          directory in which to write the various outputs
 * @return
 * @throws IOException
 * @throws TalismaneException
 *           if an observer does not implement this interface, or if no
 *           constructor is found with the correct signature
 */
public static List<ParseEvaluationObserver> getObservers(File outDir, String sessionId) throws IOException, ClassNotFoundException, ReflectiveOperationException, TalismaneException {
    if (outDir != null)
        outDir.mkdirs();
    Config config = ConfigFactory.load();
    Config parserConfig = config.getConfig("talismane.core." + sessionId + ".parser");
    Config evalConfig = parserConfig.getConfig("evaluate");
    List<ParseEvaluationObserver> observers = new ArrayList<>();
    List<ParseConfigurationProcessor> processors = ParseConfigurationProcessor.getProcessors(null, outDir, sessionId);
    for (ParseConfigurationProcessor processor : processors) {
        ParseConfigurationProcessorWrapper wrapper = new ParseConfigurationProcessorWrapper(processor);
        observers.add(wrapper);
    }
    List<String> classes = evalConfig.getStringList("observers");
    if (outDir != null)
        outDir.mkdirs();
    for (String className : classes) {
        @SuppressWarnings("rawtypes") Class untypedClass = Class.forName(className);
        if (!ParseEvaluationObserver.class.isAssignableFrom(untypedClass))
            throw new TalismaneException("Class " + className + " does not implement interface " + ParseEvaluationObserver.class.getSimpleName());
        @SuppressWarnings("unchecked") Class<? extends ParseEvaluationObserver> clazz = untypedClass;
        Constructor<? extends ParseEvaluationObserver> cons = null;
        ParseEvaluationObserver observer = null;
        if (cons == null) {
            try {
                cons = clazz.getConstructor(File.class, String.class);
            } catch (NoSuchMethodException e) {
            // do nothing
            }
            if (cons != null) {
                observer = cons.newInstance(outDir, sessionId);
            }
        }
        if (cons == null) {
            try {
                cons = clazz.getConstructor(String.class);
            } catch (NoSuchMethodException e) {
            // do nothing
            }
            if (cons != null) {
                observer = cons.newInstance(sessionId);
            } else {
                throw new TalismaneException("No constructor found with correct signature for: " + className);
            }
        }
        observers.add(observer);
    }
    return observers;
}
Also used : TalismaneException(com.joliciel.talismane.TalismaneException) Config(com.typesafe.config.Config) ArrayList(java.util.ArrayList) File(java.io.File) ParseConfigurationProcessor(com.joliciel.talismane.parser.output.ParseConfigurationProcessor)

Example 34 with TalismaneException

use of com.joliciel.talismane.TalismaneException in project talismane by joliciel-informatique.

the class Parsers method getParser.

public static Parser getParser(String sessionId) throws IOException, TalismaneException, ClassNotFoundException, ReflectiveOperationException {
    Parser parser = parserMap.get(sessionId);
    if (parser == null) {
        Config config = ConfigFactory.load();
        Config parserConfig = config.getConfig("talismane.core." + sessionId + ".parser");
        String className = parserConfig.getString("parser");
        @SuppressWarnings("rawtypes") Class untypedClass = Class.forName(className);
        if (!Parser.class.isAssignableFrom(untypedClass))
            throw new TalismaneException("Class " + className + " does not implement interface " + Parser.class.getSimpleName());
        @SuppressWarnings("unchecked") Class<? extends Parser> clazz = untypedClass;
        Constructor<? extends Parser> cons = null;
        if (cons == null) {
            try {
                cons = clazz.getConstructor(String.class);
            } catch (NoSuchMethodException e) {
            // do nothing
            }
            if (cons != null) {
                parser = cons.newInstance(sessionId);
            } else {
                throw new TalismaneException("No constructor found with correct signature for: " + className);
            }
        }
        parserMap.put(sessionId, parser);
    }
    return parser.cloneParser();
}
Also used : TalismaneException(com.joliciel.talismane.TalismaneException) Config(com.typesafe.config.Config)

Example 35 with TalismaneException

use of com.joliciel.talismane.TalismaneException in project talismane by joliciel-informatique.

the class ParseOutputRewriter method setElementValues.

private void setElementValues(Map<CorpusElement, String> elementValues, Map<Integer, Integer> oldToNewIndexMap, CorpusLine origLine, CorpusLine splitCorpusLine) throws TalismaneException {
    for (CorpusElement key : elementValues.keySet()) {
        String elementValue = elementValues.get(key);
        if (elementValue.equals("${orig}")) {
            splitCorpusLine.setElement(key, origLine.getElement(key));
        } else {
            Matcher matcher = linePattern.matcher(elementValue);
            if (matcher.matches()) {
                int lineNumber = Integer.parseInt(matcher.group(1));
                int equivalentIndex = (origLine.getIndex() + lineNumber) - 1;
                switch(key) {
                    case GOVERNOR:
                        splitCorpusLine.setGovernorIndex(equivalentIndex);
                        break;
                    case NON_PROJ_GOVERNOR:
                        splitCorpusLine.setNonProjGovernorIndex(equivalentIndex);
                        break;
                    default:
                        throw new TalismaneException("element value '" + elementValue + "' not supported for corpus element " + key.name());
                }
            } else {
                splitCorpusLine.setElement(key, elementValue);
            }
        }
    }
}
Also used : CorpusElement(com.joliciel.talismane.corpus.CorpusLine.CorpusElement) Matcher(java.util.regex.Matcher) TalismaneException(com.joliciel.talismane.TalismaneException)

Aggregations

TalismaneException (com.joliciel.talismane.TalismaneException)47 ArrayList (java.util.ArrayList)27 Config (com.typesafe.config.Config)14 File (java.io.File)11 List (java.util.List)10 TreeSet (java.util.TreeSet)10 FeatureResult (com.joliciel.talismane.machineLearning.features.FeatureResult)9 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 Set (java.util.Set)9 Decision (com.joliciel.talismane.machineLearning.Decision)8 RuntimeEnvironment (com.joliciel.talismane.machineLearning.features.RuntimeEnvironment)8 PosTaggedToken (com.joliciel.talismane.posTagger.PosTaggedToken)8 Token (com.joliciel.talismane.tokeniser.Token)8 Map (java.util.Map)8 SortedSet (java.util.SortedSet)8 Collectors (java.util.stream.Collectors)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 Sentence (com.joliciel.talismane.rawText.Sentence)7