Search in sources :

Example 16 with StoryBookParagraph

use of ai.elimu.model.content.StoryBookParagraph 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 17 with StoryBookParagraph

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

the class StoryBooksJsonService method getStoryBooksJSONArray.

@Cacheable("storyBooks")
public JSONArray getStoryBooksJSONArray() {
    logger.info("getStoryBooksJSONArray");
    Date dateStart = new Date();
    JSONArray storyBooksJsonArray = new JSONArray();
    for (StoryBook storyBook : storyBookDao.readAllOrdered()) {
        StoryBookGson storyBookGson = JpaToGsonConverter.getStoryBookGson(storyBook);
        // Add chapters
        List<StoryBookChapterGson> storyBookChapterGsons = new ArrayList<>();
        for (StoryBookChapter storyBookChapter : storyBookChapterDao.readAll(storyBook)) {
            StoryBookChapterGson storyBookChapterGson = JpaToGsonConverter.getStoryBookChapterGson(storyBookChapter);
            // Add paragraphs
            List<StoryBookParagraphGson> storyBookParagraphGsons = new ArrayList<>();
            for (StoryBookParagraph storyBookParagraph : storyBookParagraphDao.readAll(storyBookChapter)) {
                StoryBookParagraphGson storyBookParagraphGson = JpaToGsonConverter.getStoryBookParagraphGson(storyBookParagraph);
                storyBookParagraphGsons.add(storyBookParagraphGson);
            }
            storyBookChapterGson.setStoryBookParagraphs(storyBookParagraphGsons);
            storyBookChapterGsons.add(storyBookChapterGson);
        }
        storyBookGson.setStoryBookChapters(storyBookChapterGsons);
        String json = new Gson().toJson(storyBookGson);
        storyBooksJsonArray.put(new JSONObject(json));
    }
    Date dateEnd = new Date();
    logger.info("getStoryBooksJSONArray duration: " + (dateEnd.getTime() - dateStart.getTime()) + " ms");
    return storyBooksJsonArray;
}
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) StoryBookGson(ai.elimu.model.v2.gson.content.StoryBookGson) ArrayList(java.util.ArrayList) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) StoryBookGson(ai.elimu.model.v2.gson.content.StoryBookGson) StoryBookParagraphGson(ai.elimu.model.v2.gson.content.StoryBookParagraphGson) Gson(com.google.gson.Gson) StoryBookChapterGson(ai.elimu.model.v2.gson.content.StoryBookChapterGson) Date(java.util.Date) JSONObject(org.json.JSONObject) StoryBookChapterGson(ai.elimu.model.v2.gson.content.StoryBookChapterGson) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 18 with StoryBookParagraph

use of ai.elimu.model.content.StoryBookParagraph 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 19 with StoryBookParagraph

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

the class StringToStoryBookParagraphConverter method convert.

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

Aggregations

StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)19 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 StoryBook (ai.elimu.model.content.StoryBook)11 StoryBookChapter (ai.elimu.model.content.StoryBookChapter)11 ArrayList (java.util.ArrayList)11 Word (ai.elimu.model.content.Word)9 Language (ai.elimu.model.v2.enums.Language)9 StoryBookContributionEvent (ai.elimu.model.contributor.StoryBookContributionEvent)6 Audio (ai.elimu.model.content.multimedia.Audio)5 Image (ai.elimu.model.content.multimedia.Image)5 HashMap (java.util.HashMap)5 Letter (ai.elimu.model.content.Letter)4 Contributor (ai.elimu.model.contributor.Contributor)4 Scheduled (org.springframework.scheduling.annotation.Scheduled)4 StoryBookChapterGson (ai.elimu.model.v2.gson.content.StoryBookChapterGson)3 StoryBookParagraphGson (ai.elimu.model.v2.gson.content.StoryBookParagraphGson)3 Emoji (ai.elimu.model.content.Emoji)2 AudioContributionEvent (ai.elimu.model.contributor.AudioContributionEvent)2 StoryBookGson (ai.elimu.model.v2.gson.content.StoryBookGson)2 Gson (com.google.gson.Gson)2