Search in sources :

Example 6 with MultiThreadedJLanguageTool

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);
    }
}
Also used : MultiThreadedJLanguageTool(org.languagetool.MultiThreadedJLanguageTool) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 7 with MultiThreadedJLanguageTool

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;
    }
}
Also used : Language(org.languagetool.Language) ArrayList(java.util.ArrayList) MultiThreadedJLanguageTool(org.languagetool.MultiThreadedJLanguageTool) StringTools.readerToString(org.languagetool.tools.StringTools.readerToString) BitextRule(org.languagetool.rules.bitext.BitextRule)

Example 8 with MultiThreadedJLanguageTool

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();
            }
        }
    }
}
Also used : MultiThreadedJLanguageTool(org.languagetool.MultiThreadedJLanguageTool) IOException(java.io.IOException) RuleMatch(org.languagetool.rules.RuleMatch) Language(org.languagetool.Language)

Example 9 with MultiThreadedJLanguageTool

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);
    }
}
Also used : MultiThreadedJLanguageTool(org.languagetool.MultiThreadedJLanguageTool) File(java.io.File) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

MultiThreadedJLanguageTool (org.languagetool.MultiThreadedJLanguageTool)9 IOException (java.io.IOException)4 RuleMatch (org.languagetool.rules.RuleMatch)4 Language (org.languagetool.Language)3 ArrayList (java.util.ArrayList)2 SAXException (org.xml.sax.SAXException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Random (java.util.Random)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 IncorrectExample (org.languagetool.rules.IncorrectExample)1 BitextRule (org.languagetool.rules.bitext.BitextRule)1 DisambiguationPatternRule (org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule)1 StringTools.readerToString (org.languagetool.tools.StringTools.readerToString)1