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;
}
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;
}
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;
}
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());
}
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 + "'");
}
}
Aggregations