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");
}
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");
}
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";
}
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();
}
}
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));
}
Aggregations