Search in sources :

Example 1 with Syllable

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

the class SyllableListController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model, HttpSession session) {
    logger.info("handleRequest");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    // To ease development/testing, auto-generate Syllables
    List<Syllable> syllablesGenerated = generateSyllables(contributor.getLocale());
    for (Syllable syllable : syllablesGenerated) {
        logger.info("syllable.getText(): " + syllable.getText());
        Syllable existingSyllable = syllableDao.readByText(syllable.getLocale(), syllable.getText());
        if (existingSyllable == null) {
            syllableDao.create(syllable);
        }
    }
    List<Syllable> syllables = syllableDao.readAllOrdered(contributor.getLocale());
    logger.info("syllables.size(): " + syllables.size());
    model.addAttribute("syllables", syllables);
    return "content/syllable/list";
}
Also used : Contributor(ai.elimu.model.Contributor) Syllable(ai.elimu.model.content.Syllable) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with Syllable

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

the class WordCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpSession session, @Valid Word word, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Word existingWord = wordDao.readByText(word.getLocale(), word.getText());
    if (existingWord != null) {
        result.rejectValue("text", "NonUnique");
    }
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    List<Allophone> allophones = allophoneDao.readAllOrderedByUsage(contributor.getLocale());
    // Verify that only valid Allophones are used
    String allAllophonesCombined = "";
    for (Allophone allophone : allophones) {
        allAllophonesCombined += allophone.getValueIpa();
    }
    if (StringUtils.isNotBlank(word.getPhonetics())) {
        for (char allophoneCharacter : word.getPhonetics().toCharArray()) {
            String allophone = String.valueOf(allophoneCharacter);
            if (!allAllophonesCombined.contains(allophone)) {
                result.rejectValue("phonetics", "Invalid");
                break;
            }
        }
    }
    if (result.hasErrors()) {
        model.addAttribute("word", word);
        model.addAttribute("allophones", allophones);
        model.addAttribute("wordTypes", WordType.values());
        model.addAttribute("spellingConsistencies", SpellingConsistency.values());
        return "content/word/create";
    } else {
        if (!"I".equals(word.getText())) {
            word.setText(word.getText().toLowerCase());
        }
        word.setTimeLastUpdate(Calendar.getInstance());
        wordDao.create(word);
        WordRevisionEvent wordRevisionEvent = new WordRevisionEvent();
        wordRevisionEvent.setContributor(contributor);
        wordRevisionEvent.setCalendar(Calendar.getInstance());
        wordRevisionEvent.setWord(word);
        wordRevisionEvent.setText(word.getText());
        wordRevisionEvent.setPhonetics(word.getPhonetics());
        wordRevisionEventDao.create(wordRevisionEvent);
        // Label Image with Word of matching title
        Image matchingImage = imageDao.read(word.getText(), word.getLocale());
        if (matchingImage != null) {
            Set<Word> labeledWords = matchingImage.getWords();
            if (!labeledWords.contains(word)) {
                labeledWords.add(word);
                matchingImage.setWords(labeledWords);
                imageDao.update(matchingImage);
            }
        }
        // Delete syllables that are actual words
        Syllable syllable = syllableDao.readByText(contributor.getLocale(), word.getText());
        if (syllable != null) {
            syllableDao.delete(syllable);
        }
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String text = URLEncoder.encode(contributor.getFirstName() + " just added a new Word:\n" + "• Language: \"" + word.getLocale().getLanguage() + "\"\n" + "• Text: \"" + word.getText() + "\"\n" + "• Phonetics (IPA): /" + word.getPhonetics() + "/\n" + "• Word type: " + word.getWordType() + "\n" + "• Spelling consistency: " + word.getSpellingConsistency() + "\n" + "See ") + "http://elimu.ai/content/word/edit/" + word.getId();
            String iconUrl = contributor.getImageUrl();
            SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.CONTENT_CREATION), text, iconUrl, null);
        }
        return "redirect:/content/word/list#" + word.getId();
    }
}
Also used : Word(ai.elimu.model.content.Word) Allophone(ai.elimu.model.content.Allophone) Contributor(ai.elimu.model.Contributor) WordRevisionEvent(ai.elimu.model.contributor.WordRevisionEvent) Image(ai.elimu.model.content.multimedia.Image) Syllable(ai.elimu.model.content.Syllable) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with Syllable

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

the class SyllableCsvExportController method handleRequest.

@RequestMapping(value = "/syllables.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) {
    logger.info("handleRequest");
    // Generate CSV file
    String csvFileContent = "id,text,sound_ids,usage_count" + "\n";
    List<Syllable> syllables = syllableDao.readAllOrderedByUsage();
    logger.info("syllables.size(): " + syllables.size());
    for (Syllable syllable : syllables) {
        long[] soundIdsArray = new long[syllable.getSounds().size()];
        int index = 0;
        for (Sound sound : syllable.getSounds()) {
            soundIdsArray[index] = sound.getId();
            index++;
        }
        csvFileContent += syllable.getId() + "," + "\"" + syllable.getText() + "\"," + Arrays.toString(soundIdsArray) + "," + syllable.getUsageCount() + "\n";
    }
    response.setContentType("text/csv");
    byte[] bytes = csvFileContent.getBytes();
    response.setContentLength(bytes.length);
    try {
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    } catch (IOException ex) {
        logger.error(ex);
    }
}
Also used : Sound(ai.elimu.model.content.Sound) IOException(java.io.IOException) Syllable(ai.elimu.model.content.Syllable) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with Syllable

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

the class SyllableRestController 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 syllables = new JSONArray();
    for (Syllable syllable : syllableDao.readAllOrdered(locale)) {
        SyllableGson syllableGson = JavaToGsonConverter.getSyllableGson(syllable);
        String json = new Gson().toJson(syllableGson);
        syllables.put(new JSONObject(json));
    }
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("result", "success");
    jsonObject.put("syllables", syllables);
    logger.info("jsonObject: " + jsonObject);
    return jsonObject.toString();
}
Also used : JSONObject(org.json.JSONObject) SyllableGson(ai.elimu.model.gson.content.SyllableGson) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) SyllableGson(ai.elimu.model.gson.content.SyllableGson) Syllable(ai.elimu.model.content.Syllable) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with Syllable

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

the class WordCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpServletRequest request, HttpSession session, @Valid Word word, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Word existingWord = wordDao.readByText(word.getText());
    if (existingWord != null) {
        result.rejectValue("text", "NonUnique");
    }
    if (StringUtils.containsAny(word.getText(), " ")) {
        result.rejectValue("text", "WordSpace");
    }
    if (result.hasErrors()) {
        model.addAttribute("word", word);
        model.addAttribute("timeStart", request.getParameter("timeStart"));
        // TODO: sort by letter(s) text
        model.addAttribute("letterSoundCorrespondences", letterSoundCorrespondenceDao.readAllOrderedByUsage());
        model.addAttribute("rootWords", wordDao.readAllOrdered());
        model.addAttribute("emojisByWordId", getEmojisByWordId());
        model.addAttribute("wordTypes", WordType.values());
        model.addAttribute("spellingConsistencies", SpellingConsistency.values());
        model.addAttribute("audio", audioDao.readByTranscription(word.getText()));
        return "content/word/create";
    } else {
        word.setTimeLastUpdate(Calendar.getInstance());
        wordDao.create(word);
        WordContributionEvent wordContributionEvent = new WordContributionEvent();
        wordContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
        wordContributionEvent.setTime(Calendar.getInstance());
        wordContributionEvent.setWord(word);
        wordContributionEvent.setRevisionNumber(word.getRevisionNumber());
        wordContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 1000));
        wordContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
        wordContributionEvent.setPlatform(Platform.WEBAPP);
        wordContributionEventDao.create(wordContributionEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/word/edit/" + word.getId();
        DiscordHelper.sendChannelMessage("Word created: " + contentUrl, "\"" + wordContributionEvent.getWord().getText() + "\"", "Comment: \"" + wordContributionEvent.getComment() + "\"", null, null);
        // Note: updating the list of Words in StoryBookParagraphs is handled by the ParagraphWordScheduler
        // Label Image with Word of matching title
        Image matchingImage = imageDao.read(word.getText());
        if (matchingImage != null) {
            Set<Word> labeledWords = matchingImage.getWords();
            if (!labeledWords.contains(word)) {
                labeledWords.add(word);
                matchingImage.setWords(labeledWords);
                imageDao.update(matchingImage);
            }
        }
        // Delete syllables that are actual words
        Syllable syllable = syllableDao.readByText(word.getText());
        if (syllable != null) {
            syllableDao.delete(syllable);
        }
        // Generate Audio for this Word (if it has not been done already)
        List<Audio> audios = audioDao.readAll(word);
        if (audios.isEmpty()) {
            Calendar timeStart = Calendar.getInstance();
            Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
            try {
                byte[] audioBytes = GoogleCloudTextToSpeechHelper.synthesizeText(word.getText(), language);
                logger.info("audioBytes: " + audioBytes);
                if (audioBytes != null) {
                    Audio audio = new Audio();
                    audio.setTimeLastUpdate(Calendar.getInstance());
                    audio.setContentType(AudioFormat.MP3.getContentType());
                    audio.setWord(word);
                    audio.setTitle("word-id-" + word.getId());
                    audio.setTranscription(word.getText());
                    audio.setBytes(audioBytes);
                    // TODO: Convert from byte[] to File, and extract audio duration
                    audio.setDurationMs(null);
                    audio.setAudioFormat(AudioFormat.MP3);
                    audioDao.create(audio);
                    audios.add(audio);
                    model.addAttribute("audios", audios);
                    AudioContributionEvent audioContributionEvent = new AudioContributionEvent();
                    audioContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
                    audioContributionEvent.setTime(Calendar.getInstance());
                    audioContributionEvent.setAudio(audio);
                    audioContributionEvent.setRevisionNumber(audio.getRevisionNumber());
                    audioContributionEvent.setComment("Google Cloud Text-to-Speech (🤖 auto-generated comment)️");
                    audioContributionEvent.setTimeSpentMs(System.currentTimeMillis() - timeStart.getTimeInMillis());
                    audioContributionEvent.setPlatform(Platform.WEBAPP);
                    audioContributionEventDao.create(audioContributionEvent);
                }
            } catch (Exception ex) {
                logger.error(ex);
            }
        }
        return "redirect:/content/word/list#" + word.getId();
    }
}
Also used : Word(ai.elimu.model.content.Word) Calendar(java.util.Calendar) Image(ai.elimu.model.content.multimedia.Image) Language(ai.elimu.model.v2.enums.Language) WordContributionEvent(ai.elimu.model.contributor.WordContributionEvent) AudioContributionEvent(ai.elimu.model.contributor.AudioContributionEvent) Audio(ai.elimu.model.content.multimedia.Audio) Syllable(ai.elimu.model.content.Syllable) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Syllable (ai.elimu.model.content.Syllable)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 Word (ai.elimu.model.content.Word)4 Image (ai.elimu.model.content.multimedia.Image)3 Contributor (ai.elimu.model.Contributor)2 WordContributionEvent (ai.elimu.model.contributor.WordContributionEvent)2 Language (ai.elimu.model.v2.enums.Language)2 Allophone (ai.elimu.model.content.Allophone)1 Emoji (ai.elimu.model.content.Emoji)1 Sound (ai.elimu.model.content.Sound)1 StoryBook (ai.elimu.model.content.StoryBook)1 StoryBookChapter (ai.elimu.model.content.StoryBookChapter)1 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)1 Audio (ai.elimu.model.content.multimedia.Audio)1 AudioContributionEvent (ai.elimu.model.contributor.AudioContributionEvent)1 WordRevisionEvent (ai.elimu.model.contributor.WordRevisionEvent)1 SyllableGson (ai.elimu.model.gson.content.SyllableGson)1 Gson (com.google.gson.Gson)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1