Search in sources :

Example 41 with Language

use of enums.Language in project KaellyBot by Kaysoro.

the class Translator method detectLanguage.

public static Language detectLanguage(IChannel channel) {
    Language result = Constants.defaultLanguage;
    Map<Language, Integer> languages = new HashMap<>();
    for (Language lang : Language.values()) languages.put(lang, 0);
    languages.put(null, 0);
    List<String> sources = getReformatedMessages(channel);
    for (String source : sources) {
        Language lg = getLanguageFrom(source);
        languages.put(lg, languages.get(lg) + 1);
    }
    Map.Entry<Language, Integer> better = null;
    for (Map.Entry<Language, Integer> chosenLanguage : languages.entrySet()) {
        if (better == null)
            better = chosenLanguage;
        if (better.getValue() < chosenLanguage.getValue())
            better = chosenLanguage;
    }
    try {
        if (better.getKey() != null)
            return better.getKey();
    } catch (NullPointerException e) {
        LoggerFactory.getLogger(Translator.class).warn("Translator.detectLanguage", e);
    }
    return result;
}
Also used : Language(enums.Language) ChannelLanguage(data.ChannelLanguage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 42 with Language

use of enums.Language in project KaellyBot by Kaysoro.

the class Translator method getLanguageFrom.

public static Language getLanguageFrom(IChannel channel) {
    Language result = Constants.defaultLanguage;
    if (!channel.isPrivate()) {
        Guild guild = Guild.getGuild(channel.getGuild());
        result = guild.getLanguage();
        ChannelLanguage channelLanguage = ChannelLanguage.getChannelLanguages().get(channel.getLongID());
        if (channelLanguage != null)
            result = channelLanguage.getLang();
    }
    return result;
}
Also used : ChannelLanguage(data.ChannelLanguage) Language(enums.Language) ChannelLanguage(data.ChannelLanguage) Guild(data.Guild)

Example 43 with Language

use of enums.Language 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;
}
Also used : LdLocale(com.optimaize.langdetect.i18n.LdLocale) TextObject(com.optimaize.langdetect.text.TextObject) Language(enums.Language) ChannelLanguage(data.ChannelLanguage)

Aggregations

Language (enums.Language)43 Matcher (java.util.regex.Matcher)27 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 ChannelLanguage (data.ChannelLanguage)6 AbstractCommand (commands.model.AbstractCommand)4 Command (commands.model.Command)4 Guild (data.Guild)4 ServerDofus (data.ServerDofus)4 BasicDiscordException (exceptions.BasicDiscordException)3 DiscordException (exceptions.DiscordException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SQLException (java.sql.SQLException)3 List (java.util.List)3 Document (org.jsoup.nodes.Document)3 Element (org.jsoup.nodes.Element)3 Elements (org.jsoup.select.Elements)3 Almanax (data.Almanax)2 Embedded (data.Embedded)2 BadUseCommandDiscordException (exceptions.BadUseCommandDiscordException)2