Search in sources :

Example 16 with RuntimeEnvironment

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

the class Jochre method doCommandTestFeature.

/**
 * Test a feature on a particular shape.
 */
public void doCommandTestFeature(int shapeId) {
    // just a utility for testing a feature on a particular shape
    ShapeFeature<?> feature = new VerticalElongationFeature();
    GraphicsDao graphicsDao = GraphicsDao.getInstance(jochreSession);
    if (shapeId > 0) {
        Shape shape = graphicsDao.loadShape(shapeId);
        shape.writeImageToLog();
        RuntimeEnvironment env = new RuntimeEnvironment();
        feature.check(shape, env);
    } else {
        // String result = "false";
        // TrainingServiceLocator trainingServiceLocator =
        // locator.getTrainingServiceLocator();
        // TrainingService trainingService =
        // trainingServiceLocator.getTrainingService();
        // List<Integer> shapeIds =
        // trainingService.findShapesForFeature("ג", feature, result);
        List<Integer> shapeIds = graphicsDao.findShapeIds("—");
        Map<Object, Integer> outcomeMap = new HashMap<>();
        for (int oneShapeId : shapeIds) {
            Shape shape = graphicsDao.loadShape(oneShapeId);
            shape.writeImageToLog();
            RuntimeEnvironment env = new RuntimeEnvironment();
            FeatureResult<?> weightedOutcome = feature.check(shape, env);
            Object outcome = weightedOutcome.getOutcome();
            Integer count = outcomeMap.get(outcome);
            if (count == null)
                outcomeMap.put(outcome, 1);
            else
                outcomeMap.put(outcome, count.intValue() + 1);
        }
        LOG.debug("Outcomes:");
        for (Object outcome : outcomeMap.keySet()) {
            LOG.debug("Outcome " + outcome.toString() + ", count " + outcomeMap.get(outcome));
        }
    }
}
Also used : Shape(com.joliciel.jochre.graphics.Shape) RuntimeEnvironment(com.joliciel.talismane.machineLearning.features.RuntimeEnvironment) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) GraphicsDao(com.joliciel.jochre.graphics.GraphicsDao) VerticalElongationFeature(com.joliciel.jochre.graphics.features.VerticalElongationFeature)

Example 17 with RuntimeEnvironment

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

the class JochreMergeEventStream method next.

@Override
public ClassificationEvent next() {
    ClassificationEvent event = null;
    if (this.hasNext()) {
        LOG.debug("next event, " + mergeCandidate.getFirstShape() + ", " + mergeCandidate.getSecondShape());
        List<FeatureResult<?>> featureResults = new ArrayList<>();
        // analyse features
        for (MergeFeature<?> feature : mergeFeatures) {
            RuntimeEnvironment env = new RuntimeEnvironment();
            FeatureResult<?> featureResult = feature.check(mergeCandidate, env);
            if (featureResult != null) {
                featureResults.add(featureResult);
                if (LOG.isTraceEnabled()) {
                    LOG.trace(featureResult.toString());
                }
            }
        }
        MergeOutcome outcome = MergeOutcome.DO_NOT_MERGE;
        boolean shouldMerge = false;
        if (mergeCandidate.getFirstShape().getLetter().startsWith("|")) {
            if (mergeCandidate.getSecondShape().getLetter().length() == 0 || mergeCandidate.getSecondShape().getLetter().endsWith("|"))
                shouldMerge = true;
        } else if (mergeCandidate.getSecondShape().getLetter().endsWith("|")) {
            if (mergeCandidate.getFirstShape().getLetter().length() == 0)
                shouldMerge = true;
        }
        if (shouldMerge)
            outcome = MergeOutcome.DO_MERGE;
        if (outcome.equals(MergeOutcome.DO_MERGE))
            yesCount++;
        else
            noCount++;
        LOG.debug("Outcome: " + outcome);
        event = new ClassificationEvent(featureResults, outcome.name());
        // set mergeCandidate to null so that hasNext can retrieve the next
        // one.
        this.mergeCandidate = 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)

Example 18 with RuntimeEnvironment

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

the class ShapeMerger method checkMerge.

/**
 * Given two sequential shape, returns the probability of a merge.
 */
public double checkMerge(Shape shape1, Shape shape2) {
    ShapePair mergeCandidate = new ShapePair(shape1, shape2);
    if (LOG.isTraceEnabled())
        LOG.trace("mergeCandidate: " + mergeCandidate);
    List<FeatureResult<?>> featureResults = new ArrayList<FeatureResult<?>>();
    // analyse features
    for (MergeFeature<?> feature : mergeFeatures) {
        RuntimeEnvironment env = new RuntimeEnvironment();
        FeatureResult<?> featureResult = feature.check(mergeCandidate, env);
        if (featureResult != null) {
            featureResults.add(featureResult);
            if (LOG.isTraceEnabled()) {
                LOG.trace(featureResult.toString());
            }
        }
    }
    List<Decision> decisions = decisionMaker.decide(featureResults);
    double yesProb = 0.0;
    for (Decision decision : decisions) {
        if (decision.getOutcome().equals(MergeOutcome.DO_MERGE)) {
            yesProb = decision.getProbability();
            break;
        }
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("yesProb: " + yesProb);
    }
    return yesProb;
}
Also used : 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 19 with RuntimeEnvironment

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

the class PosTagEventStream method next.

@Override
public ClassificationEvent next() throws TalismaneException, IOException {
    ClassificationEvent event = null;
    if (this.hasNext()) {
        PosTaggedToken taggedToken = currentSentence.get(currentIndex++);
        String classification = taggedToken.getTag().getCode();
        if (LOG.isDebugEnabled())
            LOG.debug("next event, token: " + taggedToken.getToken().getAnalyisText() + " : " + classification);
        PosTaggerContext context = new PosTaggerContextImpl(taggedToken.getToken(), currentHistory);
        List<FeatureResult<?>> posTagFeatureResults = new ArrayList<FeatureResult<?>>();
        for (PosTaggerFeature<?> posTaggerFeature : posTaggerFeatures) {
            RuntimeEnvironment env = new RuntimeEnvironment();
            FeatureResult<?> featureResult = posTaggerFeature.check(context, env);
            if (featureResult != null)
                posTagFeatureResults.add(featureResult);
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("Token: " + taggedToken.getToken().getAnalyisText());
            SortedSet<String> featureResultSet = posTagFeatureResults.stream().map(f -> f.toString()).collect(Collectors.toCollection(() -> new TreeSet<String>()));
            for (String featureResultString : featureResultSet) {
                LOG.trace(featureResultString);
            }
        }
        event = new ClassificationEvent(posTagFeatureResults, classification);
        currentHistory.addPosTaggedToken(taggedToken);
        if (currentIndex == currentSentence.size()) {
            currentSentence = null;
        }
    }
    return event;
}
Also used : Logger(org.slf4j.Logger) SortedSet(java.util.SortedSet) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) IOException(java.io.IOException) ClassificationEvent(com.joliciel.talismane.machineLearning.ClassificationEvent) Collectors(java.util.stream.Collectors) TreeSet(java.util.TreeSet) TalismaneException(com.joliciel.talismane.TalismaneException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RuntimeEnvironment(com.joliciel.talismane.machineLearning.features.RuntimeEnvironment) PosTaggerFeature(com.joliciel.talismane.posTagger.features.PosTaggerFeature) List(java.util.List) ClassificationEventStream(com.joliciel.talismane.machineLearning.ClassificationEventStream) FeatureResult(com.joliciel.talismane.machineLearning.features.FeatureResult) Map(java.util.Map) RuntimeEnvironment(com.joliciel.talismane.machineLearning.features.RuntimeEnvironment) ArrayList(java.util.ArrayList) TreeSet(java.util.TreeSet) ClassificationEvent(com.joliciel.talismane.machineLearning.ClassificationEvent) FeatureResult(com.joliciel.talismane.machineLearning.features.FeatureResult)

Example 20 with RuntimeEnvironment

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

the class LanguageDetectorEventStream method next.

@Override
public ClassificationEvent next() throws TalismaneException {
    LanguageTaggedText languageTaggedText = this.corpusReader.nextText();
    List<FeatureResult<?>> featureResults = new ArrayList<FeatureResult<?>>();
    for (LanguageDetectorFeature<?> feature : features) {
        RuntimeEnvironment env = new RuntimeEnvironment();
        FeatureResult<?> featureResult = feature.check(languageTaggedText.getText(), env);
        if (featureResult != null)
            featureResults.add(featureResult);
    }
    String classification = languageTaggedText.getLanguage().toLanguageTag();
    if (LOG.isTraceEnabled()) {
        for (FeatureResult<?> result : featureResults) {
            LOG.trace(result.toString());
        }
        LOG.trace("classification: " + classification);
    }
    ClassificationEvent event = new ClassificationEvent(featureResults, classification);
    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