public class JLanguageTool extends Object
grammar.xml)
Rule classes added with addRule(Rule)
You will probably want to use the sub class MultiThreadedJLanguageTool for best performance.
Unicode: LanguageTool expects text in Normalization Form KC (NFKC). For example, the German character 'ΓΌ' (lowercase u umlaut) must be in the string as Unicode character U+00FC, not as 'u' followed by the combining diaeresis character. See the Oracle docs on normalizing text.
Thread-safety: this class is not thread safe. Create one instance per thread,
but create the language only once (e.g. new English()) and use it for all
instances of JLanguageTool.
MultiThreadedJLanguageTool| Modifier and Type | Class and Description |
|---|---|
static class |
JLanguageTool.ParagraphHandling
Constants for correct paragraph-rule handling:
NORMAL - all kinds of rules run
ONLYPARA - only paragraph-level rules
ONLYNONPARA - only sentence-level rules
|
| Modifier and Type | Field and Description |
|---|---|
static String |
BUILD_DATE
LanguageTool build date and time like
2013-10-17 16:10 or null if not run from JAR. |
static String |
FALSE_FRIEND_FILE
The name of the file with false friend information.
|
static String |
MESSAGE_BUNDLE
Name of the message bundle for translations.
|
static String |
PARAGRAPH_END_TAGNAME
The internal tag used to mark the end of a paragraph.
|
static String |
PATTERN_FILE
The name of the file with error patterns.
|
static String |
SENTENCE_END_TAGNAME
The internal tag used to mark the end of a sentence.
|
static String |
SENTENCE_START_TAGNAME
The internal tag used to mark the beginning of a sentence.
|
static String |
VERSION
LanguageTool version as a string like
2.3 or 2.4-SNAPSHOT. |
| Constructor and Description |
|---|
JLanguageTool(Language language)
Create a JLanguageTool and setup the built-in Java rules for the
given language.
|
JLanguageTool(Language language,
Language motherTongue)
Create a JLanguageTool and setup the built-in rules for the
given language and false friend rules for the text language / mother tongue pair.
|
| Modifier and Type | Method and Description |
|---|---|
void |
activateLanguageModelRules(File indexDir)
Activate rules that depend on a language model.
|
void |
addRule(Rule rule)
Add a rule to be used by the next call to the check methods like
check(String). |
static void |
addTemporaryFile(File file)
Adds a temporary file to the internal list
(internal method, you should never need to call this as a user of LanguageTool)
|
RuleMatch |
adjustRuleMatchPos(RuleMatch match,
int charCount,
int columnCount,
int lineCount,
String sentence,
AnnotatedText annotatedText)
Change RuleMatch positions so they are relative to the complete text,
not just to the sentence.
|
protected List<AnalyzedSentence> |
analyzeSentences(List<String> sentences) |
List<AnalyzedSentence> |
analyzeText(String text)
Use this method if you want to access LanguageTool's otherwise
internal analysis of the text.
|
List<RuleMatch> |
check(AnnotatedText text)
The main check method.
|
List<RuleMatch> |
check(AnnotatedText annotatedText,
boolean tokenizeText,
JLanguageTool.ParagraphHandling paraMode)
The main check method.
|
List<RuleMatch> |
check(String text)
The main check method.
|
List<RuleMatch> |
check(String text,
boolean tokenizeText,
JLanguageTool.ParagraphHandling paraMode) |
List<RuleMatch> |
checkAnalyzedSentence(JLanguageTool.ParagraphHandling paraMode,
List<Rule> allRules,
int charCount,
int lineCount,
int columnCount,
String sentence,
AnalyzedSentence analyzedSentence)
Deprecated.
will be made non-public, please use one of the
check(String) methods instead (deprecated since 3.0) |
List<RuleMatch> |
checkAnalyzedSentence(JLanguageTool.ParagraphHandling paraMode,
List<Rule> rules,
int charCount,
int lineCount,
int columnCount,
String sentence,
AnalyzedSentence analyzedSentence,
AnnotatedText annotatedText)
This is an internal method that's public only for technical reasons, please use one
of the
check(String) methods instead. |
void |
disableCategory(String categoryName)
Disable the given rule category so the check methods like
check(String) won't use it. |
void |
disableRule(String ruleId)
Disable a given rule so the check methods like
check(String) won't use it. |
void |
disableRules(List<String> ruleIds)
Disable the given rules so the check methods like
check(String) won't use them. |
void |
enableDefaultOffRule(String ruleId)
Enable a rule that is switched off by default (
default="off" in the XML). |
void |
enableRule(String ruleId)
Re-enable a given rule so the check methods like
check(String) will use it. |
List<Rule> |
getAllActiveRules()
Get all active (not disabled) rules for the current language that are built-in or that
have been added using e.g.
|
List<Rule> |
getAllRules()
Get all rules for the current language that are built-in or that have been
added using
addRule(Rule). |
AnalyzedSentence |
getAnalyzedSentence(String sentence)
Tokenizes the given
sentence into words and analyzes it,
and then disambiguates POS tags. |
static ResourceDataBroker |
getDataBroker()
The grammar checker needs resources from following
directories:
/resource
/rules
This method is thread-safe. |
Set<String> |
getDisabledCategories()
Get category ids of the rule categories that have been explicitly disabled.
|
Set<String> |
getDisabledRules()
Get rule ids of the rules that have been explicitly disabled.
|
Language |
getLanguage()
Get the language that was used to configure this instance.
|
static ResourceBundle |
getMessageBundle()
Gets the ResourceBundle (i18n strings) for the default language of the user's system.
|
static ResourceBundle |
getMessageBundle(Language lang)
Gets the ResourceBundle (i18n strings) for the given user interface language.
|
List<PatternRule> |
getPatternRulesByIdAndSubId(String Id,
String subId)
Get pattern rules by Id and SubId.
|
AnalyzedSentence |
getRawAnalyzedSentence(String sentence)
Tokenizes the given
sentence into words and analyzes it. |
int |
getSentenceCount()
Deprecated.
use
analyzeText(String) instead (deprecated since 2.7) |
List<String> |
getUnknownWords()
Get the alphabetically sorted list of unknown words in the latest run of one of the
check(String) methods. |
List<PatternRule> |
loadFalseFriendRules(String filename)
Load false friend rules from an XML file.
|
List<PatternRule> |
loadPatternRules(String filename)
Load pattern rules from an XML file.
|
protected List<RuleMatch> |
performCheck(List<AnalyzedSentence> analyzedSentences,
List<String> sentences,
List<Rule> allRules,
JLanguageTool.ParagraphHandling paraMode,
AnnotatedText annotatedText) |
protected void |
printIfVerbose(String s) |
protected void |
printSentenceInfo(AnalyzedSentence analyzedSentence) |
protected void |
rememberUnknownWords(AnalyzedSentence analyzedText) |
static void |
removeTemporaryFiles()
Clean up all temporary files, if there are any.
|
List<String> |
sentenceTokenize(String text)
Tokenizes the given text into sentences.
|
static void |
setDataBroker(ResourceDataBroker broker)
The grammar checker needs resources from following
directories:
/resource
/rules
This method is thread-safe. |
void |
setListUnknownWords(boolean listUnknownWords)
Whether the
check(String) methods store unknown words. |
void |
setOutput(PrintStream printStream)
Set a PrintStream that will receive verbose output.
|
public static final String VERSION
2.3 or 2.4-SNAPSHOT.@Nullable public static final String BUILD_DATE
2013-10-17 16:10 or null if not run from JAR.public static final String PATTERN_FILE
public static final String FALSE_FRIEND_FILE
public static final String SENTENCE_START_TAGNAME
public static final String SENTENCE_END_TAGNAME
public static final String PARAGRAPH_END_TAGNAME
public static final String MESSAGE_BUNDLE
public JLanguageTool(Language language)
language - the language of the text to be checkedpublic JLanguageTool(Language language, Language motherTongue)
language - the language of the text to be checkedmotherTongue - the user's mother tongue, used for false friend rules, or null.
The mother tongue may also be used as a source language for checking bilingual texts.public static ResourceDataBroker getDataBroker()
/resource/rulesDefaultResourceDataBroker will
be instantiated and returned.public static void setDataBroker(ResourceDataBroker broker)
/resource/rulesbroker - The new resource broker to be used.public void setListUnknownWords(boolean listUnknownWords)
check(String) methods store unknown words. If set to
true (default: false), you can get the list of unknown words
using getUnknownWords().public static ResourceBundle getMessageBundle()
public static ResourceBundle getMessageBundle(Language lang)
public void setOutput(PrintStream printStream)
null (which is the default) to disable verbose output.public List<PatternRule> loadPatternRules(String filename) throws IOException
addRule(Rule) to add these
rules to the checking process.filename - path to an XML file in the classpath or in the filesystem - the classpath is checked firstPatternRule objectsIOExceptionpublic List<PatternRule> loadFalseFriendRules(String filename) throws ParserConfigurationException, SAXException, IOException
addRule(Rule) to add these rules to the
checking process.filename - path to an XML file in the classpath or in the filesystem - the classpath is checked firstPatternRule objects, or an empty list if mother tongue is not setParserConfigurationExceptionSAXExceptionIOExceptionpublic void activateLanguageModelRules(File indexDir) throws IOException
indexDir - directory with a '3grams' sub directory which contains a Lucene index with 3gram occurrence countsIOExceptionpublic void addRule(Rule rule)
check(String).public void disableRule(String ruleId)
check(String) won't use it.ruleId - the id of the rule to disable - no error will be thrown if the id does not existpublic void disableRules(List<String> ruleIds)
check(String) won't use them.ruleIds - the ids of the rules to disable - no error will be thrown if the id does not existpublic void disableCategory(String categoryName)
check(String) won't use it.categoryName - the id of the category to disable - no error will be thrown if the id does not existpublic Language getLanguage()
public Set<String> getDisabledRules()
public void enableDefaultOffRule(String ruleId)
default="off" in the XML).ruleId - the id of the turned off rule to enable.public Set<String> getDisabledCategories()
public void enableRule(String ruleId)
check(String) will use it.
Note that you need to use enableDefaultOffRule(String) for rules that
are off by default. This will not throw an exception if the given rule id
doesn't exist.ruleId - the id of the rule to enablepublic List<String> sentenceTokenize(String text)
public List<RuleMatch> check(String text) throws IOException
text - the text to be checkedRuleMatch objectsIOExceptionpublic List<RuleMatch> check(String text, boolean tokenizeText, JLanguageTool.ParagraphHandling paraMode) throws IOException
IOExceptionpublic List<RuleMatch> check(AnnotatedText text) throws IOException
IOExceptionpublic List<RuleMatch> check(AnnotatedText annotatedText, boolean tokenizeText, JLanguageTool.ParagraphHandling paraMode) throws IOException
annotatedText - The text to be checked, created with AnnotatedTextBuilder.
Call this method with the complete text to be checked. If you call it
repeatedly with smaller chunks like paragraphs or sentence, those rules that work across
paragraphs/sentences won't work (their status gets reset whenever this method is called).tokenizeText - If true, then the text is tokenized into sentences.
Otherwise, it is assumed it's already tokenized, i.e. it is only one sentenceparaMode - Uses paragraph-level rules only if true.RuleMatch objects, describing potential errors in the textIOExceptionpublic List<AnalyzedSentence> analyzeText(String text) throws IOException
check... methods instead.text - The text to be analyzedIOExceptionprotected List<AnalyzedSentence> analyzeSentences(List<String> sentences) throws IOException
IOExceptionprotected void printSentenceInfo(AnalyzedSentence analyzedSentence)
protected List<RuleMatch> performCheck(List<AnalyzedSentence> analyzedSentences, List<String> sentences, List<Rule> allRules, JLanguageTool.ParagraphHandling paraMode, AnnotatedText annotatedText) throws IOException
IOExceptionpublic List<RuleMatch> checkAnalyzedSentence(JLanguageTool.ParagraphHandling paraMode, List<Rule> allRules, int charCount, int lineCount, int columnCount, String sentence, AnalyzedSentence analyzedSentence) throws IOException
check(String) methods instead (deprecated since 3.0)IOExceptionpublic List<RuleMatch> checkAnalyzedSentence(JLanguageTool.ParagraphHandling paraMode, List<Rule> rules, int charCount, int lineCount, int columnCount, String sentence, AnalyzedSentence analyzedSentence, AnnotatedText annotatedText) throws IOException
check(String) methods instead.IOExceptionpublic RuleMatch adjustRuleMatchPos(RuleMatch match, int charCount, int columnCount, int lineCount, String sentence, AnnotatedText annotatedText)
charCount - Count of characters in the sentences beforecolumnCount - Current column numberlineCount - Current line numbersentence - The text being checkedprotected void rememberUnknownWords(AnalyzedSentence analyzedText)
public List<String> getUnknownWords()
check(String) methods.IllegalStateException - if setListUnknownWords(boolean) has been set to falsepublic AnalyzedSentence getAnalyzedSentence(String sentence) throws IOException
sentence into words and analyzes it,
and then disambiguates POS tags.sentence - sentence to be analyzedIOExceptionpublic AnalyzedSentence getRawAnalyzedSentence(String sentence) throws IOException
sentence into words and analyzes it.
This is the same as getAnalyzedSentence(String) but it does not run
the disambiguator.sentence - sentence to be analyzedIOExceptionpublic List<Rule> getAllRules()
addRule(Rule).Rule objectspublic List<Rule> getAllActiveRules()
addRule(Rule).Rule objectspublic List<PatternRule> getPatternRulesByIdAndSubId(String Id, String subId)
<or>...</or>
are internally expanded into several rules.Rule objectspublic int getSentenceCount()
analyzeText(String) instead (deprecated since 2.7)check(String) has checked.protected void printIfVerbose(String s)
public static void addTemporaryFile(File file)
file - the file to be added.public static void removeTemporaryFiles()