use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class MorfologikBritishSpellerRuleTest method testMorfologikSpeller.
@Test
public void testMorfologikSpeller() throws IOException {
BritishEnglish language = new BritishEnglish();
MorfologikBritishSpellerRule rule = new MorfologikBritishSpellerRule(TestTools.getMessages("en"), language);
JLanguageTool langTool = new JLanguageTool(language);
// correct sentences:
assertEquals(0, rule.match(langTool.getAnalyzedSentence("This is an example: we get behaviour as a dictionary word.")).length);
assertEquals(0, rule.match(langTool.getAnalyzedSentence("Why don't we speak today.")).length);
//with doesn't
assertEquals(0, rule.match(langTool.getAnalyzedSentence("He doesn't know what to do.")).length);
//with diacritics
assertEquals(0, rule.match(langTool.getAnalyzedSentence("The entrée at the café.")).length);
//with an abbreviation:
assertEquals(0, rule.match(langTool.getAnalyzedSentence("This is my Ph.D. thesis.")).length);
assertEquals(0, rule.match(langTool.getAnalyzedSentence(",")).length);
assertEquals(0, rule.match(langTool.getAnalyzedSentence("123454")).length);
//incorrect sentences:
RuleMatch[] matches1 = rule.match(langTool.getAnalyzedSentence("Behavior"));
// check match positions:
assertEquals(1, matches1.length);
assertEquals(0, matches1[0].getFromPos());
assertEquals(8, matches1[0].getToPos());
assertEquals("Behaviour", matches1[0].getSuggestedReplacements().get(0));
assertEquals(1, rule.match(langTool.getAnalyzedSentence("aõh")).length);
assertEquals(0, rule.match(langTool.getAnalyzedSentence("a")).length);
//based on replacement pairs:
RuleMatch[] matches2 = rule.match(langTool.getAnalyzedSentence("He teached us."));
// check match positions:
assertEquals(1, matches2.length);
assertEquals(3, matches2[0].getFromPos());
assertEquals(10, matches2[0].getToPos());
assertEquals("taught", matches2[0].getSuggestedReplacements().get(0));
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class MorfologikCanadianSpellerRuleTest method testMorfologikSpeller.
@Test
public void testMorfologikSpeller() throws IOException {
CanadianEnglish language = new CanadianEnglish();
MorfologikBritishSpellerRule rule = new MorfologikBritishSpellerRule(TestTools.getMessages("en"), language);
JLanguageTool langTool = new JLanguageTool(language);
// correct sentences:
assertEquals(0, rule.match(langTool.getAnalyzedSentence("This is an example: we get behaviour as a dictionary word.")).length);
assertEquals(0, rule.match(langTool.getAnalyzedSentence("Why don't we speak today.")).length);
//with doesn't
assertEquals(0, rule.match(langTool.getAnalyzedSentence("He doesn't know what to do.")).length);
assertEquals(0, rule.match(langTool.getAnalyzedSentence(",")).length);
assertEquals(0, rule.match(langTool.getAnalyzedSentence("123454")).length);
//incorrect sentences:
RuleMatch[] matches = rule.match(langTool.getAnalyzedSentence("arbor"));
// check match positions:
assertEquals(1, matches.length);
assertEquals(0, matches[0].getFromPos());
assertEquals(5, matches[0].getToPos());
assertEquals("arbour", matches[0].getSuggestedReplacements().get(0));
assertEquals(1, rule.match(langTool.getAnalyzedSentence("aõh")).length);
assertEquals(0, rule.match(langTool.getAnalyzedSentence("a")).length);
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class AtomFeedChecker method toWikipediaRuleMatches.
private List<WikipediaRuleMatch> toWikipediaRuleMatches(String content, PlainTextMapping filteredContent, List<RuleMatch> ruleMatches, AtomFeedItem item) {
List<WikipediaRuleMatch> result = new ArrayList<>();
for (RuleMatch ruleMatch : ruleMatches) {
Location fromPos = filteredContent.getOriginalTextPositionFor(ruleMatch.getFromPos() + 1);
Location toPos = filteredContent.getOriginalTextPositionFor(ruleMatch.getToPos() + 1);
int origFrom = LocationHelper.absolutePositionFor(fromPos, content);
int origTo = LocationHelper.absolutePositionFor(toPos, content);
String errorContext = contextTools.getContext(origFrom, origTo, content);
result.add(new WikipediaRuleMatch(language, ruleMatch, errorContext, item));
}
return result;
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class WikipediaQuickCheck method main.
/*public static void mainTest(String[] args) throws IOException {
TextFilter filter = new SwebleWikipediaTextFilter();
String plainText = filter.filter("hallo\n* eins\n* zwei");
System.out.println(plainText);
}*/
public static void main(String[] args) throws IOException, PageNotFoundException {
if (args.length != 1) {
System.out.println("Usage: " + WikipediaQuickCheck.class.getName() + " <url>");
System.exit(1);
}
WikipediaQuickCheck check = new WikipediaQuickCheck();
// URL examples:
//String urlString = "http://de.wikipedia.org/wiki/Angela_Merkel";
//String urlString = "https://de.wikipedia.org/wiki/Benutzer_Diskussion:Dnaber";
//String urlString = "https://secure.wikimedia.org/wikipedia/de/wiki/Gütersloh";
String urlString = args[0];
MarkupAwareWikipediaResult result = check.checkPage(new URL(urlString), new ErrorMarker("***", "***"));
int errorCount = 0;
for (AppliedRuleMatch match : result.getAppliedRuleMatches()) {
RuleMatchApplication matchApplication = match.getRuleMatchApplications().get(0);
RuleMatch ruleMatch = match.getRuleMatch();
Rule rule = ruleMatch.getRule();
System.out.println("");
String message = ruleMatch.getMessage().replace("<suggestion>", "'").replace("</suggestion>", "'");
errorCount++;
System.out.print(errorCount + ". " + message);
if (rule instanceof AbstractPatternRule) {
System.out.println(" (" + ((AbstractPatternRule) rule).getFullId() + ")");
} else {
System.out.println(" (" + rule.getId() + ")");
}
System.out.println(" ..." + matchApplication.getOriginalErrorContext(50).replace("\n", "\\n") + "...");
}
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class WikipediaQuickCheck method checkWikipediaMarkup.
MarkupAwareWikipediaResult checkWikipediaMarkup(URL url, MediaWikiContent wikiContent, Language language, ErrorMarker errorMarker) throws IOException {
SwebleWikipediaTextFilter filter = new SwebleWikipediaTextFilter();
PlainTextMapping mapping = filter.filter(wikiContent.getContent());
MultiThreadedJLanguageTool langTool = getLanguageTool(language);
List<AppliedRuleMatch> appliedMatches = new ArrayList<>();
List<RuleMatch> matches;
try {
matches = langTool.check(mapping.getPlainText());
} finally {
langTool.shutdown();
}
int internalErrors = 0;
for (RuleMatch match : matches) {
SuggestionReplacer replacer = errorMarker != null ? new SuggestionReplacer(mapping, wikiContent.getContent(), errorMarker) : new SuggestionReplacer(mapping, wikiContent.getContent());
try {
List<RuleMatchApplication> ruleMatchApplications = replacer.applySuggestionsToOriginalText(match);
appliedMatches.add(new AppliedRuleMatch(match, ruleMatchApplications));
} catch (Exception e) {
System.err.println("Failed to apply suggestion for rule match '" + match + "' for URL " + url + ": " + e);
internalErrors++;
}
}
return new MarkupAwareWikipediaResult(wikiContent, appliedMatches, internalErrors);
}
Aggregations