Search in sources :

Example 66 with Word

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

the class EmojiCsvExportController method handleRequest.

@RequestMapping(value = "/emojis.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) throws IOException {
    logger.info("handleRequest");
    List<Emoji> emojis = emojiDao.readAllOrdered();
    logger.info("emojis.size(): " + emojis.size());
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "glyph", "unicode_version", "unicode_emoji_version", "word_ids", "word_texts");
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    for (Emoji emoji : emojis) {
        logger.info("emoji.getGlyph(): \"" + emoji.getGlyph() + "\"");
        JSONArray wordIdsJsonArray = new JSONArray();
        int index = 0;
        for (Word word : emoji.getWords()) {
            wordIdsJsonArray.put(index, word.getId());
            index++;
        }
        JSONArray wordTextsJsonArray = new JSONArray();
        index = 0;
        for (Word word : emoji.getWords()) {
            wordTextsJsonArray.put(index, word.getText());
            index++;
        }
        csvPrinter.printRecord(emoji.getId(), emoji.getGlyph(), emoji.getUnicodeVersion(), emoji.getUnicodeEmojiVersion(), wordIdsJsonArray, wordTextsJsonArray);
        csvPrinter.flush();
    }
    String csvFileContent = stringWriter.toString();
    response.setContentType("text/csv");
    byte[] bytes = csvFileContent.getBytes();
    response.setContentLength(bytes.length);
    try {
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    } catch (IOException ex) {
        logger.error(ex);
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) Word(ai.elimu.model.content.Word) StringWriter(java.io.StringWriter) JSONArray(org.json.JSONArray) Emoji(ai.elimu.model.content.Emoji) CSVFormat(org.apache.commons.csv.CSVFormat) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 67 with Word

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

the class NumberCsvExportController method handleRequest.

@RequestMapping(value = "/numbers.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) throws IOException {
    logger.info("handleRequest");
    List<Number> numbers = numberDao.readAllOrdered();
    logger.info("numbers.size(): " + numbers.size());
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "value", "symbol", "word_ids", "word_texts");
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    for (Number number : numbers) {
        logger.info("number.getValue(): \"" + number.getValue() + "\"");
        JSONArray wordIdsJsonArray = new JSONArray();
        int index = 0;
        for (Word word : number.getWords()) {
            wordIdsJsonArray.put(index, word.getId());
            index++;
        }
        JSONArray wordTextsJsonArray = new JSONArray();
        index = 0;
        for (Word word : number.getWords()) {
            wordTextsJsonArray.put(index, word.getText());
            index++;
        }
        csvPrinter.printRecord(number.getId(), number.getValue(), number.getSymbol(), wordIdsJsonArray, wordTextsJsonArray);
        csvPrinter.flush();
    }
    String csvFileContent = stringWriter.toString();
    response.setContentType("text/csv");
    byte[] bytes = csvFileContent.getBytes();
    response.setContentLength(bytes.length);
    try {
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    } catch (IOException ex) {
        logger.error(ex);
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) Word(ai.elimu.model.content.Word) Number(ai.elimu.model.content.Number) StringWriter(java.io.StringWriter) JSONArray(org.json.JSONArray) CSVFormat(org.apache.commons.csv.CSVFormat) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 68 with Word

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

the class NumberEditController 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 69 with Word

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

the class StoryBookEditController 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 70 with Word

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

the class StoryBookEditController method handleSubmit.

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(@Valid StoryBook storyBook, BindingResult result, Model model, HttpServletRequest request, HttpSession session) {
    logger.info("handleSubmit");
    StoryBook existingStoryBook = storyBookDao.readByTitle(storyBook.getTitle());
    if ((existingStoryBook != null) && !existingStoryBook.getId().equals(storyBook.getId())) {
        result.rejectValue("title", "NonUnique");
    }
    if (result.hasErrors()) {
        model.addAttribute("storyBook", storyBook);
        model.addAttribute("timeStart", System.currentTimeMillis());
        model.addAttribute("contentLicenses", ContentLicense.values());
        List<Image> coverImages = imageDao.readAllOrdered();
        model.addAttribute("coverImages", coverImages);
        model.addAttribute("readingLevels", ReadingLevel.values());
        List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
        model.addAttribute("storyBookChapters", storyBookChapters);
        // Map<StoryBookChapter.id, List<StoryBookParagraph>>
        Map<Long, List<StoryBookParagraph>> paragraphsPerStoryBookChapterMap = new HashMap<>();
        for (StoryBookChapter storyBookChapter : storyBookChapters) {
            paragraphsPerStoryBookChapterMap.put(storyBookChapter.getId(), storyBookParagraphDao.readAll(storyBookChapter));
        }
        model.addAttribute("paragraphsPerStoryBookChapterMap", paragraphsPerStoryBookChapterMap);
        List<String> paragraphs = new ArrayList<>();
        for (StoryBookChapter storyBookChapter : storyBookChapters) {
            List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookChapter);
            for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
                paragraphs.add(storyBookParagraph.getOriginalText());
            }
        }
        model.addAttribute("storyBookContributionEvents", storyBookContributionEventDao.readAll(storyBook));
        model.addAttribute("storyBookPeerReviewEvents", storyBookPeerReviewEventDao.readAll(storyBook));
        Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
        Map<String, Integer> wordFrequencyMap = WordFrequencyHelper.getWordFrequency(paragraphs, language);
        model.addAttribute("wordFrequencyMap", wordFrequencyMap);
        Map<String, Word> wordMap = new HashMap<>();
        for (Word word : wordDao.readAllOrdered()) {
            wordMap.put(word.getText(), word);
        }
        model.addAttribute("wordMap", wordMap);
        model.addAttribute("emojisByWordId", getEmojisByWordId());
        Map<String, Integer> letterFrequencyMap = LetterFrequencyHelper.getLetterFrequency(paragraphs, language);
        model.addAttribute("letterFrequencyMap", letterFrequencyMap);
        Map<String, Letter> letterMap = new HashMap<>();
        for (Letter letter : letterDao.readAllOrdered()) {
            letterMap.put(letter.getText(), letter);
        }
        model.addAttribute("letterMap", letterMap);
        return "content/storybook/edit";
    } else {
        storyBook.setTimeLastUpdate(Calendar.getInstance());
        storyBook.setRevisionNumber(storyBook.getRevisionNumber() + 1);
        storyBookDao.update(storyBook);
        StoryBookContributionEvent storyBookContributionEvent = new StoryBookContributionEvent();
        storyBookContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
        storyBookContributionEvent.setTime(Calendar.getInstance());
        storyBookContributionEvent.setStoryBook(storyBook);
        storyBookContributionEvent.setRevisionNumber(storyBook.getRevisionNumber());
        storyBookContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 1000));
        storyBookContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
        storyBookContributionEvent.setPlatform(Platform.WEBAPP);
        storyBookContributionEventDao.create(storyBookContributionEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/storybook/edit/" + storyBook.getId();
        String embedThumbnailUrl = null;
        if (storyBook.getCoverImage() != null) {
            embedThumbnailUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/image/" + storyBook.getCoverImage().getId() + "_r" + storyBook.getCoverImage().getRevisionNumber() + "." + storyBook.getCoverImage().getImageFormat().toString().toLowerCase();
        }
        DiscordHelper.sendChannelMessage("Storybook edited: " + contentUrl, "\"" + storyBookContributionEvent.getStoryBook().getTitle() + "\"", "Comment: \"" + storyBookContributionEvent.getComment() + "\"", null, embedThumbnailUrl);
        // Refresh REST API cache
        storyBooksJsonService.refreshStoryBooksJSONArray();
        return "redirect:/content/storybook/list#" + storyBook.getId();
    }
}
Also used : StoryBookChapter(ai.elimu.model.content.StoryBookChapter) StoryBook(ai.elimu.model.content.StoryBook) Word(ai.elimu.model.content.Word) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) Image(ai.elimu.model.content.multimedia.Image) Letter(ai.elimu.model.content.Letter) StoryBookContributionEvent(ai.elimu.model.contributor.StoryBookContributionEvent) Language(ai.elimu.model.v2.enums.Language) ArrayList(java.util.ArrayList) List(java.util.List) 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