Search in sources :

Example 16 with Language

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;
}
Also used : Language(org.languagetool.Language) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with Language

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");
}
Also used : Language(org.languagetool.Language)

Example 18 with Language

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();
}
Also used : Language(org.languagetool.Language) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.Test)

Example 19 with Language

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 + ")");
}
Also used : Language(org.languagetool.Language)

Example 20 with Language

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;
}
Also used : Constructor(java.lang.reflect.Constructor) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Language(org.languagetool.Language) BitextRule(org.languagetool.rules.bitext.BitextRule)

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