Search in sources :

Example 61 with Language

use of org.languagetool.Language in project languagetool by languagetool-org.

the class LanguageBuilder method makeLanguage.

/**
   * Takes an XML file named <tt>rules-xx-language.xml</tt>,
   * e.g. <tt>rules-de-German.xml</tt> and builds
   * a Language object for that language.
   */
private static Language makeLanguage(File file, boolean isAdditional) throws IllegalAccessException, InstantiationException {
    Objects.requireNonNull(file, "file cannot be null");
    if (!file.getName().endsWith(".xml")) {
        throw new RuleFilenameException(file);
    }
    String[] parts = file.getName().split("-");
    boolean startsWithRules = parts[0].equals("rules");
    boolean secondPartHasCorrectLength = parts.length == 3 && (parts[1].length() == "en".length() || parts[1].length() == "ast".length() || parts[1].length() == "en_US".length());
    if (!startsWithRules || !secondPartHasCorrectLength) {
        throw new RuleFilenameException(file);
    }
    //TODO: when the XML file is mergeable with
    // other rules (check this in the XML Rule Loader by using rules[@integrate='add']?),
    // subclass the existing language,
    //and adjust the settings if any are set in the rule file default configuration set
    Language newLanguage;
    if (Languages.isLanguageSupported(parts[1])) {
        Language baseLanguage = Languages.getLanguageForShortCode(parts[1]).getClass().newInstance();
        newLanguage = new ExtendedLanguage(baseLanguage, parts[2].replace(".xml", ""), file);
    } else {
        newLanguage = new Language() {

            @Override
            public Locale getLocale() {
                return new Locale(getShortCode());
            }

            @Override
            public Contributor[] getMaintainers() {
                return null;
            }

            @Override
            public String getShortCode() {
                if (parts[1].length() == 2) {
                    return parts[1];
                }
                //en as in en_US
                return parts[1].split("_")[0];
            }

            @Override
            public String[] getCountries() {
                if (parts[1].length() == 2) {
                    return new String[] { "" };
                }
                //US as in en_US
                return new String[] { parts[1].split("_")[1] };
            }

            @Override
            public String getName() {
                return parts[2].replace(".xml", "");
            }

            @Override
            public List<Rule> getRelevantRules(ResourceBundle messages) {
                return Collections.emptyList();
            }

            @Override
            public List<String> getRuleFileNames() {
                List<String> ruleFiles = new ArrayList<>();
                ruleFiles.add(file.getAbsolutePath());
                return ruleFiles;
            }

            @Override
            public boolean isExternal() {
                return isAdditional;
            }
        };
    }
    return newLanguage;
}
Also used : Language(org.languagetool.Language)

Example 62 with Language

use of org.languagetool.Language in project languagetool by languagetool-org.

the class LanguageIdentifier method getLanguageCodes.

private static List<String> getLanguageCodes() {
    List<String> langCodes = new ArrayList<>();
    for (Language lang : Languages.get()) {
        String langCode = lang.getShortCode();
        boolean ignore = lang.isVariant() || ignoreLangCodes.contains(langCode) || externalLangCodes.contains(langCode);
        if (ignore) {
            continue;
        }
        if ("zh".equals(langCode)) {
            langCodes.add("zh-CN");
            langCodes.add("zh-TW");
        } else {
            langCodes.add(langCode);
        }
    }
    return langCodes;
}
Also used : Language(org.languagetool.Language) ArrayList(java.util.ArrayList)

Example 63 with Language

use of org.languagetool.Language in project languagetool by languagetool-org.

the class LanguageToolSupport method checkText.

private synchronized List<RuleMatch> checkText(Object caller) throws IOException {
    if (this.mustDetectLanguage) {
        mustDetectLanguage = false;
        if (!this.textComponent.getText().isEmpty()) {
            Language detectedLanguage = autoDetectLanguage(this.textComponent.getText());
            if (!detectedLanguage.equals(this.languageTool.getLanguage())) {
                reloadLanguageTool(detectedLanguage);
                if (SwingUtilities.isEventDispatchThread()) {
                    fireEvent(LanguageToolEvent.Type.LANGUAGE_CHANGED, caller);
                } else {
                    try {
                        SwingUtilities.invokeAndWait(() -> fireEvent(LanguageToolEvent.Type.LANGUAGE_CHANGED, caller));
                    } catch (InterruptedException ex) {
                    //ignore
                    } catch (InvocationTargetException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        }
    }
    if (SwingUtilities.isEventDispatchThread()) {
        fireEvent(LanguageToolEvent.Type.CHECKING_STARTED, caller);
    } else {
        try {
            SwingUtilities.invokeAndWait(() -> fireEvent(LanguageToolEvent.Type.CHECKING_STARTED, caller));
        } catch (InterruptedException ex) {
        //ignore
        } catch (InvocationTargetException ex) {
            throw new RuntimeException(ex);
        }
    }
    long startTime = System.currentTimeMillis();
    List<RuleMatch> matches = this.languageTool.check(this.textComponent.getText());
    long elapsedTime = System.currentTimeMillis() - startTime;
    int v = check.get();
    if (v == 0) {
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeLater(() -> {
                updateHighlights(matches);
                fireEvent(LanguageToolEvent.Type.CHECKING_FINISHED, caller, elapsedTime);
            });
        } else {
            updateHighlights(matches);
            fireEvent(LanguageToolEvent.Type.CHECKING_FINISHED, caller, elapsedTime);
        }
    }
    return matches;
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) Language(org.languagetool.Language) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 64 with Language

use of org.languagetool.Language in project languagetool by languagetool-org.

the class RuleLinkTest method testBuildDeactivationLink.

@Test
public void testBuildDeactivationLink() {
    Language language = new English();
    RuleLink ruleLink = RuleLink.buildDeactivationLink(new WordRepeatRule(TestTools.getMessages(language.getShortCode()), language));
    assertEquals("WORD_REPEAT_RULE", ruleLink.getId());
    assertEquals("http://languagetool.org/deactivate/WORD_REPEAT_RULE", ruleLink.toString());
}
Also used : English(org.languagetool.language.English) Language(org.languagetool.Language) WordRepeatRule(org.languagetool.rules.WordRepeatRule) Test(org.junit.Test)

Example 65 with Language

use of org.languagetool.Language in project languagetool by languagetool-org.

the class LanguageIdentifierTest method langAssert.

private void langAssert(String expectedLangCode, String text) {
    Language expectedLang = expectedLangCode != null ? Languages.getLanguageForShortCode(expectedLangCode) : null;
    Language detectedLang = identifier.detectLanguage(text);
    if (!Objects.equals(expectedLang, detectedLang)) {
        fail("Got '" + detectedLang + "', expected '" + expectedLangCode + "' for '" + text + "'");
    }
}
Also used : Language(org.languagetool.Language)

Aggregations

Language (org.languagetool.Language)84 Test (org.junit.Test)23 File (java.io.File)15 ArrayList (java.util.ArrayList)12 JLanguageTool (org.languagetool.JLanguageTool)11 Rule (org.languagetool.rules.Rule)11 RuleMatch (org.languagetool.rules.RuleMatch)10 IOException (java.io.IOException)7 Ignore (org.junit.Ignore)6 StringTools.readerToString (org.languagetool.tools.StringTools.readerToString)5 InputStream (java.io.InputStream)4 English (org.languagetool.language.English)4 BitextRule (org.languagetool.rules.bitext.BitextRule)4 URL (java.net.URL)3 HashSet (java.util.HashSet)3 MultiThreadedJLanguageTool (org.languagetool.MultiThreadedJLanguageTool)3 AmericanEnglish (org.languagetool.language.AmericanEnglish)3 LanguageModel (org.languagetool.languagemodel.LanguageModel)3 LuceneLanguageModel (org.languagetool.languagemodel.LuceneLanguageModel)3 BufferedReader (java.io.BufferedReader)2