use of ai.elimu.model.content.StoryBook in project webapp by elimu-ai.
the class StoryBookRestController method list.
@RequestMapping("/list")
public String list(HttpServletRequest request, @RequestParam String deviceId, // TODO: checksum,
@RequestParam Locale locale) {
logger.info("list");
logger.info("request.getQueryString(): " + request.getQueryString());
JSONArray storyBooks = new JSONArray();
for (StoryBook storyBook : storyBookDao.readAllOrdered(locale)) {
StoryBookGson storyBookGson = JavaToGsonConverter.getStoryBookGson(storyBook);
String json = new Gson().toJson(storyBookGson);
storyBooks.put(new JSONObject(json));
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("result", "success");
jsonObject.put("storyBooks", storyBooks);
logger.info("jsonObject: " + jsonObject);
return jsonObject.toString();
}
use of ai.elimu.model.content.StoryBook in project webapp by elimu-ai.
the class SyllableUsageCountScheduler method execute.
// At 07:30 every morning
@Scheduled(cron = "00 30 07 * * *")
public synchronized void execute() {
logger.info("execute");
for (Locale locale : Locale.values()) {
logger.info("Calculating usage count for Syllables with locale " + locale);
Map<String, Integer> syllableFrequencyMap = 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> syllableFrequencyMapForBook = SyllableFrequencyHelper.getSyllableFrequency(storyBook);
for (String key : syllableFrequencyMapForBook.keySet()) {
String syllableText = key;
int syllableFrequency = syllableFrequencyMapForBook.get(key);
if (!syllableFrequencyMap.containsKey(syllableText)) {
syllableFrequencyMap.put(syllableText, syllableFrequency);
} else {
syllableFrequencyMap.put(syllableText, syllableFrequencyMap.get(syllableText) + syllableFrequency);
}
}
}
logger.info("syllableFrequencyMap: " + syllableFrequencyMap);
for (String key : syllableFrequencyMap.keySet()) {
String syllableText = key;
// Skip syllables that are actual words
// TODO: add logic to Word editing
Word word = wordDao.readByText(locale, syllableText);
if (word != null) {
continue;
}
// TODO: add support for trigrams
if (syllableText.length() != 2) {
continue;
}
Syllable existingSyllable = syllableDao.readByText(locale, syllableText);
if (existingSyllable == null) {
Syllable syllable = new Syllable();
syllable.setLocale(locale);
syllable.setTimeLastUpdate(Calendar.getInstance());
syllable.setText(syllableText);
syllable.setUsageCount(syllableFrequencyMap.get(syllableText));
syllableDao.create(syllable);
} else {
existingSyllable.setUsageCount(syllableFrequencyMap.get(syllableText));
syllableDao.update(existingSyllable);
}
}
}
logger.info("execute complete");
}
use of ai.elimu.model.content.StoryBook in project webapp by elimu-ai.
the class SyllableFrequencyHelperTest method testGetSyllableFrequency.
@Test
public void testGetSyllableFrequency() {
StoryBook storyBook = new StoryBook();
storyBook.setLocale(Locale.EN);
List<String> paragraphs = new ArrayList<>();
paragraphs.add("\"Mom,\" called Lebo. \"Come and look.\"");
storyBook.setParagraphs(paragraphs);
Map<String, Integer> syllableFrequencyMap = SyllableFrequencyHelper.getSyllableFrequency(storyBook);
assertThat(syllableFrequencyMap.get("mom"), is(1));
assertThat(syllableFrequencyMap.get("called"), is(1));
assertThat(syllableFrequencyMap.get("lebo"), is(1));
assertThat(syllableFrequencyMap.get("come"), is(1));
assertThat(syllableFrequencyMap.get("and"), is(1));
assertThat(syllableFrequencyMap.get("look"), is(1));
}
Aggregations