Search in sources :

Example 11 with StoryBook

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();
}
Also used : StoryBook(ai.elimu.model.content.StoryBook) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) StoryBookGson(ai.elimu.model.gson.content.StoryBookGson) StoryBookGson(ai.elimu.model.gson.content.StoryBookGson) Gson(com.google.gson.Gson) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with StoryBook

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");
}
Also used : Locale(ai.elimu.model.enums.Locale) StoryBook(ai.elimu.model.content.StoryBook) Word(ai.elimu.model.content.Word) HashMap(java.util.HashMap) Syllable(ai.elimu.model.content.Syllable) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 13 with StoryBook

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));
}
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