Search in sources :

Example 1 with Locale

use of ai.elimu.model.enums.Locale in project webapp by elimu-ai.

the class AllophoneUsageCountScheduler method execute.

// At 06:30 every day
@Scheduled(cron = "00 30 06 * * *")
public synchronized void execute() {
    logger.info("execute");
    for (Locale locale : Locale.values()) {
        logger.info("Calculating usage count of Allophones for locale " + locale);
        Map<String, Integer> allophoneFrequencyMap = new HashMap<>();
        List<Allophone> allophones = allophoneDao.readAllOrdered(locale);
        logger.info("allophones.size(): " + allophones.size());
        List<Word> words = wordDao.readAllOrdered(locale);
        logger.info("words.size(): " + words.size());
        for (Word word : words) {
            List<String> allophonesInWord = PhoneticsHelper.getAllophones(word);
            for (String allophoneInWord : allophonesInWord) {
                if (!allophoneFrequencyMap.containsKey(allophoneInWord)) {
                    allophoneFrequencyMap.put(allophoneInWord, word.getUsageCount());
                } else {
                    allophoneFrequencyMap.put(allophoneInWord, allophoneFrequencyMap.get(allophoneInWord) + word.getUsageCount());
                }
            }
        }
        for (String allophoneIpa : allophoneFrequencyMap.keySet()) {
            Allophone allophone = allophoneDao.readByValueIpa(locale, allophoneIpa);
            allophone.setUsageCount(allophoneFrequencyMap.get(allophoneIpa));
            allophoneDao.update(allophone);
        }
    }
    logger.info("execute complete");
}
Also used : Locale(ai.elimu.model.enums.Locale) Word(ai.elimu.model.content.Word) HashMap(java.util.HashMap) Allophone(ai.elimu.model.content.Allophone) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 2 with Locale

use of ai.elimu.model.enums.Locale 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 3 with Locale

use of ai.elimu.model.enums.Locale 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 4 with Locale

use of ai.elimu.model.enums.Locale in project webapp by elimu-ai.

the class AllophoneDaoTest method testLowerCaseVsUpperCase.

@Test
public void testLowerCaseVsUpperCase() {
    Locale locale = Locale.values()[(int) (Math.random() * Locale.values().length)];
    logger.info("locale: " + locale);
    Allophone allophoneLowerCaseT = new Allophone();
    allophoneLowerCaseT.setLocale(locale);
    allophoneLowerCaseT.setValueIpa("t");
    allophoneLowerCaseT.setValueSampa("t");
    allophoneDao.create(allophoneLowerCaseT);
    Allophone allophoneUpperCaseT = new Allophone();
    allophoneUpperCaseT.setLocale(locale);
    allophoneUpperCaseT.setValueIpa("θ");
    allophoneUpperCaseT.setValueSampa("T");
    allophoneDao.create(allophoneUpperCaseT);
    assertThat(allophoneDao.readByValueSampa(locale, "t").getValueSampa(), is("t"));
    assertThat(allophoneDao.readByValueSampa(locale, "T").getValueSampa(), is("T"));
}
Also used : Locale(ai.elimu.model.enums.Locale) Allophone(ai.elimu.model.content.Allophone) Test(org.junit.Test)

Example 5 with Locale

use of ai.elimu.model.enums.Locale 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)

Aggregations

Locale (ai.elimu.model.enums.Locale)8 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 Scheduled (org.springframework.scheduling.annotation.Scheduled)4 Allophone (ai.elimu.model.content.Allophone)3 StoryBook (ai.elimu.model.content.StoryBook)3 Word (ai.elimu.model.content.Word)3 Device (ai.elimu.model.Device)1 Application (ai.elimu.model.admin.Application)1 ApplicationOpenedEvent (ai.elimu.model.analytics.ApplicationOpenedEvent)1 Letter (ai.elimu.model.content.Letter)1 Syllable (ai.elimu.model.content.Syllable)1