use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.
the class JLanguageTool method activateDefaultPatternRules.
/**
* Loads and activates the pattern rules from
* {@code org/languagetool/rules/<languageCode>/grammar.xml}.
*/
private void activateDefaultPatternRules() throws IOException {
List<AbstractPatternRule> patternRules = language.getPatternRules();
List<String> enabledRules = language.getDefaultEnabledRulesForVariant();
List<String> disabledRules = language.getDefaultDisabledRulesForVariant();
if (!enabledRules.isEmpty() || !disabledRules.isEmpty()) {
for (AbstractPatternRule patternRule : patternRules) {
if (enabledRules.contains(patternRule.getId())) {
patternRule.setDefaultOn();
}
if (disabledRules.contains(patternRule.getId())) {
patternRule.setDefaultOff();
}
}
}
userRules.addAll(patternRules);
}
use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.
the class CompactStdoutHandler method handleResult.
@Override
protected void handleResult(Sentence sentence, List<RuleMatch> ruleMatches, Language language) {
if (ruleMatches.size() > 0) {
for (RuleMatch match : ruleMatches) {
String ruleId = match.getRule().getId();
if (match.getRule() instanceof AbstractPatternRule) {
AbstractPatternRule pRule = (AbstractPatternRule) match.getRule();
ruleId = pRule.getFullId();
}
System.out.println(ruleId + ": " + contextTools.getContext(match.getFromPos(), match.getToPos(), sentence.getText()));
checkMaxErrors(++errorCount);
}
}
checkMaxSentences(++sentenceCount);
}
use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.
the class CommandLineTools method printMatches.
/**
* Displays matches in a simple text format.
* @param ruleMatches Matches from rules.
* @param prevMatches Number of previously found matches.
* @param contents The text that was checked.
* @param contextSize The size of contents displayed.
* @since 1.0.1
*/
private static void printMatches(List<RuleMatch> ruleMatches, int prevMatches, String contents, int contextSize) {
int i = 1;
ContextTools contextTools = new ContextTools();
contextTools.setContextSize(contextSize);
for (RuleMatch match : ruleMatches) {
Rule rule = match.getRule();
String output = i + prevMatches + ".) Line " + (match.getLine() + 1) + ", column " + match.getColumn() + ", Rule ID: " + rule.getId();
if (rule instanceof AbstractPatternRule) {
AbstractPatternRule pRule = (AbstractPatternRule) rule;
if (pRule.getSubId() != null) {
output += "[" + pRule.getSubId() + "]";
}
}
System.out.println(output);
String msg = match.getMessage();
msg = msg.replaceAll("</?suggestion>", "'");
System.out.println("Message: " + msg);
List<String> replacements = match.getSuggestedReplacements();
if (!replacements.isEmpty()) {
System.out.println("Suggestion: " + String.join("; ", replacements));
}
System.out.println(contextTools.getPlainTextContext(match.getFromPos(), match.getToPos(), contents));
if (rule.getUrl() != null) {
System.out.println("More info: " + rule.getUrl());
}
if (i < ruleMatches.size()) {
System.out.println();
}
i++;
}
}
use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.
the class Main method main.
/**
* Command line tool to check plain text files.
*/
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
JnaTools.setBugWorkaroundProperty();
CommandLineParser commandLineParser = new CommandLineParser();
CommandLineOptions options = null;
try {
options = commandLineParser.parseOptions(args);
} catch (WrongParameterNumberException e) {
commandLineParser.printUsage();
System.exit(1);
} catch (IllegalArgumentException e) {
System.err.println(e.toString());
System.exit(1);
} catch (UnknownParameterException e) {
if (e.getMessage() != null) {
System.err.println(e.getMessage());
} else {
System.err.println(e.toString());
}
commandLineParser.printUsage(System.err);
System.exit(1);
}
if (options.isPrintUsage()) {
commandLineParser.printUsage();
System.exit(1);
}
if (options.isPrintVersion()) {
System.out.println("LanguageTool version " + JLanguageTool.VERSION + " (" + JLanguageTool.BUILD_DATE + ")");
System.exit(0);
}
if (options.isPrintLanguages()) {
printLanguages();
System.exit(0);
}
if (options.getFilename() == null) {
options.setFilename("-");
}
String languageHint = null;
if (options.getLanguage() == null) {
if (!options.isXmlFormat() && !options.isAutoDetect()) {
System.err.println("No language specified, using English (no spell checking active, " + "specify a language variant like 'en-GB' if available)");
}
options.setLanguage(new English());
} else if (!options.isXmlFormat() && !options.isApplySuggestions()) {
languageHint = "Expected text language: " + options.getLanguage().getName();
}
options.getLanguage().getSentenceTokenizer().setSingleLineBreaksMarksParagraph(options.isSingleLineBreakMarksParagraph());
Main prg = new Main(options);
if (options.getFalseFriendFile() != null) {
List<AbstractPatternRule> ffRules = prg.lt.loadFalseFriendRules(options.getFalseFriendFile());
for (AbstractPatternRule ffRule : ffRules) {
prg.lt.addRule(ffRule);
}
}
if (prg.lt.getAllActiveRules().size() == 0) {
List<String> catIds = options.getEnabledCategories().stream().map(i -> i.toString()).collect(Collectors.toList());
throw new RuntimeException("No rules are active. Please make sure your rule ids " + "(" + options.getEnabledRules() + ") and " + "category ids (" + catIds + ") are correct");
}
if (languageHint != null) {
String spellHint = prg.isSpellCheckingActive() ? "" : " (no spell checking active, specify a language variant like 'en-GB' if available)";
System.err.println(languageHint + spellHint);
}
prg.setListUnknownWords(options.isListUnknown());
if (options.isProfile()) {
prg.setProfilingMode();
}
if (options.isBitext()) {
if (options.getMotherTongue() == null) {
throw new IllegalArgumentException("You have to set the source language (as mother tongue) in bitext mode");
}
File bitextRuleFile = options.getBitextRuleFile() != null ? new File(options.getBitextRuleFile()) : null;
prg.setBitextMode(options.getMotherTongue(), options.getDisabledRules(), options.getEnabledRules(), bitextRuleFile);
}
if (options.isRecursive()) {
prg.runRecursive(options.getFilename(), options.getEncoding(), options.isXmlFiltering());
} else {
if (options.isLineByLine()) {
prg.runOnFileLineByLine(options.getFilename(), options.getEncoding());
} else {
prg.runOnFile(options.getFilename(), options.getEncoding(), options.isXmlFiltering());
}
}
prg.cleanUp();
}
use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.
the class DatabaseHandler method handleResult.
@Override
protected void handleResult(Sentence sentence, List<RuleMatch> ruleMatches, Language language) {
try {
java.sql.Date nowDate = new java.sql.Date(new Date().getTime());
for (RuleMatch match : ruleMatches) {
String smallContext = smallContextTools.getContext(match.getFromPos(), match.getToPos(), sentence.getText());
insertSt.setString(1, language.getShortCode());
Rule rule = match.getRule();
insertSt.setString(2, rule.getId());
insertSt.setString(3, rule.getCategory().getName());
if (rule instanceof AbstractPatternRule) {
AbstractPatternRule patternRule = (AbstractPatternRule) rule;
insertSt.setString(4, patternRule.getSubId());
} else {
insertSt.setNull(4, Types.VARCHAR);
}
insertSt.setString(5, rule.getDescription());
insertSt.setString(6, StringUtils.abbreviate(match.getMessage(), 255));
String context = contextTools.getContext(match.getFromPos(), match.getToPos(), sentence.getText());
if (context.length() > MAX_CONTEXT_LENGTH) {
// let's skip these strange cases, as shortening the text might leave us behind with invalid markup etc
continue;
}
insertSt.setString(7, context);
insertSt.setString(8, StringUtils.abbreviate(smallContext, 255));
// should actually be the dump's date, but isn't really used anyway...
insertSt.setDate(9, nowDate);
insertSt.setDate(10, nowDate);
insertSt.setString(11, sentence.getUrl());
insertSt.setString(12, sentence.getSource());
insertSt.addBatch();
if (++batchCount >= batchSize) {
executeBatch();
batchCount = 0;
}
checkMaxErrors(++errorCount);
if (errorCount % 100 == 0) {
System.out.println("Storing error #" + errorCount + " for text:");
System.out.println(" " + sentence.getText());
}
}
checkMaxSentences(++sentenceCount);
} catch (DocumentLimitReachedException | ErrorLimitReachedException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Error storing matches for '" + sentence.getTitle() + "'", e);
}
}
Aggregations