Search in sources :

Example 1 with StoryBookChapter

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

the class LetterUsageCountScheduler method execute.

// At 06:15 every day
@Scheduled(cron = "00 15 06 * * *")
public synchronized void execute() {
    logger.info("execute");
    logger.info("Calculating usage count for Letters");
    Map<String, Integer> letterFrequencyMap = new HashMap<>();
    Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
    List<StoryBook> storyBooks = storyBookDao.readAllOrdered();
    logger.info("storyBooks.size(): " + storyBooks.size());
    for (StoryBook storyBook : storyBooks) {
        logger.info("storyBook.getTitle(): " + storyBook.getTitle());
        List<String> paragraphs = new ArrayList<>();
        List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
        for (StoryBookChapter storyBookChapter : storyBookChapters) {
            List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookChapter);
            for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
                paragraphs.add(storyBookParagraph.getOriginalText());
            }
        }
        Map<String, Integer> letterFrequencyMapForBook = LetterFrequencyHelper.getLetterFrequency(paragraphs, language);
        letterFrequencyMapForBook.keySet().forEach(letterText -> letterFrequencyMap.put(letterText, letterFrequencyMap.getOrDefault(letterText, 0) + letterFrequencyMapForBook.get(letterText)));
    }
    logger.info("letterFrequencyMap: " + letterFrequencyMap);
    for (String letterText : letterFrequencyMap.keySet()) {
        Letter existingLetter = letterDao.readByText(letterText);
        if (existingLetter != null) {
            existingLetter.setUsageCount(letterFrequencyMap.get(letterText));
            letterDao.update(existingLetter);
        }
    }
    logger.info("execute complete");
}
Also used : StoryBookChapter(ai.elimu.model.content.StoryBookChapter) StoryBook(ai.elimu.model.content.StoryBook) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) Letter(ai.elimu.model.content.Letter) Language(ai.elimu.model.v2.enums.Language) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 2 with StoryBookChapter

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

the class WordUsageCountScheduler method execute.

// At 06:00 every day
@Scheduled(cron = "00 00 06 * * *")
public synchronized void execute() {
    logger.info("execute");
    logger.info("Calculating usage count for Words");
    Map<String, Integer> wordFrequencyMap = new HashMap<>();
    Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
    List<StoryBook> storyBooks = storyBookDao.readAllOrdered();
    logger.info("storyBooks.size(): " + storyBooks.size());
    for (StoryBook storyBook : storyBooks) {
        logger.info("storyBook.getTitle(): " + storyBook.getTitle());
        List<String> paragraphs = new ArrayList<>();
        List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
        for (StoryBookChapter storyBookChapter : storyBookChapters) {
            List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookChapter);
            for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
                paragraphs.add(storyBookParagraph.getOriginalText());
            }
        }
        Map<String, Integer> wordFrequencyMapForBook = WordFrequencyHelper.getWordFrequency(paragraphs, language);
        wordFrequencyMapForBook.keySet().forEach(word -> wordFrequencyMap.put(word, wordFrequencyMap.getOrDefault(word, 0) + wordFrequencyMapForBook.get(word)));
    }
    for (String key : wordFrequencyMap.keySet()) {
        String wordLowerCase = key.toLowerCase();
        logger.info("wordLowerCase: \"" + wordLowerCase + "\"");
        Word word = wordDao.readByText(wordLowerCase);
        if (word != null) {
            word.setUsageCount(wordFrequencyMap.get(wordLowerCase));
            wordDao.update(word);
        }
    }
    logger.info("execute complete");
}
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) Language(ai.elimu.model.v2.enums.Language) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 3 with StoryBookChapter

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

the class StringToStoryBookChapterConverter method convert.

/**
 * Convert StoryBookChapter id to StoryBookChapter entity
 */
public StoryBookChapter convert(String id) {
    if (StringUtils.isBlank(id)) {
        return null;
    } else {
        Long storyBookChapterId = Long.parseLong(id);
        StoryBookChapter storyBookChapter = storyBookChapterDao.read(storyBookChapterId);
        return storyBookChapter;
    }
}
Also used : StoryBookChapter(ai.elimu.model.content.StoryBookChapter)

Example 4 with StoryBookChapter

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

the class StoryBookEditController method handleRequest.

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(Model model, @PathVariable Long id) {
    logger.info("handleRequest");
    StoryBook storyBook = storyBookDao.read(id);
    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";
}
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) Language(ai.elimu.model.v2.enums.Language) ArrayList(java.util.ArrayList) List(java.util.List) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with StoryBookChapter

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

the class StoryBookCsvExportController method handleRequest.

@RequestMapping(value = "/storybooks.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) throws IOException {
    logger.info("handleRequest");
    List<StoryBook> storyBooks = storyBookDao.readAllOrdered();
    logger.info("storyBooks.size(): " + storyBooks.size());
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "title", "description", "content_license", "attribution_url", "reading_level", "cover_image_id", "chapters");
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    for (StoryBook storyBook : storyBooks) {
        logger.info("storyBook.getTitle(): \"" + storyBook.getTitle() + "\"");
        Long coverImageId = null;
        if (storyBook.getCoverImage() != null) {
            coverImageId = storyBook.getCoverImage().getId();
        }
        // Store chapters as JSON objects
        JSONArray chaptersJsonArray = new JSONArray();
        List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
        logger.info("storyBookChapters.size(): " + storyBookChapters.size());
        for (StoryBookChapter storyBookChapter : storyBookChapters) {
            logger.info("storyBookChapter.getId(): " + storyBookChapter.getId());
            StoryBookChapterGson storyBookChapterGson = JpaToGsonConverter.getStoryBookChapterGson(storyBookChapter);
            // TODO: move this code block to JpaToGsonConverter?
            if (storyBookChapterGson.getImage() != null) {
                ImageGson imageGsonWithIdOnly = new ImageGson();
                imageGsonWithIdOnly.setId(storyBookChapterGson.getImage().getId());
                storyBookChapterGson.setImage(imageGsonWithIdOnly);
            }
            // Store paragraphs as JSON objects
            List<StoryBookParagraphGson> storyBookParagraphs = new ArrayList<>();
            logger.info("storyBookParagraphs.size(): " + storyBookParagraphs.size());
            for (StoryBookParagraph storyBookParagraph : storyBookParagraphDao.readAll(storyBookChapter)) {
                logger.info("storyBookParagraph.getId(): " + storyBookParagraph.getId());
                StoryBookParagraphGson storyBookParagraphGson = JpaToGsonConverter.getStoryBookParagraphGson(storyBookParagraph);
                storyBookParagraphGson.setWords(null);
                storyBookParagraphs.add(storyBookParagraphGson);
            }
            storyBookChapterGson.setStoryBookParagraphs(storyBookParagraphs);
            String json = new Gson().toJson(storyBookChapterGson);
            JSONObject jsonObject = new JSONObject(json);
            logger.info("jsonObject: " + jsonObject);
            chaptersJsonArray.put(jsonObject);
        }
        logger.info("chaptersJsonArray: " + chaptersJsonArray);
        csvPrinter.printRecord(storyBook.getId(), storyBook.getTitle(), storyBook.getDescription(), storyBook.getContentLicense(), storyBook.getAttributionUrl(), storyBook.getReadingLevel(), coverImageId, chaptersJsonArray);
        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 : StoryBookChapter(ai.elimu.model.content.StoryBookChapter) StoryBook(ai.elimu.model.content.StoryBook) StoryBookParagraphGson(ai.elimu.model.v2.gson.content.StoryBookParagraphGson) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) ImageGson(ai.elimu.model.v2.gson.content.ImageGson) Gson(com.google.gson.Gson) StoryBookParagraphGson(ai.elimu.model.v2.gson.content.StoryBookParagraphGson) StoryBookChapterGson(ai.elimu.model.v2.gson.content.StoryBookChapterGson) IOException(java.io.IOException) CSVPrinter(org.apache.commons.csv.CSVPrinter) ImageGson(ai.elimu.model.v2.gson.content.ImageGson) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) CSVFormat(org.apache.commons.csv.CSVFormat) StoryBookChapterGson(ai.elimu.model.v2.gson.content.StoryBookChapterGson) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

StoryBookChapter (ai.elimu.model.content.StoryBookChapter)13 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)11 StoryBook (ai.elimu.model.content.StoryBook)10 ArrayList (java.util.ArrayList)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 Word (ai.elimu.model.content.Word)5 Image (ai.elimu.model.content.multimedia.Image)5 Language (ai.elimu.model.v2.enums.Language)5 HashMap (java.util.HashMap)5 Letter (ai.elimu.model.content.Letter)4 StoryBookContributionEvent (ai.elimu.model.contributor.StoryBookContributionEvent)4 StoryBookChapterGson (ai.elimu.model.v2.gson.content.StoryBookChapterGson)3 StoryBookParagraphGson (ai.elimu.model.v2.gson.content.StoryBookParagraphGson)3 Scheduled (org.springframework.scheduling.annotation.Scheduled)3 Contributor (ai.elimu.model.contributor.Contributor)2 StoryBookGson (ai.elimu.model.v2.gson.content.StoryBookGson)2 Gson (com.google.gson.Gson)2 File (java.io.File)2 URL (java.net.URL)2 List (java.util.List)2