Search in sources :

Example 66 with Language

use of ai.elimu.model.v2.enums.Language in project webapp by elimu-ai.

the class CustomDispatcherServlet method initWebApplicationContext.

@Override
protected WebApplicationContext initWebApplicationContext() {
    logger.info("initWebApplicationContext");
    WebApplicationContext webApplicationContext = super.initWebApplicationContext();
    // Database migration
    logger.info("Performing database migration...");
    new DbMigrationHelper().performDatabaseMigration(webApplicationContext);
    if (EnvironmentContextLoaderListener.env == Environment.DEV) {
        // To ease development, pre-populate database with educational content extracted from the test server
        // Lookup the language of the educational content from the config file
        Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
        logger.info("language: " + language);
        // Import the educational content
        logger.info("Performing database content import...");
        new DbContentImportHelper().performDatabaseContentImport(Environment.TEST, language, webApplicationContext);
        createJpaSchemaExport();
    }
    return webApplicationContext;
}
Also used : DbContentImportHelper(ai.elimu.util.db.DbContentImportHelper) DbMigrationHelper(ai.elimu.util.db.DbMigrationHelper) Language(ai.elimu.model.v2.enums.Language) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 67 with Language

use of ai.elimu.model.v2.enums.Language in project webapp by elimu-ai.

the class WordCreationsPendingController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model) {
    logger.info("handleRequest");
    List<String> paragraphs = new ArrayList<>();
    for (StoryBookParagraph storyBookParagraph : storyBookParagraphDao.readAll()) {
        paragraphs.add(storyBookParagraph.getOriginalText());
    }
    logger.info("paragraphs.size(): " + paragraphs.size());
    Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
    Map<String, Integer> wordFrequencyMap = WordFrequencyHelper.getWordFrequency(paragraphs, language);
    model.addAttribute("wordFrequencyMap", wordFrequencyMap);
    logger.info("wordFrequencyMap.size(): " + wordFrequencyMap.size());
    // Remove Words that have already been added
    Iterator<String> wordTextIterator = wordFrequencyMap.keySet().iterator();
    while (wordTextIterator.hasNext()) {
        String wordText = wordTextIterator.next();
        Word existingWord = wordDao.readByText(wordText);
        if (existingWord != null) {
            wordTextIterator.remove();
        }
    }
    int maxUsageCount = 0;
    for (Integer usageCount : wordFrequencyMap.values()) {
        if (usageCount > maxUsageCount) {
            maxUsageCount = usageCount;
        }
    }
    model.addAttribute("maxUsageCount", maxUsageCount);
    return "content/word/pending";
}
Also used : Word(ai.elimu.model.content.Word) Language(ai.elimu.model.v2.enums.Language) ArrayList(java.util.ArrayList) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 68 with Language

use of ai.elimu.model.v2.enums.Language in project webapp by elimu-ai.

the class ParagraphWordScheduler method execute.

// Every hour
@Scheduled(cron = "00 00 * * * *")
public synchronized void execute() {
    logger.info("execute");
    Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
    List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll();
    logger.info("storyBookParagraphs.size(): " + storyBookParagraphs.size());
    for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
        List<String> wordsInOriginalText = WordExtractionHelper.getWords(storyBookParagraph.getOriginalText(), language);
        logger.info("wordsInOriginalText.size(): " + wordsInOriginalText.size());
        // Look for matches of existing Words in the paragraph's original text
        List<Word> words = new ArrayList<>();
        for (String wordInOriginalText : wordsInOriginalText) {
            logger.info("wordInOriginalText: \"" + wordInOriginalText + "\"");
            wordInOriginalText = wordInOriginalText.toLowerCase();
            logger.info("wordInOriginalText (lower-case): \"" + wordInOriginalText + "\"");
            Word word = wordDao.readByText(wordInOriginalText);
            logger.info("word: " + word);
            words.add(word);
        }
        logger.info("words.size(): " + words.size());
        storyBookParagraph.setWords(words);
        // Update the paragraph's list of Words in the database
        storyBookParagraphDao.update(storyBookParagraph);
    }
    // Refresh REST API cache
    storyBooksJsonService.refreshStoryBooksJSONArray();
    logger.info("execute complete");
}
Also used : Word(ai.elimu.model.content.Word) Language(ai.elimu.model.v2.enums.Language) ArrayList(java.util.ArrayList) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 69 with Language

use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.

the class LanguageCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    Optional<discord4j.core.object.entity.Guild> guild = message.getGuild().blockOptional();
    Optional<MessageChannel> channel = message.getChannel().blockOptional();
    if (guild.isPresent() && channel.isPresent())
        if (m.group(2) != null) {
            // Ajouts
            String channelName = ((GuildMessageChannel) channel.get()).getName();
            if (isUserHasEnoughRights(message)) {
                List<Language> langs = new ArrayList<>();
                for (Language lang : Language.values()) if (m.group(2).trim().toUpperCase().equals(lang.getAbrev()))
                    langs.add(lang);
                if (langs.size() == 1) {
                    if (m.group(1) == null) {
                        Guild.getGuild(guild.get()).setLanguage(langs.get(0));
                        final Language LG = langs.get(0);
                        message.getChannel().flatMap(chan -> chan.createMessage(guild.get().getName() + " " + Translator.getLabel(LG, "lang.request.1") + " " + langs.get(0))).subscribe();
                    } else {
                        ChannelLanguage chan = ChannelLanguage.getChannelLanguages().get(channel.get().getId().asLong());
                        if (chan != null) {
                            if (chan.getLang().equals(langs.get(0))) {
                                chan.removeToDatabase();
                                final Language LG = Translator.getLanguageFrom(channel.get());
                                message.getChannel().flatMap(salon -> salon.createMessage(channelName + " " + Translator.getLabel(LG, "lang.request.2") + " " + Guild.getGuild(guild.get()).getLanguage())).subscribe();
                            } else {
                                chan.setLanguage(langs.get(0));
                                final Language LG = langs.get(0);
                                message.getChannel().flatMap(salon -> salon.createMessage(channelName + " " + Translator.getLabel(LG, "lang.request.1") + " " + chan.getLang())).subscribe();
                            }
                        } else {
                            final Language LG = langs.get(0);
                            final ChannelLanguage CHAN = new ChannelLanguage(langs.get(0), channel.get().getId().asLong());
                            CHAN.addToDatabase();
                            message.getChannel().flatMap(salon -> salon.createMessage(channelName + " " + Translator.getLabel(LG, "lang.request.1") + " " + CHAN.getLang())).subscribe();
                        }
                    }
                } else if (langs.isEmpty())
                    notFoundLang.throwException(message, this, lg);
                else
                    tooMuchLangs.throwException(message, this, lg);
            } else
                BasicDiscordException.NO_ENOUGH_RIGHTS.throwException(message, this, lg);
        } else {
            // Consultation
            String text = "**" + guild.get().getName() + "** " + Translator.getLabel(lg, "lang.request.3") + " " + Guild.getGuild(guild.get()).getLanguage() + ".";
            ChannelLanguage chanLang = ChannelLanguage.getChannelLanguages().get(channel.get().getId().asLong());
            if (chanLang != null)
                text += "\nLe salon *" + ((GuildMessageChannel) channel.get()).getName() + "* " + Translator.getLabel(lg, "lang.request.3") + " " + chanLang.getLang() + ".";
            final String TEXT = text;
            message.getChannel().flatMap(salon -> salon.createMessage(TEXT)).subscribe();
        }
}
Also used : ChannelLanguage(data.ChannelLanguage) MessageChannel(discord4j.core.object.entity.channel.MessageChannel) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel) Language(enums.Language) ChannelLanguage(data.ChannelLanguage) ArrayList(java.util.ArrayList) List(java.util.List) Guild(data.Guild)

Example 70 with Language

use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.

the class AbstractCommand method request.

@Override
public final void request(MessageCreateEvent event, Message message) {
    Language lg = Translator.getLanguageFrom(message.getChannel().block());
    try {
        Matcher m = getMatcher(message);
        boolean isFound = m.find();
        // Caché si la fonction est désactivée/réservée aux admin et que l'auteur n'est pas super-admin
        if ((!isPublic() || isAdmin()) && message.getAuthor().map(user -> user.getId().asLong() != Constants.authorId).orElse(false))
            return;
        // S'il s'agit d'une demande d'aide...
        if (message.getContent().matches(Pattern.quote(getPrefix(message)) + getName() + "\\s+help")) {
            message.getChannel().flatMap(chan -> chan.createMessage(helpDetailed(lg, getPrefix(message)))).subscribe();
            return;
        }
        // La commande est trouvée
        if (isFound) {
            // Mais n'est pas utilisable en MP
            MessageChannel channel = message.getChannel().block();
            if (!isUsableInMP() && channel instanceof PrivateChannel) {
                BasicDiscordException.NOT_USABLE_IN_MP.throwException(message, this, lg);
                return;
            } else // Mais est désactivée par la guilde
            if (!(channel instanceof PrivateChannel) && message.getAuthor().map(user -> user.getId().asLong() != Constants.authorId).orElse(false) && isForbidden(Guild.getGuild(message.getGuild().block()))) {
                BasicDiscordException.COMMAND_FORBIDDEN.throwException(message, this, lg);
                return;
            }
        } else // Mais est mal utilisée
        if (message.getContent().startsWith(getPrefix(message) + getName())) {
            badUse.throwException(message, this, lg);
            return;
        }
        if (isFound)
            request(event, message, m, lg);
    } catch (Exception e) {
        BasicDiscordException.UNKNOWN_ERROR.throwException(message, this, lg);
        Reporter.report(e);
        LOG.error("request", e);
    }
}
Also used : Language(enums.Language) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) MessageChannel(discord4j.core.object.entity.channel.MessageChannel) Logger(org.slf4j.Logger) TextChannel(discord4j.core.object.entity.channel.TextChannel) LoggerFactory(org.slf4j.LoggerFactory) Permission(discord4j.rest.util.Permission) Constants(data.Constants) Snowflake(discord4j.common.util.Snowflake) Guild(data.Guild) PermissionSet(discord4j.rest.util.PermissionSet) Reporter(util.Reporter) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel) Matcher(java.util.regex.Matcher) BadUseCommandDiscordException(exceptions.BadUseCommandDiscordException) DiscordException(exceptions.DiscordException) PrivateChannel(discord4j.core.object.entity.channel.PrivateChannel) BasicDiscordException(exceptions.BasicDiscordException) Translator(util.Translator) Pattern(java.util.regex.Pattern) discord4j.core.object.entity(discord4j.core.object.entity) PrivateChannel(discord4j.core.object.entity.channel.PrivateChannel) Language(enums.Language) MessageChannel(discord4j.core.object.entity.channel.MessageChannel) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel) Matcher(java.util.regex.Matcher) BadUseCommandDiscordException(exceptions.BadUseCommandDiscordException) DiscordException(exceptions.DiscordException) BasicDiscordException(exceptions.BasicDiscordException)

Aggregations

Language (enums.Language)69 Matcher (java.util.regex.Matcher)43 ArrayList (java.util.ArrayList)29 AbstractCommand (commands.model.AbstractCommand)19 IOException (java.io.IOException)19 Translator (util.Translator)19 Message (discord4j.core.object.entity.Message)18 MessageCreateEvent (discord4j.core.event.domain.message.MessageCreateEvent)16 List (java.util.List)15 Language (ai.elimu.model.v2.enums.Language)12 Guild (data.Guild)12 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)10 Word (ai.elimu.model.content.Word)10 ChannelLanguage (data.ChannelLanguage)10 Command (commands.model.Command)9 Snowflake (discord4j.common.util.Snowflake)8 BasicDiscordException (exceptions.BasicDiscordException)8 ServerDofus (data.ServerDofus)7 exceptions (exceptions)7 Document (org.jsoup.nodes.Document)7