Search in sources :

Example 1 with StoryBook

use of ai.elimu.model.content.StoryBook 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");
    for (Locale locale : Locale.values()) {
        logger.info("Calculating usage count for Letters with locale " + locale);
        Map<String, Integer> letterFrequencyMap = new HashMap<>();
        List<StoryBook> storyBooks = storyBookDao.readAllOrdered(locale);
        logger.info("storyBooks.size(): " + storyBooks.size());
        for (StoryBook storyBook : storyBooks) {
            logger.info("storyBook.getTitle(): " + storyBook.getTitle());
            Map<String, Integer> letterFrequencyMapForBook = LetterFrequencyHelper.getLetterFrequency(storyBook);
            for (String key : letterFrequencyMapForBook.keySet()) {
                String letterText = key;
                int letterFrequency = letterFrequencyMapForBook.get(key);
                if (!letterFrequencyMap.containsKey(letterText)) {
                    letterFrequencyMap.put(letterText, letterFrequency);
                } else {
                    letterFrequencyMap.put(letterText, letterFrequencyMap.get(letterText) + letterFrequency);
                }
            }
        }
        logger.info("letterFrequencyMap: " + letterFrequencyMap);
        for (String key : letterFrequencyMap.keySet()) {
            String letterText = key;
            Letter existingLetter = letterDao.readByText(locale, letterText);
            if (existingLetter != null) {
                existingLetter.setUsageCount(letterFrequencyMap.get(letterText));
                letterDao.update(existingLetter);
            }
        }
    }
    logger.info("execute complete");
}
Also used : Locale(ai.elimu.model.enums.Locale) Letter(ai.elimu.model.content.Letter) StoryBook(ai.elimu.model.content.StoryBook) HashMap(java.util.HashMap) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 2 with StoryBook

use of ai.elimu.model.content.StoryBook 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");
    for (Locale locale : Locale.values()) {
        logger.info("Calculating usage count for Words with locale " + locale);
        Map<String, Integer> wordFrequencyMap = new HashMap<>();
        List<StoryBook> storyBooks = storyBookDao.readAllOrdered(locale);
        logger.info("storyBooks.size(): " + storyBooks.size());
        for (StoryBook storyBook : storyBooks) {
            logger.info("storyBook.getTitle(): " + storyBook.getTitle());
            Map<String, Integer> wordFrequencyMapForBook = WordFrequencyHelper.getWordFrequency(storyBook);
            for (String key : wordFrequencyMapForBook.keySet()) {
                int wordFrequency = wordFrequencyMapForBook.get(key);
                String wordLowerCase = key.toLowerCase();
                if (!wordFrequencyMap.containsKey(wordLowerCase)) {
                    wordFrequencyMap.put(wordLowerCase, wordFrequency);
                } else {
                    wordFrequencyMap.put(wordLowerCase, wordFrequencyMap.get(wordLowerCase) + wordFrequency);
                }
            }
        }
        for (String key : wordFrequencyMap.keySet()) {
            String wordLowerCase = key.toLowerCase();
            Word word = wordDao.readByText(locale, wordLowerCase);
            if (word != null) {
                word.setUsageCount(wordFrequencyMap.get(wordLowerCase));
                wordDao.update(word);
            }
        }
    }
    logger.info("execute complete");
}
Also used : Locale(ai.elimu.model.enums.Locale) StoryBook(ai.elimu.model.content.StoryBook) Word(ai.elimu.model.content.Word) HashMap(java.util.HashMap) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 3 with StoryBook

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

the class StoryBookCreateController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model, HttpSession session) {
    logger.info("handleRequest");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    StoryBook storyBook = new StoryBook();
    model.addAttribute("storyBook", storyBook);
    model.addAttribute("contentLicenses", ContentLicense.values());
    List<Image> coverImages = imageDao.readAllOrdered(contributor.getLocale());
    model.addAttribute("coverImages", coverImages);
    model.addAttribute("gradeLevels", GradeLevel.values());
    return "content/storybook/create";
}
Also used : StoryBook(ai.elimu.model.content.StoryBook) Contributor(ai.elimu.model.Contributor) Image(ai.elimu.model.content.multimedia.Image) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with StoryBook

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

the class StoryBookCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpSession session, @Valid StoryBook storyBook, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    StoryBook existingStoryBook = storybookDao.readByTitle(storyBook.getLocale(), storyBook.getTitle());
    if (existingStoryBook != null) {
        result.rejectValue("title", "NonUnique");
    }
    List<String> paragraphs = storyBook.getParagraphs();
    logger.info("paragraphs: " + paragraphs);
    if (result.hasErrors()) {
        model.addAttribute("storybook", storyBook);
        model.addAttribute("contentLicenses", ContentLicense.values());
        List<Image> coverImages = imageDao.readAllOrdered(contributor.getLocale());
        model.addAttribute("coverImages", coverImages);
        model.addAttribute("gradeLevels", GradeLevel.values());
        return "content/storybook/create";
    } else {
        storyBook.setTimeLastUpdate(Calendar.getInstance());
        storybookDao.create(storyBook);
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String text = URLEncoder.encode(contributor.getFirstName() + " just added a new StoryBook:\n" + "• Language: \"" + storyBook.getLocale().getLanguage() + "\"\n" + "• Title: \"" + storyBook.getTitle() + "\"\n" + "• Grade level: " + storyBook.getGradeLevel() + "\n" + "• Paragraphs: " + storyBook.getParagraphs() + "\n" + "See ") + "http://elimu.ai/content/storybook/edit/" + storyBook.getId();
            String iconUrl = contributor.getImageUrl();
            SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.CONTENT_CREATION), text, iconUrl, null);
        }
        return "redirect:/content/storybook/list#" + storyBook.getId();
    }
}
Also used : StoryBook(ai.elimu.model.content.StoryBook) Contributor(ai.elimu.model.Contributor) Image(ai.elimu.model.content.multimedia.Image) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with StoryBook

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

the class LetterFrequencyHelperTest method testGetLetterFrequency.

@Test
public void testGetLetterFrequency() {
    StoryBook storyBook = new StoryBook();
    List<String> paragraphs = new ArrayList<>();
    paragraphs.add("\"Mom,\" called Lebo. \"Come and look.\"");
    storyBook.setParagraphs(paragraphs);
    Map<String, Integer> letterFrequencyMap = LetterFrequencyHelper.getLetterFrequency(storyBook);
    assertThat(letterFrequencyMap.get("o"), is(5));
    assertThat(letterFrequencyMap.get("e"), is(3));
    assertThat(letterFrequencyMap.get("l"), is(3));
    assertThat(letterFrequencyMap.get("a"), is(2));
    assertThat(letterFrequencyMap.get("d"), is(2));
    assertThat(letterFrequencyMap.get("m"), is(2));
    assertThat(letterFrequencyMap.get("b"), is(1));
    assertThat(letterFrequencyMap.get("c"), is(1));
    assertThat(letterFrequencyMap.get("n"), is(1));
    assertThat(letterFrequencyMap.get("C"), is(1));
    assertThat(letterFrequencyMap.get("L"), is(1));
    assertThat(letterFrequencyMap.get("M"), is(1));
}
Also used : StoryBook(ai.elimu.model.content.StoryBook) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

StoryBook (ai.elimu.model.content.StoryBook)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 Contributor (ai.elimu.model.Contributor)5 Image (ai.elimu.model.content.multimedia.Image)5 ArrayList (java.util.ArrayList)4 Locale (ai.elimu.model.enums.Locale)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 Scheduled (org.springframework.scheduling.annotation.Scheduled)3 Word (ai.elimu.model.content.Word)2 Letter (ai.elimu.model.content.Letter)1 Syllable (ai.elimu.model.content.Syllable)1 StoryBookGson (ai.elimu.model.gson.content.StoryBookGson)1 Gson (com.google.gson.Gson)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1