use of org.languagetool.Language in project languagetool by languagetool-org.
the class V2TextChecker method getLanguage.
@Override
@NotNull
protected Language getLanguage(String text, Map<String, String> parameters, List<String> preferredVariants) {
Language lang;
String langParam = parameters.get("language");
if (getLanguageAutoDetect(parameters)) {
lang = detectLanguageOfString(text, null, preferredVariants);
} else {
lang = Languages.getLanguageForShortCode(langParam);
}
return lang;
}
use of org.languagetool.Language in project languagetool by languagetool-org.
the class HTTPServerMultiLangLoadTest method runTestsV2.
@Override
void runTestsV2() throws IOException, SAXException, ParserConfigurationException {
Language language = getRandomLanguage();
String text = langCodeToText.get(language);
int fromPos = random.nextInt(text.length());
int toPos = fromPos + random.nextInt(MAX_TEXT_LENGTH - MIN_TEXT_LENGTH) + MIN_TEXT_LENGTH;
String textSubstring = text.substring(fromPos, Math.min(toPos, text.length()));
long sleepTime = random.nextInt(MAX_SLEEP_MILLIS);
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
long startTime = System.currentTimeMillis();
counter.incrementAndGet();
checkByPOST(language, textSubstring);
System.out.println(counter.get() + ". Sleep: " + sleepTime + "ms, Lang: " + language.getShortCodeWithCountryAndVariant() + ", Length: " + textSubstring.length() + ", Time: " + (System.currentTimeMillis() - startTime) + "ms");
}
use of org.languagetool.Language in project languagetool by languagetool-org.
the class HTTPServerMultiLangLoadTest method testHTTPServer.
@Test
@Override
public void testHTTPServer() throws Exception {
File dir = new File(DATA_PATH);
List<Language> languages = new ArrayList<>();
//languages.add(new German());
languages.addAll(Languages.get());
for (Language language : languages) {
File file = new File(dir, "tatoeba-" + language.getShortCode() + ".txt");
if (!file.exists()) {
System.err.println("No data found for " + language + ", language will not be tested");
} else {
String content = StringTools.readerToString(new FileReader(file));
langCodeToText.put(language, content);
System.err.println("Using " + content.length() + " bytes of data for " + language);
}
}
if (langCodeToText.size() == 0) {
throw new RuntimeException("No input data found in " + dir);
}
System.out.println("Testing " + langCodeToText.keySet().size() + " languages and variants");
//super.testHTTPServer(); // start server in this JVM
// assume server has been started manually in its own JVM
super.doTest();
}
use of org.languagetool.Language in project languagetool by languagetool-org.
the class HTTPServerMultiLangLoadTest method getRandomLanguage.
protected Language getRandomLanguage() {
int randomNumber = random.nextInt(langCodeToText.size());
int i = 0;
for (Language lang : langCodeToText.keySet()) {
if (i++ == randomNumber) {
return lang;
}
}
throw new RuntimeException("Could not find a random language (" + i + ")");
}
use of org.languagetool.Language in project languagetool by languagetool-org.
the class Tools method getAllBuiltinBitextRules.
/**
* Use reflection to add bitext rules.
*/
private static List<BitextRule> getAllBuiltinBitextRules(Language language, ResourceBundle messages) {
List<BitextRule> rules = new ArrayList<>();
try {
List<Class<? extends BitextRule>> classes = BitextRule.getRelevantRules();
for (Class class1 : classes) {
Constructor[] constructors = class1.getConstructors();
boolean foundConstructor = false;
for (Constructor constructor : constructors) {
Class[] paramTypes = constructor.getParameterTypes();
if (paramTypes.length == 0) {
rules.add((BitextRule) constructor.newInstance());
foundConstructor = true;
break;
}
if (paramTypes.length == 1 && paramTypes[0].equals(ResourceBundle.class)) {
rules.add((BitextRule) constructor.newInstance(messages));
foundConstructor = true;
break;
}
if (paramTypes.length == 2 && paramTypes[0].equals(ResourceBundle.class) && paramTypes[1].equals(Language.class)) {
rules.add((BitextRule) constructor.newInstance(messages, language));
foundConstructor = true;
break;
}
}
if (!foundConstructor) {
throw new RuntimeException("Unknown constructor type for rule class " + class1.getName() + ", it supports only these constructors: " + Arrays.toString(constructors));
}
}
} catch (Exception e) {
throw new RuntimeException("Failed to load bitext rules", e);
}
return rules;
}
Aggregations