Search in sources :

Example 36 with AnalyzedSentence

use of org.languagetool.AnalyzedSentence in project languagetool by languagetool-org.

the class EnglishConfusionProbabilityRuleTest method assertMatch.

private void assertMatch(String input, int expectedMatches) throws IOException {
    AnalyzedSentence errorSentence = lt.getAnalyzedSentence(input);
    RuleMatch[] matches = rule.match(errorSentence);
    assertThat("Got " + matches.length + " match(es) for: " + input, matches.length, is(expectedMatches));
}
Also used : AnalyzedSentence(org.languagetool.AnalyzedSentence) RuleMatch(org.languagetool.rules.RuleMatch)

Example 37 with AnalyzedSentence

use of org.languagetool.AnalyzedSentence in project languagetool by languagetool-org.

the class PatternRuleTest method testCorrectSentences.

private void testCorrectSentences(JLanguageTool languageTool, JLanguageTool allRulesLanguageTool, Language lang, AbstractPatternRule rule) throws IOException {
    List<CorrectExample> goodSentences = rule.getCorrectExamples();
    // necessary for XML Pattern rules containing <or>
    List<AbstractPatternRule> rules = allRulesLanguageTool.getPatternRulesByIdAndSubId(rule.getId(), rule.getSubId());
    for (CorrectExample goodSentenceObj : goodSentences) {
        // enable indentation use
        String goodSentence = goodSentenceObj.getExample().replaceAll("[\\n\\t]+", "");
        goodSentence = cleanXML(goodSentence);
        assertTrue(lang + ": Empty correct example in rule " + rule.getFullId(), goodSentence.trim().length() > 0);
        boolean isMatched = false;
        // necessary for XML Pattern rules containing <or>
        for (Rule auxRule : rules) {
            isMatched = isMatched || match(auxRule, goodSentence, languageTool);
        }
        if (isMatched) {
            AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(goodSentence);
            StringBuilder sb = new StringBuilder("Analyzed token readings:");
            for (AnalyzedTokenReadings atr : analyzedSentence.getTokens()) {
                sb.append(" ").append(atr);
            }
            fail(lang + ": Did not expect error in:\n" + "  " + goodSentence + "\n" + "  " + sb + "\n" + "Matching Rule: " + rule.getFullId());
        }
    // avoid matches with all the *other* rules:
    /*
      List<RuleMatch> matches = allRulesLanguageTool.check(goodSentence);
      for (RuleMatch match : matches) {
        System.err.println("WARN: " + lang.getShortCode() + ": '" + goodSentence + "' did not match "
                + rule.getId() + " but matched " + match.getRule().getId());
      }
      */
    }
}
Also used : AnalyzedSentence(org.languagetool.AnalyzedSentence) CorrectExample(org.languagetool.rules.CorrectExample) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) DisambiguationPatternRule(org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule) Rule(org.languagetool.rules.Rule) AnalyzedTokenReadings(org.languagetool.AnalyzedTokenReadings)

Example 38 with AnalyzedSentence

use of org.languagetool.AnalyzedSentence in project languagetool by languagetool-org.

the class SimilarNameRuleTest method assertErrors.

private void assertErrors(String input, int expectedMatches, SimilarNameRule rule, JLanguageTool lt) throws IOException {
    AnalyzedSentence sentence = lt.getAnalyzedSentence(input);
    assertThat(rule.match(Collections.singletonList(sentence)).length, is(expectedMatches));
}
Also used : AnalyzedSentence(org.languagetool.AnalyzedSentence)

Example 39 with AnalyzedSentence

use of org.languagetool.AnalyzedSentence in project languagetool by languagetool-org.

the class VerbAgreementRule method match.

@Override
public RuleMatch[] match(List<AnalyzedSentence> sentences) {
    List<RuleMatch> ruleMatches = new ArrayList<>();
    int pos = 0;
    for (AnalyzedSentence sentence : sentences) {
        ruleMatches.addAll(match(sentence, pos));
        pos += sentence.getText().length();
    }
    return toRuleMatchArray(ruleMatches);
}
Also used : AnalyzedSentence(org.languagetool.AnalyzedSentence) ArrayList(java.util.ArrayList)

Example 40 with AnalyzedSentence

use of org.languagetool.AnalyzedSentence in project languagetool by languagetool-org.

the class SimilarNameRule method match.

@Override
public RuleMatch[] match(List<AnalyzedSentence> sentences) throws IOException {
    Set<String> namesSoFar = new HashSet<>();
    List<RuleMatch> ruleMatches = new ArrayList<>();
    int pos = 0;
    for (AnalyzedSentence sentence : sentences) {
        AnalyzedTokenReadings[] tokens = sentence.getTokensWithoutWhitespace();
        for (AnalyzedTokenReadings token : tokens) {
            String word = token.getToken();
            // not tagged = too many correct words are not known so we cannot use that:
            //boolean isName = word.length() > minLength && (token.hasPartialPosTag("EIG:") || !token.isTagged());
            boolean isName = word.length() >= minLength && token.hasPartialPosTag("EIG:") && !token.hasPartialPosTag(":COU");
            if (isName && StringTools.startsWithUppercase(word)) {
                String similarName = similarName(word, namesSoFar);
                if (similarName != null) {
                    String msg = "'" + word + "' ähnelt dem vorher benutzten '" + similarName + "', handelt es sich evtl. um einen Tippfehler?";
                    RuleMatch ruleMatch = new RuleMatch(this, pos + token.getStartPos(), pos + token.getEndPos(), msg);
                    ruleMatch.setSuggestedReplacement(similarName);
                    ruleMatches.add(ruleMatch);
                }
                namesSoFar.add(word);
            }
        }
        pos += sentence.getText().length();
    }
    return toRuleMatchArray(ruleMatches);
}
Also used : AnalyzedSentence(org.languagetool.AnalyzedSentence) AnalyzedTokenReadings(org.languagetool.AnalyzedTokenReadings)

Aggregations

AnalyzedSentence (org.languagetool.AnalyzedSentence)40 AnalyzedTokenReadings (org.languagetool.AnalyzedTokenReadings)21 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 JLanguageTool (org.languagetool.JLanguageTool)8 RuleMatch (org.languagetool.rules.RuleMatch)8 Rule (org.languagetool.rules.Rule)5 IOException (java.io.IOException)4 DisambiguationPatternRule (org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule)4 English (org.languagetool.language.English)3 SpellingCheckRule (org.languagetool.rules.spelling.SpellingCheckRule)3 AnalyzedToken (org.languagetool.AnalyzedToken)2 Ukrainian (org.languagetool.language.Ukrainian)2 InputStream (java.io.InputStream)1 Document (org.apache.lucene.document.Document)1 ConfusionSet (org.languagetool.rules.ConfusionSet)1 CorrectExample (org.languagetool.rules.CorrectExample)1 IncorrectExample (org.languagetool.rules.IncorrectExample)1 BitextRule (org.languagetool.rules.bitext.BitextRule)1 ConfusionProbabilityRule (org.languagetool.rules.ngrams.ConfusionProbabilityRule)1