Search in sources :

Example 6 with TokeniserOutcome

use of com.joliciel.talismane.tokeniser.TokeniserOutcome 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 7 with TokeniserOutcome

use of com.joliciel.talismane.tokeniser.TokeniserOutcome in project talismane by joliciel-informatique.

the class PatternTokeniser method applyDecision.

TokenisedAtomicTokenSequence applyDecision(Token token, Decision decision, TokenisedAtomicTokenSequence history, TokenPatternMatchSequence matchSequence, Decision defaultDecision) {
    TaggedToken<TokeniserOutcome> taggedToken = new TaggedToken<>(token, decision, TokeniserOutcome.valueOf(decision.getOutcome()));
    TokenisedAtomicTokenSequence tokenisedSequence = new TokenisedAtomicTokenSequence(history);
    tokenisedSequence.add(taggedToken);
    if (decision.isStatistical())
        tokenisedSequence.addDecision(decision);
    if (matchSequence != null) {
        for (Token otherToken : matchSequence.getTokensToCheck()) {
            if (otherToken.equals(token)) {
                continue;
            }
            TaggedToken<TokeniserOutcome> anotherTaggedToken = new TaggedToken<>(otherToken, decision, TokeniserOutcome.valueOf(decision.getOutcome()));
            tokenisedSequence.add(anotherTaggedToken);
        }
    }
    return tokenisedSequence;
}
Also used : TaggedToken(com.joliciel.talismane.tokeniser.TaggedToken) TaggedToken(com.joliciel.talismane.tokeniser.TaggedToken) Token(com.joliciel.talismane.tokeniser.Token) TokeniserOutcome(com.joliciel.talismane.tokeniser.TokeniserOutcome) TokenisedAtomicTokenSequence(com.joliciel.talismane.tokeniser.TokenisedAtomicTokenSequence)

Example 8 with TokeniserOutcome

use of com.joliciel.talismane.tokeniser.TokeniserOutcome in project talismane by joliciel-informatique.

the class TokenEvaluationCorpusWriter method onNextTokenSequence.

@Override
public void onNextTokenSequence(TokenSequence realSequence, List<TokenisedAtomicTokenSequence> guessedAtomicSequences) throws IOException {
    List<Integer> realSplits = realSequence.getTokenSplits();
    TokenisedAtomicTokenSequence tokenisedAtomicTokenSequence = guessedAtomicSequences.get(0);
    Map<Integer, TokeniserOutcome> realOutcomes = new HashMap<Integer, TokeniserOutcome>();
    Map<Integer, TokeniserOutcome> guessedOutcomes = new HashMap<Integer, TokeniserOutcome>();
    Map<Integer, List<String>> guessedAuthorities = new HashMap<Integer, List<String>>();
    List<Integer> indexes = new ArrayList<Integer>();
    corpusWriter.write(realSequence.getSentence().getText() + "\n");
    for (TaggedToken<TokeniserOutcome> guessTag : tokenisedAtomicTokenSequence) {
        TokeniserOutcome guessDecision = guessTag.getTag();
        int startIndex = guessTag.getToken().getStartIndex();
        boolean realSplit = realSplits.contains(startIndex);
        TokeniserOutcome realDecision = realSplit ? TokeniserOutcome.SEPARATE : TokeniserOutcome.JOIN;
        indexes.add(startIndex);
        realOutcomes.put(startIndex, realDecision);
        guessedOutcomes.put(startIndex, guessDecision);
        guessedAuthorities.put(startIndex, guessTag.getDecision().getAuthorities());
    }
    int prevEndIndex = 0;
    for (Token token : realSequence) {
        corpusWriter.write(token.getOriginalText());
        Set<String> authorities = new TreeSet<String>();
        boolean correct = true;
        for (int index : indexes) {
            if (prevEndIndex <= index && index < token.getEndIndex()) {
                correct = correct && realOutcomes.get(index) == guessedOutcomes.get(index);
                authorities.addAll(guessedAuthorities.get(index));
            }
        }
        corpusWriter.write("\t" + correct);
        for (String authority : authorities) {
            if (!authority.startsWith("_")) {
                corpusWriter.write("\t" + authority);
            }
        }
        corpusWriter.write("\n");
        corpusWriter.flush();
        prevEndIndex = token.getEndIndex();
    }
    corpusWriter.write("\n");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TaggedToken(com.joliciel.talismane.tokeniser.TaggedToken) Token(com.joliciel.talismane.tokeniser.Token) TokeniserOutcome(com.joliciel.talismane.tokeniser.TokeniserOutcome) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) TokenisedAtomicTokenSequence(com.joliciel.talismane.tokeniser.TokenisedAtomicTokenSequence)

Example 9 with TokeniserOutcome

use of com.joliciel.talismane.tokeniser.TokeniserOutcome in project talismane by joliciel-informatique.

the class TokenFScoreCalculator method onNextTokenSequence.

@Override
public void onNextTokenSequence(TokenSequence realSequence, List<TokenisedAtomicTokenSequence> guessedAtomicSequences) {
    List<Integer> realSplits = realSequence.getTokenSplits();
    String sentence = realSequence.getSentence().getText().toString();
    TokenisedAtomicTokenSequence tokeniserAtomicTokenSequence = guessedAtomicSequences.get(0);
    TokenSequence guessedSequence = tokeniserAtomicTokenSequence.inferTokenSequence();
    List<Integer> guessedSplits = guessedSequence.getTokenSplits();
    if (LOG.isDebugEnabled()) {
        int pos = 0;
        StringBuilder sb = new StringBuilder();
        for (int split : realSplits) {
            String aToken = sentence.substring(pos, split);
            sb.append('|');
            sb.append(aToken);
            pos = split;
        }
        int pos2 = 0;
        StringBuilder sb2 = new StringBuilder();
        for (int split : guessedSplits) {
            String aToken = sentence.substring(pos2, split);
            sb2.append('|');
            sb2.append(aToken);
            pos2 = split;
        }
        LOG.debug("Real:    " + sb.toString());
        LOG.debug("Guessed: " + sb2.toString());
    }
    for (TaggedToken<TokeniserOutcome> guessTag : tokeniserAtomicTokenSequence) {
        TokeniserOutcome guessDecision = guessTag.getTag();
        boolean realSplit = realSplits.contains(guessTag.getToken().getStartIndex());
        TokeniserOutcome realDecision = realSplit ? TokeniserOutcome.SEPARATE : TokeniserOutcome.JOIN;
        if (!realDecision.equals(guessDecision)) {
            int start1 = guessTag.getToken().getStartIndex() - NUM_CHARS;
            int end1 = guessTag.getToken().getStartIndex() + NUM_CHARS;
            if (start1 < 0)
                start1 = 0;
            String startString = sentence.substring(start1, guessTag.getToken().getStartIndex());
            startString = StringUtils.padLeft(startString, NUM_CHARS);
            if (end1 >= sentence.length())
                end1 = sentence.length() - 1;
            String symbol = "+";
            if (realDecision == TokeniserOutcome.SEPARATE)
                symbol = "-";
            TokeniserErrorRecord errorRecord = new TokeniserErrorRecord();
            errorRecord.realDecision = realDecision;
            errorRecord.guessDecision = guessDecision;
            errorRecord.context = startString + "[" + symbol + "]" + sentence.substring(guessTag.getToken().getStartIndex(), end1);
            LOG.debug("guess " + guessDecision + ", real " + realDecision + ", context: " + errorRecord.context);
            for (String authority : guessTag.getDecision().getAuthorities()) {
                List<TokeniserErrorRecord> errors = errorMap.get(authority);
                if (errors == null) {
                    errors = new ArrayList<TokeniserErrorRecord>();
                    errorMap.put(authority, errors);
                }
                errors.add(errorRecord);
            }
        }
        fScoreCalculator.increment(realDecision, guessDecision);
        for (String authority : guessTag.getDecision().getAuthorities()) {
            FScoreCalculator<TokeniserOutcome> taggerFScoreCalculator = taggerFScoreCalculators.get(authority);
            if (taggerFScoreCalculator == null) {
                taggerFScoreCalculator = new FScoreCalculator<TokeniserOutcome>();
                taggerFScoreCalculators.put(authority, taggerFScoreCalculator);
            }
            taggerFScoreCalculator.increment(realDecision, guessDecision);
        }
    }
// next decision
}
Also used : TokeniserOutcome(com.joliciel.talismane.tokeniser.TokeniserOutcome) TokenisedAtomicTokenSequence(com.joliciel.talismane.tokeniser.TokenisedAtomicTokenSequence) TokenSequence(com.joliciel.talismane.tokeniser.TokenSequence) TokenisedAtomicTokenSequence(com.joliciel.talismane.tokeniser.TokenisedAtomicTokenSequence)

Example 10 with TokeniserOutcome

use of com.joliciel.talismane.tokeniser.TokeniserOutcome in project talismane by joliciel-informatique.

the class TokenComparator method compare.

/**
 * Evaluate the evaluation corpus against the reference corpus.
 *
 * @throws TalismaneException
 * @throws IOException
 */
public void compare() throws TalismaneException, IOException {
    while (referenceCorpusReader.hasNextSentence()) {
        TokenSequence realSequence = referenceCorpusReader.nextTokenSequence();
        TokenSequence guessedSequence = null;
        if (evaluationCorpusReader.hasNextSentence())
            guessedSequence = evaluationCorpusReader.nextTokenSequence();
        else {
            throw new TalismaneException("Wrong number of sentences in eval corpus: " + realSequence.getSentence().getText());
        }
        Sentence sentence = realSequence.getSentence();
        // Initially, separate the sentence into tokens using the separators
        // provided
        TokenSequence realAtomicSequence = new TokenSequence(sentence, sessionId);
        realAtomicSequence.findDefaultTokens();
        TokenSequence guessedAtomicSequence = new TokenSequence(guessedSequence.getSentence(), sessionId);
        guessedAtomicSequence.findDefaultTokens();
        List<TokenPatternMatchSequence> matchingSequences = new ArrayList<TokenPatternMatchSequence>();
        Map<Token, Set<TokenPatternMatchSequence>> tokenMatchSequenceMap = new HashMap<Token, Set<TokenPatternMatchSequence>>();
        Set<Token> matchedTokens = new HashSet<Token>();
        for (TokenPattern parsedPattern : tokeniserPatternManager.getParsedTestPatterns()) {
            List<TokenPatternMatchSequence> matchesForThisPattern = parsedPattern.match(realAtomicSequence);
            for (TokenPatternMatchSequence matchSequence : matchesForThisPattern) {
                matchingSequences.add(matchSequence);
                matchedTokens.addAll(matchSequence.getTokensToCheck());
                Token token = null;
                for (Token aToken : matchSequence.getTokensToCheck()) {
                    token = aToken;
                    if (!aToken.isWhiteSpace()) {
                        break;
                    }
                }
                Set<TokenPatternMatchSequence> matchSequences = tokenMatchSequenceMap.get(token);
                if (matchSequences == null) {
                    matchSequences = new TreeSet<TokenPatternMatchSequence>();
                    tokenMatchSequenceMap.put(token, matchSequences);
                }
                matchSequences.add(matchSequence);
            }
        }
        TokenisedAtomicTokenSequence guess = new TokenisedAtomicTokenSequence(realSequence.getSentence(), 0, sessionId);
        int i = 0;
        int mismatches = 0;
        for (Token token : realAtomicSequence) {
            if (!token.getText().equals(guessedAtomicSequence.get(i).getToken().getText())) {
                // skipped stuff at start of sentence on guess, if it's been
                // through the parser
                TokeniserOutcome outcome = TokeniserOutcome.SEPARATE;
                Decision decision = new Decision(outcome.name());
                decision.addAuthority("_" + this.getClass().getSimpleName());
                Set<TokenPatternMatchSequence> matchSequences = tokenMatchSequenceMap.get(token);
                if (matchSequences != null) {
                    decision.addAuthority("_Patterns");
                    for (TokenPatternMatchSequence matchSequence : matchSequences) {
                        decision.addAuthority(matchSequence.getTokenPattern().getName());
                    }
                }
                guess.addTaggedToken(token, decision, outcome);
                mismatches++;
                LOG.debug("Mismatch: '" + token.getText() + "', '" + guessedAtomicSequence.get(i).getToken().getText() + "'");
                if (mismatches > 6) {
                    LOG.info("Real sequence: " + realSequence.getSentence().getText());
                    LOG.info("Guessed sequence: " + guessedSequence.getSentence().getText());
                    throw new TalismaneException("Too many mismatches for sentence: " + realSequence.getSentence().getText());
                }
                continue;
            }
            TokeniserOutcome outcome = TokeniserOutcome.JOIN;
            if (guessedSequence.getTokenSplits().contains(guessedAtomicSequence.get(i).getToken().getStartIndex())) {
                outcome = TokeniserOutcome.SEPARATE;
            }
            Decision decision = new Decision(outcome.name());
            decision.addAuthority("_" + this.getClass().getSimpleName());
            Set<TokenPatternMatchSequence> matchSequences = tokenMatchSequenceMap.get(token);
            if (matchSequences != null) {
                decision.addAuthority("_Patterns");
                for (TokenPatternMatchSequence matchSequence : matchSequences) {
                    decision.addAuthority(matchSequence.getTokenPattern().getName());
                }
            }
            guess.addTaggedToken(token, decision, outcome);
            i++;
        }
        List<TokenisedAtomicTokenSequence> guessedAtomicSequences = new ArrayList<TokenisedAtomicTokenSequence>();
        guessedAtomicSequences.add(guess);
        for (TokenEvaluationObserver observer : observers) {
            observer.onNextTokenSequence(realSequence, guessedAtomicSequences);
        }
    }
    for (TokenEvaluationObserver observer : observers) {
        observer.onEvaluationComplete();
    }
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) TalismaneException(com.joliciel.talismane.TalismaneException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Token(com.joliciel.talismane.tokeniser.Token) TokeniserOutcome(com.joliciel.talismane.tokeniser.TokeniserOutcome) Decision(com.joliciel.talismane.machineLearning.Decision) TokenPattern(com.joliciel.talismane.tokeniser.patterns.TokenPattern) TokenPatternMatchSequence(com.joliciel.talismane.tokeniser.patterns.TokenPatternMatchSequence) TokenisedAtomicTokenSequence(com.joliciel.talismane.tokeniser.TokenisedAtomicTokenSequence) TokenSequence(com.joliciel.talismane.tokeniser.TokenSequence) Sentence(com.joliciel.talismane.rawText.Sentence) HashSet(java.util.HashSet) TokenisedAtomicTokenSequence(com.joliciel.talismane.tokeniser.TokenisedAtomicTokenSequence)

Aggregations

TokeniserOutcome (com.joliciel.talismane.tokeniser.TokeniserOutcome)10 Token (com.joliciel.talismane.tokeniser.Token)8 TaggedToken (com.joliciel.talismane.tokeniser.TaggedToken)6 ArrayList (java.util.ArrayList)6 TokenSequence (com.joliciel.talismane.tokeniser.TokenSequence)5 TokenisedAtomicTokenSequence (com.joliciel.talismane.tokeniser.TokenisedAtomicTokenSequence)5 Decision (com.joliciel.talismane.machineLearning.Decision)4 Sentence (com.joliciel.talismane.rawText.Sentence)4 TreeSet (java.util.TreeSet)4 TalismaneException (com.joliciel.talismane.TalismaneException)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Set (java.util.Set)3 TalismaneSession (com.joliciel.talismane.TalismaneSession)2 FeatureResult (com.joliciel.talismane.machineLearning.features.FeatureResult)2 RuntimeEnvironment (com.joliciel.talismane.machineLearning.features.RuntimeEnvironment)2 TokenPatternMatchFeature (com.joliciel.talismane.tokeniser.features.TokenPatternMatchFeature)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2