Search in sources :

Example 1 with SpellingCheckRule

use of org.languagetool.rules.spelling.SpellingCheckRule in project languagetool by languagetool-org.

the class SpellingCheckRuleTest method testIgnorePhrases.

@Test
public void testIgnorePhrases() throws IOException {
    JLanguageTool langTool = new JLanguageTool(new AmericanEnglish());
    assertThat(langTool.check("A test with myfoo mybar").size(), is(2));
    for (Rule rule : langTool.getAllActiveRules()) {
        if (rule instanceof SpellingCheckRule) {
            ((SpellingCheckRule) rule).acceptPhrases(Arrays.asList("myfoo mybar", "Myy othertest"));
        } else {
            langTool.disableRule(rule.getId());
        }
    }
    assertThat(langTool.check("A test with myfoo mybar").size(), is(0));
    // the words on their own are not ignored
    assertThat(langTool.check("A test with myfoo and mybar").size(), is(2));
    assertThat(langTool.check("myfoo mybar here").size(), is(0));
    assertThat(langTool.check("Myfoo mybar here").size(), is(0));
    assertThat(langTool.check("MYfoo mybar here").size(), is(2));
    assertThat(langTool.check("Myy othertest is okay").size(), is(0));
    assertThat(langTool.check("And Myy othertest is okay").size(), is(0));
    assertThat(langTool.check("But Myy Othertest is not okay").size(), is(2));
    assertThat(langTool.check("But myy othertest is not okay").size(), is(2));
}
Also used : SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) JLanguageTool(org.languagetool.JLanguageTool) AmericanEnglish(org.languagetool.language.AmericanEnglish) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) Rule(org.languagetool.rules.Rule) Test(org.junit.Test)

Example 2 with SpellingCheckRule

use of org.languagetool.rules.spelling.SpellingCheckRule in project languagetool by languagetool-org.

the class WordListValidatorTest method testWordListValidity.

@Test
public void testWordListValidity() throws IOException {
    Set<String> checked = new HashSet<>();
    for (Language lang : Languages.get()) {
        if (lang.getShortCode().equals("ru")) {
            // skipping, Cyrillic chars not part of the validation yet
            continue;
        }
        JLanguageTool lt = new JLanguageTool(lang);
        List<Rule> rules = lt.getAllActiveRules();
        for (Rule rule : rules) {
            if (rule instanceof SpellingCheckRule) {
                SpellingCheckRule sRule = (SpellingCheckRule) rule;
                String file = sRule.getSpellingFileName();
                if (JLanguageTool.getDataBroker().resourceExists(file) && !checked.contains(file)) {
                    System.out.println("Checking " + file);
                    CachingWordListLoader loader = new CachingWordListLoader();
                    List<String> words = loader.loadWords(file);
                    validateWords(words, file);
                    checked.add(file);
                }
            }
        }
    }
}
Also used : Language(org.languagetool.Language) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) JLanguageTool(org.languagetool.JLanguageTool) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) CachingWordListLoader(org.languagetool.rules.spelling.CachingWordListLoader) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with SpellingCheckRule

use of org.languagetool.rules.spelling.SpellingCheckRule in project languagetool by languagetool-org.

the class SpellIgnoreTest method testIgnore.

// code also used in http://wiki.languagetool.org/java-api
@Test
public void testIgnore() throws IOException {
    String text = "This is a text with specialword and myotherword";
    JLanguageTool lt = new JLanguageTool(new AmericanEnglish());
    assertThat(lt.check(text).size(), is(2));
    for (Rule rule : lt.getAllActiveRules()) {
        if (rule instanceof SpellingCheckRule) {
            List<String> wordsToIgnore = Arrays.asList("specialword", "myotherword");
            ((SpellingCheckRule) rule).addIgnoreTokens(wordsToIgnore);
        }
    }
    assertThat(lt.check(text).size(), is(0));
}
Also used : SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) AmericanEnglish(org.languagetool.language.AmericanEnglish) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) Rule(org.languagetool.rules.Rule) Test(org.junit.Test)

Example 4 with SpellingCheckRule

use of org.languagetool.rules.spelling.SpellingCheckRule in project languagetool by languagetool-org.

the class SpellingCheckRuleTest method testIgnorePhrases.

@Test
public void testIgnorePhrases() throws IOException {
    JLanguageTool lt = new JLanguageTool(new GermanyGerman());
    assertThat(lt.check("Ein Test mit Auriensis Fantasiewortus").size(), is(2));
    for (Rule rule : lt.getAllActiveRules()) {
        if (rule instanceof SpellingCheckRule) {
            ((SpellingCheckRule) rule).acceptPhrases(Arrays.asList("Auriensis Fantasiewortus", "fudeldu laberwort"));
        } else {
            lt.disableRule(rule.getId());
        }
    }
    assertThat(lt.check("Ein Test mit Auriensis Fantasiewortus").size(), is(0));
    // the words on their own are not ignored
    assertThat(lt.check("Ein Test mit Auriensis und Fantasiewortus").size(), is(2));
    assertThat(lt.check("fudeldu laberwort").size(), is(0));
    // Uppercase at sentence start is okay
    assertThat(lt.check("Fudeldu laberwort").size(), is(0));
    // Different case somewhere other than at sentence start is not okay
    assertThat(lt.check("Fudeldu Laberwort").size(), is(2));
}
Also used : SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) JLanguageTool(org.languagetool.JLanguageTool) GermanSpellerRule(org.languagetool.rules.de.GermanSpellerRule) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) Rule(org.languagetool.rules.Rule) GermanyGerman(org.languagetool.language.GermanyGerman) Test(org.junit.Test)

Example 5 with SpellingCheckRule

use of org.languagetool.rules.spelling.SpellingCheckRule in project languagetool by languagetool-org.

the class JLanguageToolTest method spellCheckerDemoCodeForHomepageWithAddedWords.

@Ignore("not a test, but used on http://wiki.languagetool.org/java-spell-checker")
@Test
public void spellCheckerDemoCodeForHomepageWithAddedWords() throws IOException {
    JLanguageTool langTool = new JLanguageTool(new BritishEnglish());
    for (Rule rule : langTool.getAllRules()) {
        if (rule instanceof SpellingCheckRule) {
            ((SpellingCheckRule) rule).addIgnoreTokens(Arrays.asList("myspecialword", "anotherspecialword"));
        }
    }
    List<RuleMatch> matches = langTool.check("These are myspecialword and anotherspecialword");
    // => "0 matches"
    System.out.println(matches.size() + " matches");
}
Also used : BritishEnglish(org.languagetool.language.BritishEnglish) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) PatternRule(org.languagetool.rules.patterns.PatternRule) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 SpellingCheckRule (org.languagetool.rules.spelling.SpellingCheckRule)5 JLanguageTool (org.languagetool.JLanguageTool)3 Rule (org.languagetool.rules.Rule)3 AmericanEnglish (org.languagetool.language.AmericanEnglish)2 HashSet (java.util.HashSet)1 Ignore (org.junit.Ignore)1 Language (org.languagetool.Language)1 BritishEnglish (org.languagetool.language.BritishEnglish)1 GermanyGerman (org.languagetool.language.GermanyGerman)1 GermanSpellerRule (org.languagetool.rules.de.GermanSpellerRule)1 PatternRule (org.languagetool.rules.patterns.PatternRule)1 CachingWordListLoader (org.languagetool.rules.spelling.CachingWordListLoader)1