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));
}
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());
}
*/
}
}
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));
}
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);
}
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);
}
Aggregations