use of com.optimaize.langdetect.text.TextObject in project languagetool by languagetool-org.
the class LanguageIdentifier method detectLanguageCode.
/**
* @return language or {@code null} if language could not be identified
*/
@Nullable
private String detectLanguageCode(String text) {
TextObject textObject = textObjectFactory.forText(text);
Optional<LdLocale> lang = languageDetector.detect(textObject);
//System.out.println(languageDetector.getProbabilities(textObject));
if (lang.isPresent()) {
return lang.get().getLanguage();
} else {
return null;
}
}
use of com.optimaize.langdetect.text.TextObject in project languagetool by languagetool-org.
the class LanguageDetectionTrainer method main.
public static void main(String[] args) throws IOException {
if (args.length != 3) {
System.out.println("Usage: " + LanguageDetectionTrainer.class.getName() + " <languageCode> <plainTextFile> <minimalFrequency>");
System.exit(1);
}
String langCode = args[0];
String fileName = args[1];
int minimalFrequency = Integer.parseInt(args[2]);
String text = IOUtils.toString(new FileReader(fileName));
TextObjectFactory textObjectFactory = CommonTextObjectFactories.forIndexingCleanText();
TextObject inputText = textObjectFactory.create().append(text);
LanguageProfile languageProfile = new LanguageProfileBuilder(langCode).ngramExtractor(NgramExtractors.standard()).minimalFrequency(minimalFrequency).addText(inputText).build();
// current dir
File outputDir = new File(System.getProperty("user.dir"));
new LanguageProfileWriter().writeToDirectory(languageProfile, outputDir);
System.out.println("Language profile written to " + new File(outputDir, langCode).getAbsolutePath());
}
use of com.optimaize.langdetect.text.TextObject in project KaellyBot by Kaysoro.
the class Translator method getLanguageFrom.
public static Language getLanguageFrom(String source) {
TextObject textObject = CommonTextObjectFactories.forDetectingOnLargeText().forText(source);
Optional<LdLocale> lang = getLanguageDetector().detect(textObject);
if (lang.isPresent())
for (Language lg : Language.values()) if (lang.get().getLanguage().equals(lg.getAbrev().toLowerCase()))
return lg;
return null;
}
use of com.optimaize.langdetect.text.TextObject in project neo4j-nlp by graphaware.
the class LanguageManager method detectLanguage.
public String detectLanguage(String text) {
if (!initialized) {
initialize();
}
if (text != null) {
TextObject textObject = textObjectFactory.forText(text);
Optional<LdLocale> lang = languageDetector.detect(textObject);
if (lang.isPresent()) {
return lang.get().getLanguage();
}
}
return LANGUAGE_NA;
}
Aggregations