Search in sources :

Example 31 with Word

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

the class JavaToGsonConverter method getNumberGson.

public static NumberGson getNumberGson(Number number) {
    if (number == null) {
        return null;
    } else {
        NumberGson numberGson = new NumberGson();
        numberGson.setId(number.getId());
        numberGson.setLocale(number.getLocale());
        numberGson.setTimeLastUpdate(number.getTimeLastUpdate());
        numberGson.setRevisionNumber(number.getRevisionNumber());
        numberGson.setContentStatus(number.getContentStatus());
        numberGson.setValue(number.getValue());
        numberGson.setSymbol(number.getSymbol());
        numberGson.setWord(getWordGson(number.getWord()));
        List<WordGson> words = new ArrayList<>();
        for (Word word : number.getWords()) {
            WordGson wordGson = getWordGson(word);
            words.add(wordGson);
        }
        numberGson.setWords(words);
        return numberGson;
    }
}
Also used : WordGson(ai.elimu.model.gson.content.WordGson) Word(ai.elimu.model.content.Word) ArrayList(java.util.ArrayList) NumberGson(ai.elimu.model.gson.content.NumberGson)

Example 32 with Word

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

the class WordRestController 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 words = new JSONArray();
    for (Word word : wordDao.readAllOrdered(locale)) {
        WordGson wordGson = JavaToGsonConverter.getWordGson(word);
        String json = new Gson().toJson(wordGson);
        words.put(new JSONObject(json));
    }
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("result", "success");
    jsonObject.put("words", words);
    logger.info("jsonObject: " + jsonObject);
    return jsonObject.toString();
}
Also used : WordGson(ai.elimu.model.gson.content.WordGson) Word(ai.elimu.model.content.Word) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) WordGson(ai.elimu.model.gson.content.WordGson) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 33 with Word

use of ai.elimu.model.content.Word 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 34 with Word

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

the class ImageDaoTest method testReadAllLabeled.

@Test
public void testReadAllLabeled() {
    Word wordDog = new Word();
    wordDog.setText("dog");
    wordDog.setLocale(Locale.EN);
    wordDao.create(wordDog);
    Word wordCat = new Word();
    wordCat.setText("cat");
    wordCat.setLocale(Locale.EN);
    wordDao.create(wordCat);
    List<Image> images = imageDao.readAllLabeled(wordCat, Locale.EN);
    assertThat(images.size(), is(0));
    Set<Word> words = new HashSet<>();
    words.add(wordCat);
    Image image = new Image();
    image.setTitle("image");
    image.setWords(words);
    image.setLocale(Locale.EN);
    imageDao.create(image);
    images = imageDao.readAllLabeled(wordDog, Locale.EN);
    assertThat(images.size(), is(0));
    images = imageDao.readAllLabeled(wordCat, Locale.EN);
    assertThat(images.size(), is(1));
    assertThat(images.get(0).getWords().size(), is(1));
    words.add(wordDog);
    image.setWords(words);
    imageDao.update(image);
    images = imageDao.readAllLabeled(wordCat, Locale.EN);
    assertThat(images.size(), is(1));
    assertThat(images.get(0).getWords().size(), is(2));
}
Also used : Word(ai.elimu.model.content.Word) Image(ai.elimu.model.content.multimedia.Image) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 35 with Word

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

the class NumberDaoTest method testStoreWithMultipleNumberWords.

@Test
public void testStoreWithMultipleNumberWords() {
    Word word1 = new Word();
    wordDao.create(word1);
    Word word2 = new Word();
    wordDao.create(word2);
    List<Word> numberWords = new ArrayList<>();
    numberWords.add(word1);
    numberWords.add(word2);
    Number number = new Number();
    number.setWords(numberWords);
    numberDao.create(number);
    assertThat(number.getWords().size(), is(2));
}
Also used : Word(ai.elimu.model.content.Word) Number(ai.elimu.model.content.Number) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Word (ai.elimu.model.content.Word)35 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 Number (ai.elimu.model.content.Number)14 Contributor (ai.elimu.model.Contributor)10 Letter (ai.elimu.model.content.Letter)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Image (ai.elimu.model.content.multimedia.Image)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 Allophone (ai.elimu.model.content.Allophone)5 WordGson (ai.elimu.model.gson.content.WordGson)5 Audio (ai.elimu.model.content.multimedia.Audio)4 NumberGson (ai.elimu.model.gson.content.NumberGson)4 HashMap (java.util.HashMap)4 Syllable (ai.elimu.model.content.Syllable)3 Locale (ai.elimu.model.enums.Locale)3 LetterGson (ai.elimu.model.gson.content.LetterGson)3 Scheduled (org.springframework.scheduling.annotation.Scheduled)3 StoryBook (ai.elimu.model.content.StoryBook)2 Video (ai.elimu.model.content.multimedia.Video)2