use of org.languagetool.MultiThreadedJLanguageTool in project languagetool by languagetool-org.
the class Main method changeLanguage.
private void changeLanguage(Language language, Language motherTongue, List<String> disabledRules, List<String> enabledRules) {
try {
lt = new MultiThreadedJLanguageTool(language, motherTongue);
Tools.selectRules(lt, disabledRules, enabledRules, true);
if (options.isVerbose()) {
lt.setOutput(System.err);
}
} catch (Exception e) {
throw new RuntimeException("Could not create LanguageTool instance for language " + language, e);
}
}
use of org.languagetool.MultiThreadedJLanguageTool in project languagetool by languagetool-org.
the class Main method setBitextMode.
private void setBitextMode(Language sourceLang, List<String> disabledRules, List<String> enabledRules, File bitextRuleFile) throws IOException, ParserConfigurationException, SAXException {
bitextMode = true;
Language target = lt.getLanguage();
lt = new MultiThreadedJLanguageTool(target, null);
srcLt = new MultiThreadedJLanguageTool(sourceLang);
Tools.selectRules(lt, disabledRules, enabledRules, true);
Tools.selectRules(srcLt, disabledRules, enabledRules, true);
bRules = Tools.getBitextRules(sourceLang, lt.getLanguage(), bitextRuleFile);
List<BitextRule> bRuleList = new ArrayList<>(bRules);
for (BitextRule bitextRule : bRules) {
for (String disabledRule : disabledRules) {
if (bitextRule.getId().equals(disabledRule)) {
bRuleList.remove(bitextRule);
}
}
}
bRules = bRuleList;
if (enabledRules.size() > 0) {
bRuleList = new ArrayList<>();
for (String enabledRule : enabledRules) {
for (BitextRule bitextRule : bRules) {
if (bitextRule.getId().equals(enabledRule)) {
bRuleList.add(bitextRule);
}
}
}
bRules = bRuleList;
}
}
use of org.languagetool.MultiThreadedJLanguageTool in project languagetool by languagetool-org.
the class SentenceSourceChecker method run.
private void run(File propFile, Set<String> disabledRules, String langCode, List<String> fileNames, String[] ruleIds, String[] additionalCategoryIds, int maxSentences, int maxErrors, File languageModelDir, Pattern filter) throws IOException {
Language lang = Languages.getLanguageForShortCode(langCode);
MultiThreadedJLanguageTool languageTool = new MultiThreadedJLanguageTool(lang);
languageTool.setCleanOverlappingMatches(false);
if (languageModelDir != null) {
languageTool.activateLanguageModelRules(languageModelDir);
}
if (ruleIds != null) {
enableOnlySpecifiedRules(ruleIds, languageTool);
} else {
applyRuleDeactivation(languageTool, disabledRules);
}
if (filter != null) {
System.out.println("*** NOTE: only sentences that match regular expression '" + filter + "' will be checked");
}
activateAdditionalCategories(additionalCategoryIds, languageTool);
disableSpellingRules(languageTool);
System.out.println("Working on: " + StringUtils.join(fileNames, ", "));
System.out.println("Sentence limit: " + (maxSentences > 0 ? maxSentences : "no limit"));
System.out.println("Error limit: " + (maxErrors > 0 ? maxErrors : "no limit"));
ResultHandler resultHandler = null;
int ruleMatchCount = 0;
int sentenceCount = 0;
try {
if (propFile != null) {
resultHandler = new DatabaseHandler(propFile, maxSentences, maxErrors);
} else {
//resultHandler = new CompactStdoutHandler(maxSentences, maxErrors);
resultHandler = new StdoutHandler(maxSentences, maxErrors);
}
MixingSentenceSource mixingSource = MixingSentenceSource.create(fileNames, lang, filter);
while (mixingSource.hasNext()) {
Sentence sentence = mixingSource.next();
try {
List<RuleMatch> matches = languageTool.check(sentence.getText());
resultHandler.handleResult(sentence, matches, lang);
sentenceCount++;
if (sentenceCount % 5000 == 0) {
System.err.printf("%s sentences checked...\n", NumberFormat.getNumberInstance(Locale.US).format(sentenceCount));
}
ruleMatchCount += matches.size();
} catch (DocumentLimitReachedException | ErrorLimitReachedException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Check failed on sentence: " + StringUtils.abbreviate(sentence.getText(), 250), e);
}
}
} catch (DocumentLimitReachedException | ErrorLimitReachedException e) {
System.out.println(getClass().getSimpleName() + ": " + e);
} finally {
languageTool.shutdown();
if (resultHandler != null) {
float matchesPerSentence = (float) ruleMatchCount / sentenceCount;
System.out.printf(lang + ": %d total matches\n", ruleMatchCount);
System.out.printf(lang + ": ΓΈ%.2f rule matches per sentence\n", matchesPerSentence);
try {
resultHandler.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
use of org.languagetool.MultiThreadedJLanguageTool in project languagetool by languagetool-org.
the class LanguageToolSupport method reloadLanguageTool.
private void reloadLanguageTool(Language language) {
try {
//FIXME
//no need to read again the file
config = new Configuration(new File(System.getProperty("user.home")), CONFIG_FILE, language);
//config still contains old language, update it
this.config.setLanguage(language);
// Calling shutdown here may cause a RejectedExecutionException:
//if (languageTool != null) {
// languageTool.shutdownWhenDone();
//}
languageTool = new MultiThreadedJLanguageTool(language, config.getMotherTongue());
languageTool.setCleanOverlappingMatches(false);
Tools.configureFromRules(languageTool, config);
if (config.getNgramDirectory() != null) {
File ngramLangDir = new File(config.getNgramDirectory(), language.getShortCode());
if (ngramLangDir.exists()) {
try {
languageTool.activateLanguageModelRules(config.getNgramDirectory());
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error while loading ngram database.\n" + e.getMessage());
}
} else {
// user might have set ngram directory to use it for e.g. English, but they
// might not have the data for other languages that supports ngram, so don't
// annoy them with an error dialog:
System.err.println("Not loading ngram data, directory does not exist: " + ngramLangDir);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations