Search in sources :

Example 21 with RuntimeEnvironment

use of com.joliciel.talismane.machineLearning.features.RuntimeEnvironment in project talismane by joliciel-informatique.

the class PatternEventStream method next.

@Override
public ClassificationEvent next() throws TalismaneException, IOException {
    ClassificationEvent event = null;
    if (this.hasNext()) {
        TokenPatternMatch tokenPatternMatch = currentPatternMatches.get(currentIndex);
        TokeniserOutcome outcome = currentOutcomes.get(currentIndex);
        String classification = outcome.name();
        LOG.debug("next event, pattern match: " + tokenPatternMatch.toString() + ", outcome:" + classification);
        List<FeatureResult<?>> tokenFeatureResults = new ArrayList<FeatureResult<?>>();
        for (TokenPatternMatchFeature<?> feature : tokenPatternMatchFeatures) {
            RuntimeEnvironment env = new RuntimeEnvironment();
            FeatureResult<?> featureResult = feature.check(tokenPatternMatch, env);
            if (featureResult != null) {
                tokenFeatureResults.add(featureResult);
            }
        }
        if (LOG.isTraceEnabled()) {
            SortedSet<String> featureResultSet = tokenFeatureResults.stream().map(f -> f.toString()).collect(Collectors.toCollection(() -> new TreeSet<String>()));
            for (String featureResultString : featureResultSet) {
                LOG.trace(featureResultString);
            }
        }
        event = new ClassificationEvent(tokenFeatureResults, classification);
        currentIndex++;
        if (currentIndex == currentPatternMatches.size()) {
            currentPatternMatches = null;
        }
    }
    return event;
}
Also used : TokeniserAnnotatedCorpusReader(com.joliciel.talismane.tokeniser.TokeniserAnnotatedCorpusReader) SortedSet(java.util.SortedSet) LoggerFactory(org.slf4j.LoggerFactory) TokenSequence(com.joliciel.talismane.tokeniser.TokenSequence) TaggedToken(com.joliciel.talismane.tokeniser.TaggedToken) TreeSet(java.util.TreeSet) TalismaneException(com.joliciel.talismane.TalismaneException) TalismaneSession(com.joliciel.talismane.TalismaneSession) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RuntimeEnvironment(com.joliciel.talismane.machineLearning.features.RuntimeEnvironment) ClassificationEventStream(com.joliciel.talismane.machineLearning.ClassificationEventStream) TokenPatternMatchFeature(com.joliciel.talismane.tokeniser.features.TokenPatternMatchFeature) FeatureResult(com.joliciel.talismane.machineLearning.features.FeatureResult) Map(java.util.Map) Logger(org.slf4j.Logger) Set(java.util.Set) IOException(java.io.IOException) TokeniserOutcome(com.joliciel.talismane.tokeniser.TokeniserOutcome) ClassificationEvent(com.joliciel.talismane.machineLearning.ClassificationEvent) Decision(com.joliciel.talismane.machineLearning.Decision) Collectors(java.util.stream.Collectors) List(java.util.List) Token(com.joliciel.talismane.tokeniser.Token) Sentence(com.joliciel.talismane.rawText.Sentence) RuntimeEnvironment(com.joliciel.talismane.machineLearning.features.RuntimeEnvironment) ArrayList(java.util.ArrayList) TokeniserOutcome(com.joliciel.talismane.tokeniser.TokeniserOutcome) TreeSet(java.util.TreeSet) ClassificationEvent(com.joliciel.talismane.machineLearning.ClassificationEvent) FeatureResult(com.joliciel.talismane.machineLearning.features.FeatureResult)

Example 22 with RuntimeEnvironment

use of com.joliciel.talismane.machineLearning.features.RuntimeEnvironment in project jochre by urieli.

the class LetterFeatureTester method testFeatures.

void testFeatures(ShapeInSequence shapeInSequence, Set<LetterFeature<?>> features) {
    LetterSequence history = null;
    LetterGuesserContext context = new LetterGuesserContext(shapeInSequence, history);
    for (LetterFeature<?> feature : features) {
        RuntimeEnvironment env = new RuntimeEnvironment();
        feature.check(context, env);
    }
}
Also used : LetterSequence(com.joliciel.jochre.letterGuesser.LetterSequence) LetterGuesserContext(com.joliciel.jochre.letterGuesser.LetterGuesserContext) RuntimeEnvironment(com.joliciel.talismane.machineLearning.features.RuntimeEnvironment)

Example 23 with RuntimeEnvironment

use of com.joliciel.talismane.machineLearning.features.RuntimeEnvironment in project jochre by urieli.

the class JochreLetterEventStream method next.

@Override
public ClassificationEvent next() {
    ClassificationEvent event = null;
    if (this.hasNext()) {
        Shape shape = shapeInSequence.getShape();
        LOG.debug("next event, shape: " + shape);
        LetterGuesserContext context = new LetterGuesserContext(shapeInSequence, history);
        List<FeatureResult<?>> featureResults = new ArrayList<>();
        // analyse features
        for (LetterFeature<?> feature : features) {
            RuntimeEnvironment env = new RuntimeEnvironment();
            FeatureResult<?> featureResult = feature.check(context, env);
            if (featureResult != null) {
                featureResults.add(featureResult);
                if (LOG.isTraceEnabled()) {
                    LOG.trace(featureResult.toString());
                }
            }
        }
        String outcome = shape.getLetter();
        event = new ClassificationEvent(featureResults, outcome);
        history.getLetters().add(outcome);
        // set shape to null so that hasNext can retrieve the next one.
        this.shapeInSequence = null;
    }
    return event;
}
Also used : Shape(com.joliciel.jochre.graphics.Shape) RuntimeEnvironment(com.joliciel.talismane.machineLearning.features.RuntimeEnvironment) ArrayList(java.util.ArrayList) ClassificationEvent(com.joliciel.talismane.machineLearning.ClassificationEvent) FeatureResult(com.joliciel.talismane.machineLearning.features.FeatureResult)

Example 24 with RuntimeEnvironment

use of com.joliciel.talismane.machineLearning.features.RuntimeEnvironment in project jochre by urieli.

the class LetterGuesser method guessLetter.

/**
 * Analyses this shape, using the context provided for features that are not
 * intrinsic. Updates shape.getWeightedOutcomes to include all outcomes
 * above a certain threshold of probability.
 *
 * @return the best outcome for this shape.
 */
public String guessLetter(ShapeInSequence shapeInSequence, LetterSequence history) {
    Shape shape = shapeInSequence.getShape();
    if (LOG.isTraceEnabled())
        LOG.trace("guessLetter, shape: " + shape);
    List<FeatureResult<?>> featureResults = new ArrayList<FeatureResult<?>>();
    // analyse features
    for (LetterFeature<?> feature : features) {
        LetterGuesserContext context = new LetterGuesserContext(shapeInSequence, history);
        RuntimeEnvironment env = new RuntimeEnvironment();
        FeatureResult<?> featureResult = feature.check(context, env);
        if (featureResult != null) {
            featureResults.add(featureResult);
            if (LOG.isTraceEnabled()) {
                LOG.trace(featureResult.toString());
            }
        }
    }
    List<Decision> letterGuesses = decisionMaker.decide(featureResults);
    // store outcomes
    String bestOutcome = null;
    shape.getLetterGuesses().clear();
    for (Decision letterGuess : letterGuesses) {
        if (letterGuess.getProbability() >= MIN_PROB_TO_STORE) {
            shape.getLetterGuesses().add(letterGuess);
        }
    }
    bestOutcome = shape.getLetterGuesses().iterator().next().getOutcome();
    if (LOG.isTraceEnabled()) {
        LOG.trace("Shape: " + shape);
        LOG.trace("Letter: " + shape.getLetter());
        LOG.trace("Best outcome: " + bestOutcome);
    }
    return bestOutcome;
}
Also used : Shape(com.joliciel.jochre.graphics.Shape) RuntimeEnvironment(com.joliciel.talismane.machineLearning.features.RuntimeEnvironment) ArrayList(java.util.ArrayList) FeatureResult(com.joliciel.talismane.machineLearning.features.FeatureResult) Decision(com.joliciel.talismane.machineLearning.Decision)

Example 25 with RuntimeEnvironment

use of com.joliciel.talismane.machineLearning.features.RuntimeEnvironment in project jochre by urieli.

the class JochreSplitEventStream method next.

@Override
public ClassificationEvent next() {
    ClassificationEvent event = null;
    if (this.hasNext()) {
        LOG.debug("next event, " + splitCandidate.getShape() + ", split: " + splitCandidate.getPosition());
        List<FeatureResult<?>> featureResults = new ArrayList<FeatureResult<?>>();
        // analyse features
        for (SplitFeature<?> feature : splitFeatures) {
            RuntimeEnvironment env = new RuntimeEnvironment();
            FeatureResult<?> featureResult = feature.check(splitCandidate, env);
            if (featureResult != null) {
                featureResults.add(featureResult);
                if (LOG.isTraceEnabled()) {
                    LOG.trace(featureResult.toString());
                }
            }
        }
        SplitOutcome outcome = SplitOutcome.DO_NOT_SPLIT;
        for (Split split : splitCandidate.getShape().getSplits()) {
            int distance = splitCandidate.getPosition() - split.getPosition();
            if (distance < 0)
                distance = 0 - distance;
            // recall
            if (distance < splitCandidateFinder.getMinDistanceBetweenSplits()) {
                outcome = SplitOutcome.DO_SPLIT;
                break;
            }
        }
        if (outcome.equals(SplitOutcome.DO_SPLIT))
            yesCount++;
        else
            noCount++;
        LOG.debug("Outcome: " + outcome);
        event = new ClassificationEvent(featureResults, outcome.name());
        // set splitCandidate to null so that hasNext can retrieve the next one.
        this.splitCandidate = null;
    }
    return event;
}
Also used : RuntimeEnvironment(com.joliciel.talismane.machineLearning.features.RuntimeEnvironment) ArrayList(java.util.ArrayList) ClassificationEvent(com.joliciel.talismane.machineLearning.ClassificationEvent) FeatureResult(com.joliciel.talismane.machineLearning.features.FeatureResult)

Aggregations

RuntimeEnvironment (com.joliciel.talismane.machineLearning.features.RuntimeEnvironment)26 ArrayList (java.util.ArrayList)19 FeatureResult (com.joliciel.talismane.machineLearning.features.FeatureResult)18 Decision (com.joliciel.talismane.machineLearning.Decision)14 List (java.util.List)13 TokenSequence (com.joliciel.talismane.tokeniser.TokenSequence)10 Config (com.typesafe.config.Config)10 TalismaneException (com.joliciel.talismane.TalismaneException)8 ClassificationEvent (com.joliciel.talismane.machineLearning.ClassificationEvent)8 PosTaggedToken (com.joliciel.talismane.posTagger.PosTaggedToken)8 Sentence (com.joliciel.talismane.rawText.Sentence)8 Token (com.joliciel.talismane.tokeniser.Token)8 IOException (java.io.IOException)8 Map (java.util.Map)8 Set (java.util.Set)8 SortedSet (java.util.SortedSet)8 TreeSet (java.util.TreeSet)8 Collectors (java.util.stream.Collectors)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8