Search in sources :

Example 71 with Word

use of ai.elimu.model.content.Word in project webapp by elimu-ai.

the class WordCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpServletRequest request, HttpSession session, @Valid Word word, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Word existingWord = wordDao.readByText(word.getText());
    if (existingWord != null) {
        result.rejectValue("text", "NonUnique");
    }
    if (StringUtils.containsAny(word.getText(), " ")) {
        result.rejectValue("text", "WordSpace");
    }
    if (result.hasErrors()) {
        model.addAttribute("word", word);
        model.addAttribute("timeStart", request.getParameter("timeStart"));
        // TODO: sort by letter(s) text
        model.addAttribute("letterSoundCorrespondences", letterSoundCorrespondenceDao.readAllOrderedByUsage());
        model.addAttribute("rootWords", wordDao.readAllOrdered());
        model.addAttribute("emojisByWordId", getEmojisByWordId());
        model.addAttribute("wordTypes", WordType.values());
        model.addAttribute("spellingConsistencies", SpellingConsistency.values());
        model.addAttribute("audio", audioDao.readByTranscription(word.getText()));
        return "content/word/create";
    } else {
        word.setTimeLastUpdate(Calendar.getInstance());
        wordDao.create(word);
        WordContributionEvent wordContributionEvent = new WordContributionEvent();
        wordContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
        wordContributionEvent.setTime(Calendar.getInstance());
        wordContributionEvent.setWord(word);
        wordContributionEvent.setRevisionNumber(word.getRevisionNumber());
        wordContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 1000));
        wordContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
        wordContributionEvent.setPlatform(Platform.WEBAPP);
        wordContributionEventDao.create(wordContributionEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/word/edit/" + word.getId();
        DiscordHelper.sendChannelMessage("Word created: " + contentUrl, "\"" + wordContributionEvent.getWord().getText() + "\"", "Comment: \"" + wordContributionEvent.getComment() + "\"", null, null);
        // Note: updating the list of Words in StoryBookParagraphs is handled by the ParagraphWordScheduler
        // Label Image with Word of matching title
        Image matchingImage = imageDao.read(word.getText());
        if (matchingImage != null) {
            Set<Word> labeledWords = matchingImage.getWords();
            if (!labeledWords.contains(word)) {
                labeledWords.add(word);
                matchingImage.setWords(labeledWords);
                imageDao.update(matchingImage);
            }
        }
        // Delete syllables that are actual words
        Syllable syllable = syllableDao.readByText(word.getText());
        if (syllable != null) {
            syllableDao.delete(syllable);
        }
        // Generate Audio for this Word (if it has not been done already)
        List<Audio> audios = audioDao.readAll(word);
        if (audios.isEmpty()) {
            Calendar timeStart = Calendar.getInstance();
            Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
            try {
                byte[] audioBytes = GoogleCloudTextToSpeechHelper.synthesizeText(word.getText(), language);
                logger.info("audioBytes: " + audioBytes);
                if (audioBytes != null) {
                    Audio audio = new Audio();
                    audio.setTimeLastUpdate(Calendar.getInstance());
                    audio.setContentType(AudioFormat.MP3.getContentType());
                    audio.setWord(word);
                    audio.setTitle("word-id-" + word.getId());
                    audio.setTranscription(word.getText());
                    audio.setBytes(audioBytes);
                    // TODO: Convert from byte[] to File, and extract audio duration
                    audio.setDurationMs(null);
                    audio.setAudioFormat(AudioFormat.MP3);
                    audioDao.create(audio);
                    audios.add(audio);
                    model.addAttribute("audios", audios);
                    AudioContributionEvent audioContributionEvent = new AudioContributionEvent();
                    audioContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
                    audioContributionEvent.setTime(Calendar.getInstance());
                    audioContributionEvent.setAudio(audio);
                    audioContributionEvent.setRevisionNumber(audio.getRevisionNumber());
                    audioContributionEvent.setComment("Google Cloud Text-to-Speech (🤖 auto-generated comment)️");
                    audioContributionEvent.setTimeSpentMs(System.currentTimeMillis() - timeStart.getTimeInMillis());
                    audioContributionEvent.setPlatform(Platform.WEBAPP);
                    audioContributionEventDao.create(audioContributionEvent);
                }
            } catch (Exception ex) {
                logger.error(ex);
            }
        }
        return "redirect:/content/word/list#" + word.getId();
    }
}
Also used : Word(ai.elimu.model.content.Word) Calendar(java.util.Calendar) Image(ai.elimu.model.content.multimedia.Image) Language(ai.elimu.model.v2.enums.Language) WordContributionEvent(ai.elimu.model.contributor.WordContributionEvent) AudioContributionEvent(ai.elimu.model.contributor.AudioContributionEvent) Audio(ai.elimu.model.content.multimedia.Audio) Syllable(ai.elimu.model.content.Syllable) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 72 with Word

use of ai.elimu.model.content.Word in project webapp by elimu-ai.

the class WordCreateController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model, @RequestParam(required = false) String autoFillText) {
    logger.info("handleRequest");
    Word word = new Word();
    // Pre-fill the Word's text (if the user arrived from /content/storybook/edit/{id}/)
    if (StringUtils.isNotBlank(autoFillText)) {
        word.setText(autoFillText);
        autoSelectLetterSoundCorrespondences(word);
        // TODO: display information message to the Contributor that the letter-sound correspondences were auto-selected, and that they should be verified
        model.addAttribute("audio", audioDao.readByTranscription(word.getText()));
    }
    model.addAttribute("word", word);
    model.addAttribute("timeStart", System.currentTimeMillis());
    // TODO: sort by letter(s) text
    model.addAttribute("letterSoundCorrespondences", letterSoundCorrespondenceDao.readAllOrderedByUsage());
    model.addAttribute("rootWords", wordDao.readAllOrdered());
    model.addAttribute("emojisByWordId", getEmojisByWordId());
    model.addAttribute("wordTypes", WordType.values());
    model.addAttribute("spellingConsistencies", SpellingConsistency.values());
    return "content/word/create";
}
Also used : Word(ai.elimu.model.content.Word) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 73 with Word

use of ai.elimu.model.content.Word in project webapp by elimu-ai.

the class WordListController method getEmojisByWordId.

private Map<Long, String> getEmojisByWordId() {
    logger.info("getEmojisByWordId");
    Map<Long, String> emojisByWordId = new HashMap<>();
    for (Word word : wordDao.readAll()) {
        String emojiGlyphs = "";
        List<Emoji> emojis = emojiDao.readAllLabeled(word);
        for (Emoji emoji : emojis) {
            emojiGlyphs += emoji.getGlyph();
        }
        if (StringUtils.isNotBlank(emojiGlyphs)) {
            emojisByWordId.put(word.getId(), emojiGlyphs);
        }
    }
    return emojisByWordId;
}
Also used : Word(ai.elimu.model.content.Word) HashMap(java.util.HashMap) Emoji(ai.elimu.model.content.Emoji)

Example 74 with Word

use of ai.elimu.model.content.Word in project webapp by elimu-ai.

the class WordListController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model) {
    logger.info("handleRequest");
    List<Word> words = wordDao.readAllOrderedByUsage();
    model.addAttribute("words", words);
    model.addAttribute("emojisByWordId", getEmojisByWordId());
    int maxUsageCount = 0;
    for (Word word : words) {
        if (word.getUsageCount() > maxUsageCount) {
            maxUsageCount = word.getUsageCount();
        }
    }
    model.addAttribute("maxUsageCount", maxUsageCount);
    return "content/word/list";
}
Also used : Word(ai.elimu.model.content.Word) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 75 with Word

use of ai.elimu.model.content.Word in project webapp by elimu-ai.

the class WordEditController method handleSubmit.

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(HttpServletRequest request, HttpSession session, @Valid Word word, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Word existingWord = wordDao.readByText(word.getText());
    if ((existingWord != null) && !existingWord.getId().equals(word.getId())) {
        result.rejectValue("text", "NonUnique");
    }
    if (StringUtils.containsAny(word.getText(), " ")) {
        result.rejectValue("text", "WordSpace");
    }
    if (result.hasErrors()) {
        model.addAttribute("word", word);
        model.addAttribute("timeStart", request.getParameter("timeStart"));
        // TODO: sort by letter(s) text
        model.addAttribute("letterSoundCorrespondences", letterSoundCorrespondenceDao.readAllOrderedByUsage());
        model.addAttribute("rootWords", wordDao.readAllOrdered());
        model.addAttribute("emojisByWordId", getEmojisByWordId());
        model.addAttribute("wordTypes", WordType.values());
        model.addAttribute("spellingConsistencies", SpellingConsistency.values());
        model.addAttribute("wordContributionEvents", wordContributionEventDao.readAll(word));
        model.addAttribute("wordPeerReviewEvents", wordPeerReviewEventDao.readAll(word));
        model.addAttribute("audios", audioDao.readAll(word));
        // Look up variants of the same wordByTextMatch
        model.addAttribute("wordInflections", wordDao.readInflections(word));
        // Look up Multimedia content that has been labeled with this Word
        // TODO: labeled Audios
        List<Emoji> labeledEmojis = emojiDao.readAllLabeled(word);
        model.addAttribute("labeledEmojis", labeledEmojis);
        List<Image> labeledImages = imageDao.readAllLabeled(word);
        model.addAttribute("labeledImages", labeledImages);
        return "content/word/edit";
    } else {
        word.setTimeLastUpdate(Calendar.getInstance());
        word.setRevisionNumber(word.getRevisionNumber() + 1);
        wordDao.update(word);
        WordContributionEvent wordContributionEvent = new WordContributionEvent();
        wordContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
        wordContributionEvent.setTime(Calendar.getInstance());
        wordContributionEvent.setWord(word);
        wordContributionEvent.setRevisionNumber(word.getRevisionNumber());
        wordContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 1000));
        wordContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
        wordContributionEvent.setPlatform(Platform.WEBAPP);
        wordContributionEventDao.create(wordContributionEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/word/edit/" + word.getId();
        DiscordHelper.sendChannelMessage("Word edited: " + contentUrl, "\"" + word.getText() + "\"", "Comment: \"" + wordContributionEvent.getComment() + "\"", null, null);
        // Note: updating the list of Words in StoryBookParagraphs is handled by the ParagraphWordScheduler
        // Delete syllables that are actual words
        Syllable syllable = syllableDao.readByText(word.getText());
        if (syllable != null) {
            syllableDao.delete(syllable);
        }
        return "redirect:/content/word/list#" + word.getId();
    }
}
Also used : Word(ai.elimu.model.content.Word) Emoji(ai.elimu.model.content.Emoji) WordContributionEvent(ai.elimu.model.contributor.WordContributionEvent) Image(ai.elimu.model.content.multimedia.Image) Syllable(ai.elimu.model.content.Syllable) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Word (ai.elimu.model.content.Word)88 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)40 HashMap (java.util.HashMap)23 Emoji (ai.elimu.model.content.Emoji)22 ArrayList (java.util.ArrayList)20 Number (ai.elimu.model.content.Number)17 Letter (ai.elimu.model.content.Letter)15 Test (org.junit.Test)12 Image (ai.elimu.model.content.multimedia.Image)11 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)9 JSONArray (org.json.JSONArray)9 JSONObject (org.json.JSONObject)9 Contributor (ai.elimu.model.Contributor)8 Language (ai.elimu.model.v2.enums.Language)8 IOException (java.io.IOException)8 LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)7 Audio (ai.elimu.model.content.multimedia.Audio)7 Contributor (ai.elimu.model.contributor.Contributor)7 WordContributionEvent (ai.elimu.model.contributor.WordContributionEvent)7 WordGson (ai.elimu.model.v2.gson.content.WordGson)7