Java Code Examples for org.languagetool.rules.spelling.SpellingCheckRule

The following examples show how to use org.languagetool.rules.spelling.SpellingCheckRule. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: textidote   Source File: CheckLanguage.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new rule for checking a specific language
 * @param lang The language to check. If {@code null}, the
 * constructor will throw an exception
 * @param dictionary A set of words that should be ignored by
 * spell checking
 * @throws UnsupportedLanguageException If {@code lang} is null
 */
public CheckLanguage(/*@ nullable @*/ Language lang, /*@ non_null @*/ List<String> dictionary) throws UnsupportedLanguageException
{
	super("lt:");
	if (lang == null)
	{
		throw new UnsupportedLanguageException();
	}
	setName("lt:" + lang.getShortCode());
	m_languageTool = new MultiThreadedJLanguageTool(lang);
	if (m_disableWhitespace)
	{
		m_languageTool.disableRule("WHITESPACE_RULE");
	}
	for (org.languagetool.rules.Rule rule : m_languageTool.getAllActiveRules())
	{
		if (rule instanceof SpellingCheckRule)
		{
			((SpellingCheckRule) rule).addIgnoreTokens(dictionary);
		}
	}
	m_dictionary = dictionary;
}
 
Example 2
Source Project: chatbot   Source File: Application.java    License: Apache License 2.0 5 votes vote down vote up
@Autowired
public Helper(final CloudantClient cloudantClient,
              WolframRepository wolframRepository,
              @Value("${cloudant.chatDB}") String chatDBName,
              @Value("${cloudant.feedbackDB}") String feedbackDBName,
              @Value("${cloudant.explorerDB}") String explorerDBName,
              @Value("${tmdb.apiKey}") String tmdbApiKey) {
    try {
        chatDB = cloudantClient.database(chatDBName, true);
        feedbackDB = cloudantClient.database(feedbackDBName, true);
        explorerDB = cloudantClient.database(explorerDBName, true);
    }
    catch(Exception e) {
        logger.info("ERROR HERE");
        e.printStackTrace();
    }
    finally {
        this.tmdbApiKey = tmdbApiKey;
        this.wolframRepository = wolframRepository;

        riveScriptBot = new RiveScriptBot();
        eliza = new ElizaMain();
        eliza.readScript(true, "src/main/resources/eliza/script");

        sparql = new SPARQL(explorerDB);
        languageTool = new JLanguageTool(new AmericanEnglish());
        for (Rule rule : languageTool.getAllActiveRules()) {
            if (rule instanceof SpellingCheckRule) {
                List<String> wordsToIgnore = Arrays.asList(new String[] {"nlp", "merkel"});
                ((SpellingCheckRule) rule).addIgnoreTokens(wordsToIgnore);
            }
        }
    }
}
 
Example 3
Source Project: zest-writer   Source File: Corrector.java    License: GNU General Public License v3.0 5 votes vote down vote up
public String checkHtmlContent(String htmlContent) {
    AnnotatedText markup = makeAnnotatedText(htmlContent);
    StringBuilder bf = new StringBuilder(htmlContent);

    langTool.getAllActiveRules().stream().filter(rule -> rule instanceof SpellingCheckRule).forEach(rule -> ((SpellingCheckRule) rule).acceptPhrases(wordsToIgnore));

    List<RuleMatch> matches = new ArrayList<>();
    try {
        matches = langTool.check(markup);
    }
    catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    int offset = 0;
    for (RuleMatch match : matches) {
        String desc = match.getMessage();
        desc = new HtmlToPlainText().getPlainText(Jsoup.parse(desc));

        if (!match.getSuggestedReplacements().isEmpty()) {
            desc += Configuration.getBundle().getString("ui.alert.correction.tooltip.suggestion")
                    + match.getSuggestedReplacements();
        }
        String before = "<span class=\"error-french\" title=\"" + desc + "\">";
        bf.insert(match.getFromPos() + offset, before);
        offset += before.length();

        String after = "</span> ";
        bf.insert(match.getToPos() + offset, after);
        offset += after.length();

    }
    return bf.toString();
}
 
Example 4
Source Project: zest-writer   Source File: Corrector.java    License: GNU General Public License v3.0 5 votes vote down vote up
public String checkHtmlContentToText(String htmlContent, String source) {
    AnnotatedText markup = makeAnnotatedText(htmlContent);
    StringBuilder bf = new StringBuilder();
    langTool.getAllActiveRules().stream().filter(rule -> rule instanceof SpellingCheckRule).forEach(rule -> ((SpellingCheckRule) rule).addIgnoreTokens(wordsToIgnore));
    List<RuleMatch> matches = new ArrayList<>();
    try {
        matches = langTool.check(markup);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    for (RuleMatch match : matches) {
        String txt = htmlContent.substring(match.getFromPos(), match.getToPos());
        bf.append("\n\n");
        bf.append("> ");
        bf.append(markup.getPlainText().split("[\n|\r]")[match.getLine()].replace(txt, "**" + txt + "**"));
        bf.append("\n");
        bf.append(Configuration.getBundle().getString("ui.alert.correction.source")).append(source);
        bf.append("\n\n");
        bf.append(match.getRule().getDescription());
        bf.append("\n\n");
        for (String s : match.getSuggestedReplacements()) {
            bf.append("- ").append(s).append("\n");
        }
    }
    return bf.toString();
}
 
Example 5
Source Project: zest-writer   Source File: Corrector.java    License: GNU General Public License v3.0 5 votes vote down vote up
public int countMistakes(MdTextController mdTextController, String markdown) {
    String htmlText = StringEscapeUtils.unescapeHtml4(MenuController.markdownToHtml(mdTextController, markdown));
    AnnotatedText markup = makeAnnotatedText(htmlText);

    langTool.getAllActiveRules().stream()
            .filter(rule -> rule instanceof SpellingCheckRule).forEach(rule -> ((SpellingCheckRule) rule).acceptPhrases(wordsToIgnore));
    try {
        List<RuleMatch> matches = langTool.check(markup);
        return matches.size();
    }
    catch (IOException e) {
        log.error(e.getMessage(), e);
    }
    return 0;
}
 
Example 6
private JLanguageTool createLanguageTool() {
  JLanguageTool jLanguageTool = new JLanguageTool(Languages.getLanguageForShortName(language));

  Arrays.stream(rulesToIgnore.split(",")).forEach(jLanguageTool::disableRule);

  jLanguageTool.getAllActiveRules()
    .stream()
    .filter(r -> r instanceof SpellingCheckRule)
    .forEach(r -> ((SpellingCheckRule) r).addIgnoreTokens(Arrays.asList(wordsToIgnore.split(","))));

  return jLanguageTool;
}
 
Example 7
Source Project: Open-LaTeX-Studio   Source File: EditorTopComponent.java    License: MIT License 5 votes vote down vote up
private void setupSpellCheckTool() {     
    painter = new DefaultHighlighter.DefaultHighlightPainter(Color.PINK);  //Default color is: PINK
    langTool = new JLanguageTool(new AmericanEnglish());    //Default Language is: American English
    for (Rule rule : langTool.getAllActiveRules()) {
        if (rule instanceof SpellingCheckRule) {
            ((SpellingCheckRule)rule).acceptPhrases(getLatexTerms());  //Accept LaText Terms from tex.cwl
            ((SpellingCheckRule)rule).acceptPhrases(Arrays.asList("documentclass", "maketitle", "tex", "TEX", "Tex"));  //Accept some TEX terms not contained in tex.cwl
        }
    }
}