Back to LanguageTool Homepage - Privacy - Imprint

addIgnoreTokens() not working

Hi,

I’m trying to create a Java method that utilizes addIgnoreTokens(). I’m not sure if I am simply misunderstanding what this method does (I’d been assuming that any words added with this method will no longer create a match with langTool.check(text)) or if there’s something wrong with my following code.

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.languagetool.language.AmericanEnglish;
import org.languagetool.JLanguageTool;
import org.languagetool.rules.RuleMatch;
import org.languagetool.tools.ContextTools;
import org.languagetool.rules.spelling.SpellingCheckRule;

public class Grammar_Check_Java 
{
	public static String grammar_checker (String text)
	{
		JLanguageTool langTool = new JLanguageTool(new AmericanEnglish());	//creates new langtool
		List<RuleMatch> matches; //list to hold the error matches
		String errorMessages = "";
		try 
		{
			matches = langTool.check(text); //run the language tool check
			for (RuleMatch match : matches) 
			{
				ContextTools contextTool = new ContextTools();
				errorMessages = errorMessages + "Suggested correction(s): " + match.getSuggestedReplacements() + "\n" + //suggested replacements
				match.getMessage() + "\nError contained in the following line:\n"+  //what the error is
				contextTool.getPlainTextContext(match.getToPos(),match.getFromPos(),text) + "\n"; //snippet of the sentence where the error occurs				} 
			}
		}
		catch (IOException e) {e.printStackTrace();}
		if (errorMessages.length() == 0)
		{
			errorMessages = "No grammar or spelling mistakes found.";			
		}
		else if (errorMessages.length() > 4000)
		{
			errorMessages = "...\n" + errorMessages.substring(0, Math.min(errorMessages.length(), 3990)); // in case the messages are more than 4,000 characters which won't be able to get inserted into the Oracle table
		}
		return errorMessages;
       }
	
	public static void addToDictionary (String text)
	{
		AmericanEnglish language = new AmericanEnglish();
		JLanguageTool langTool = new JLanguageTool(language);	//creates new langtool
		List<RuleMatch> matches; //list to hold the error matches
		try
		{
			matches = langTool.check(text);
			for (RuleMatch match : matches) 
			{
				if (match.getRule() instanceof SpellingCheckRule)
				{
					String s = text.substring(match.getPatternFromPos(), match.getPatternToPos());
					List<String> wordsToIgnore = Arrays.asList(new String[] {s});
	                ((SpellingCheckRule) match.getRule()).addIgnoreTokens(wordsToIgnore);
				}
				
			}
			
		}
		catch (IOException e) {e.printStackTrace();}	
		
		
	}
	
	public static void main (String args[])
	{
		String text = "Helo my name is Dave.";
		addToDictionary(text);
		System.out.println(grammar_checker(text));
	}
}  

I would be expecting an output of “No grammar or spelling mistakes found.” But am instead getting an output of:
Suggested correction(s): [Held, Help, Hello, Hero, Hell, Helm, Halo, Helot, ELO, HEL, He lo]
Possible spelling mistake found.
Error contained in the following line:
Helo my name is Dave.

Thanks for any help!

I haven’t checked closely, but in grammar_checker you create a new JLanguageTool, which doesn’t know about the changes to JLanguageTool in addToDictionary?

Yes there is another JLanguageTool created in grammar_checker. Is there a way that this tool would be able to know about the changes made in addToDictionary?

No, but I think you could just use the same instance of JLanguageTool.

That worked thank you so much for your help!