Search in sources :

Example 51 with RuleMatch

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));
}
Also used : BritishEnglish(org.languagetool.language.BritishEnglish) RuleMatch(org.languagetool.rules.RuleMatch) JLanguageTool(org.languagetool.JLanguageTool) Test(org.junit.Test)

Example 52 with RuleMatch

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);
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) CanadianEnglish(org.languagetool.language.CanadianEnglish) JLanguageTool(org.languagetool.JLanguageTool) Test(org.junit.Test)

Example 53 with RuleMatch

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;
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) Location(xtc.tree.Location)

Example 54 with RuleMatch

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") + "...");
    }
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) Rule(org.languagetool.rules.Rule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule) URL(java.net.URL) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 55 with RuleMatch

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);
}
Also used : ArrayList(java.util.ArrayList) MultiThreadedJLanguageTool(org.languagetool.MultiThreadedJLanguageTool) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) RuleMatch(org.languagetool.rules.RuleMatch)

Aggregations

RuleMatch (org.languagetool.rules.RuleMatch)144 Test (org.junit.Test)64 JLanguageTool (org.languagetool.JLanguageTool)54 ArrayList (java.util.ArrayList)30 AnalyzedTokenReadings (org.languagetool.AnalyzedTokenReadings)14 Rule (org.languagetool.rules.Rule)14 Language (org.languagetool.Language)10 PatternRule (org.languagetool.rules.patterns.PatternRule)10 AnalyzedSentence (org.languagetool.AnalyzedSentence)8 Ukrainian (org.languagetool.language.Ukrainian)8 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)8 Matcher (java.util.regex.Matcher)7 English (org.languagetool.language.English)7 IOException (java.io.IOException)6 Catalan (org.languagetool.language.Catalan)6 Polish (org.languagetool.language.Polish)6 GermanyGerman (org.languagetool.language.GermanyGerman)5 AnnotatedText (org.languagetool.markup.AnnotatedText)5 PatternToken (org.languagetool.rules.patterns.PatternToken)5 AnalyzedToken (org.languagetool.AnalyzedToken)4