use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class RussianSimpleReplaceRuleTest method testRule.
@Test
public void testRule() throws IOException {
RussianSimpleReplaceRule rule = new RussianSimpleReplaceRule(TestTools.getMessages("ru"));
RuleMatch[] matches;
JLanguageTool langTool = new JLanguageTool(new Russian());
// correct sentences:
matches = rule.match(langTool.getAnalyzedSentence("Рост кораллов тут самый быстрый,"));
assertEquals(0, matches.length);
matches = rule.match(langTool.getAnalyzedSentence("Книга была порвана."));
assertEquals(0, matches.length);
// incorrect sentences:
matches = rule.match(langTool.getAnalyzedSentence("Книга была порвата."));
assertEquals(1, matches.length);
assertEquals(1, matches[0].getSuggestedReplacements().size());
assertEquals("порвана", matches[0].getSuggestedReplacements().get(0));
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class DifferentPunctuationRule method match.
@Override
public RuleMatch[] match(AnalyzedSentence sourceText, AnalyzedSentence targetText) throws IOException {
AnalyzedTokenReadings[] translationTokens = targetText.getTokens();
AnalyzedTokenReadings[] sourceTokens = sourceText.getTokens();
AnalyzedTokenReadings lastTransTokenObj = translationTokens[translationTokens.length - 1];
String lastTransToken = lastTransTokenObj.getToken();
if ((".".equals(lastTransToken) || "?".equals(lastTransToken) || "!".equals(lastTransToken)) && !lastTransToken.equals(sourceTokens[sourceTokens.length - 1].getToken())) {
int endPos = lastTransTokenObj.getEndPos();
return new RuleMatch[] { new RuleMatch(this, 1, endPos, getMessage()) };
}
return new RuleMatch[0];
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class PatternRuleMatcher method match.
@Override
public RuleMatch[] match(AnalyzedSentence sentence) throws IOException {
List<RuleMatch> ruleMatches = new ArrayList<>();
AnalyzedTokenReadings[] tokens = sentence.getTokensWithoutWhitespace();
List<Integer> tokenPositions = new ArrayList<>(tokens.length + 1);
int patternSize = patternTokenMatchers.size();
int limit = Math.max(0, tokens.length - patternSize + 1);
PatternTokenMatcher pTokenMatcher = null;
int i = 0;
int minOccurCorrection = getMinOccurrenceCorrection();
while (i < limit + minOccurCorrection && !(rule.isSentStart() && i > 0)) {
int skipShiftTotal = 0;
boolean allElementsMatch = false;
int firstMatchToken = -1;
int lastMatchToken = -1;
int firstMarkerMatchToken = -1;
int lastMarkerMatchToken = -1;
int prevSkipNext = 0;
if (rule.isTestUnification()) {
unifier.reset();
}
tokenPositions.clear();
int minOccurSkip = 0;
for (int k = 0; k < patternSize; k++) {
PatternTokenMatcher prevTokenMatcher = pTokenMatcher;
pTokenMatcher = patternTokenMatchers.get(k);
pTokenMatcher.resolveReference(firstMatchToken, tokens, rule.getLanguage());
int nextPos = i + k + skipShiftTotal - minOccurSkip;
prevMatched = false;
if (prevSkipNext + nextPos >= tokens.length || prevSkipNext < 0) {
// SENT_END?
prevSkipNext = tokens.length - (nextPos + 1);
}
int maxTok = Math.min(nextPos + prevSkipNext, tokens.length - (patternSize - k) + minOccurCorrection);
for (int m = nextPos; m <= maxTok; m++) {
allElementsMatch = !tokens[m].isImmunized() && testAllReadings(tokens, pTokenMatcher, prevTokenMatcher, m, firstMatchToken, prevSkipNext);
if (pTokenMatcher.getPatternToken().getMinOccurrence() == 0) {
boolean foundNext = false;
for (int k2 = k + 1; k2 < patternSize; k2++) {
PatternTokenMatcher nextElement = patternTokenMatchers.get(k2);
boolean nextElementMatch = !tokens[m].isImmunized() && testAllReadings(tokens, nextElement, pTokenMatcher, m, firstMatchToken, prevSkipNext);
if (nextElementMatch) {
// this element doesn't match, but it's optional so accept this and continue
allElementsMatch = true;
minOccurSkip++;
tokenPositions.add(0);
foundNext = true;
break;
} else if (nextElement.getPatternToken().getMinOccurrence() > 0) {
break;
}
}
if (foundNext) {
break;
}
}
if (allElementsMatch) {
int skipForMax = skipMaxTokens(tokens, pTokenMatcher, firstMatchToken, prevSkipNext, prevTokenMatcher, m, patternSize - k - 1);
lastMatchToken = m + skipForMax;
int skipShift = lastMatchToken - nextPos;
tokenPositions.add(skipShift + 1);
prevSkipNext = translateElementNo(pTokenMatcher.getPatternToken().getSkipNext());
skipShiftTotal += skipShift;
if (firstMatchToken == -1) {
firstMatchToken = lastMatchToken - skipForMax;
}
if (firstMarkerMatchToken == -1 && pTokenMatcher.getPatternToken().isInsideMarker()) {
firstMarkerMatchToken = lastMatchToken - skipForMax;
}
if (pTokenMatcher.getPatternToken().isInsideMarker()) {
lastMarkerMatchToken = lastMatchToken;
}
break;
}
}
if (!allElementsMatch) {
break;
}
}
if (allElementsMatch && tokenPositions.size() == patternSize) {
RuleMatch ruleMatch = createRuleMatch(tokenPositions, tokens, firstMatchToken, lastMatchToken, firstMarkerMatchToken, lastMarkerMatchToken);
if (ruleMatch != null) {
ruleMatches.add(ruleMatch);
}
}
i++;
}
RuleMatchFilter maxFilter = new RuleWithMaxFilter();
List<RuleMatch> filteredMatches = maxFilter.filter(ruleMatches);
return filteredMatches.toArray(new RuleMatch[filteredMatches.size()]);
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class RegexPatternRule method match.
@Override
public RuleMatch[] match(AnalyzedSentence sentenceObj) throws IOException {
String sentence = sentenceObj.getText();
Matcher matcher = pattern.matcher(sentence);
int startPos = 0;
List<RuleMatch> matches = new ArrayList<>();
while (matcher.find(startPos)) {
String msg = replaceBackRefs(matcher, message);
boolean sentenceStart = matcher.start(0) == 0;
List<String> suggestions = extractSuggestions(matcher, msg);
List<String> matchSuggestions = getMatchSuggestions(sentence, matcher);
msg = replaceMatchElements(msg, matchSuggestions);
int markStart = matcher.start(markGroup);
int markEnd = matcher.end(markGroup);
RuleMatch ruleMatch = new RuleMatch(this, markStart, markEnd, msg, null, sentenceStart, null);
List<String> allSuggestions = new ArrayList<>();
if (matchSuggestions.size() > 0) {
allSuggestions.addAll(matchSuggestions);
} else {
allSuggestions.addAll(suggestions);
List<String> extendedSuggestions = extractSuggestions(matcher, getSuggestionsOutMsg());
allSuggestions.addAll(extendedSuggestions);
}
ruleMatch.setSuggestedReplacements(allSuggestions);
matches.add(ruleMatch);
startPos = matcher.end();
}
return matches.toArray(new RuleMatch[matches.size()]);
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class HunspellRule method match.
@Override
public RuleMatch[] match(AnalyzedSentence sentence) throws IOException {
List<RuleMatch> ruleMatches = new ArrayList<>();
if (needsInit) {
init();
}
if (hunspellDict == null) {
// some languages might not have a dictionary, be silent about it
return toRuleMatchArray(ruleMatches);
}
String[] tokens = tokenizeText(getSentenceTextWithoutUrlsAndImmunizedTokens(sentence));
// starting with the first token to skip the zero-length START_SENT
int len = sentence.getTokens()[1].getStartPos();
for (int i = 0; i < tokens.length; i++) {
String word = tokens[i];
if (ignoreWord(Arrays.asList(tokens), i) || ignoreWord(word)) {
len += word.length() + 1;
continue;
}
if (isMisspelled(word)) {
RuleMatch ruleMatch = new RuleMatch(this, len, len + word.length(), messages.getString("spelling"), messages.getString("desc_spelling_short"));
List<String> suggestions = getSuggestions(word);
suggestions.addAll(0, getAdditionalTopSuggestions(suggestions, word));
suggestions.addAll(getAdditionalSuggestions(suggestions, word));
if (!suggestions.isEmpty()) {
filterSuggestions(suggestions);
ruleMatch.setSuggestedReplacements(suggestions);
}
ruleMatches.add(ruleMatch);
}
len += word.length() + 1;
}
return toRuleMatchArray(ruleMatches);
}
Aggregations