Search in sources :

Example 46 with Word

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

the class SyllableHelperTest method testGetTriSyllables_languageEN.

@Test
public void testGetTriSyllables_languageEN() {
    Word word = new Word();
    word.setText("grandmother");
    List<String> syllables = SyllableHelper.getSyllables(word, Language.ENG);
    assertThat(syllables.get(0), is("grand"));
    assertThat(syllables.get(1), is("moth"));
    assertThat(syllables.get(2), is("er"));
}
Also used : Word(ai.elimu.model.content.Word) Test(org.junit.Test)

Example 47 with Word

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

the class ImageCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpSession session, /*@Valid*/
Image image, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    if (StringUtils.isBlank(image.getTitle())) {
        result.rejectValue("title", "NotNull");
    } else {
        Image existingImage = imageDao.read(image.getTitle(), image.getLocale());
        if (existingImage != null) {
            result.rejectValue("title", "NonUnique");
        }
    }
    try {
        byte[] bytes = multipartFile.getBytes();
        if (multipartFile.isEmpty() || (bytes == null) || (bytes.length == 0)) {
            result.rejectValue("bytes", "NotNull");
        } else {
            String originalFileName = multipartFile.getOriginalFilename();
            logger.info("originalFileName: " + originalFileName);
            if (originalFileName.toLowerCase().endsWith(".png")) {
                image.setImageFormat(ImageFormat.PNG);
            } else if (originalFileName.toLowerCase().endsWith(".jpg") || originalFileName.toLowerCase().endsWith(".jpeg")) {
                image.setImageFormat(ImageFormat.JPG);
            } else if (originalFileName.toLowerCase().endsWith(".gif")) {
                image.setImageFormat(ImageFormat.GIF);
            } else {
                result.rejectValue("bytes", "typeMismatch");
            }
            if (image.getImageFormat() != null) {
                String contentType = multipartFile.getContentType();
                logger.info("contentType: " + contentType);
                image.setContentType(contentType);
                image.setBytes(bytes);
                if (image.getImageFormat() != ImageFormat.GIF) {
                    int width = ImageHelper.getWidth(bytes);
                    logger.info("width: " + width + "px");
                    if (width < ImageHelper.MINIMUM_WIDTH) {
                        result.rejectValue("bytes", "image.too.small");
                        image.setBytes(null);
                    } else {
                        if (width > ImageHelper.MINIMUM_WIDTH) {
                            bytes = ImageHelper.scaleImage(bytes, ImageHelper.MINIMUM_WIDTH);
                            image.setBytes(bytes);
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        logger.error(e);
    }
    if (result.hasErrors()) {
        model.addAttribute("contentLicenses", ContentLicense.values());
        model.addAttribute("literacySkills", LiteracySkill.values());
        model.addAttribute("numeracySkills", NumeracySkill.values());
        return "content/multimedia/image/create";
    } else {
        image.setTitle(image.getTitle().toLowerCase());
        int[] dominantColor = ImageColorHelper.getDominantColor(image.getBytes());
        image.setDominantColor("rgb(" + dominantColor[0] + "," + dominantColor[1] + "," + dominantColor[2] + ")");
        image.setTimeLastUpdate(Calendar.getInstance());
        imageDao.create(image);
        // TODO: store RevisionEvent
        // Label Image with Word of matching title
        Word matchingWord = wordDao.readByText(contributor.getLocale(), image.getTitle());
        if (matchingWord != null) {
            Set<Word> labeledWords = new HashSet<>();
            if (!labeledWords.contains(matchingWord)) {
                labeledWords.add(matchingWord);
                image.setWords(labeledWords);
                imageDao.update(image);
            }
        }
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String text = URLEncoder.encode(contributor.getFirstName() + " just added a new Image:\n" + "• Language: \"" + image.getLocale().getLanguage() + "\"\n" + "• Title: \"" + image.getTitle() + "\"\n" + "See ") + "http://elimu.ai/content/multimedia/image/edit/" + image.getId();
            String iconUrl = contributor.getImageUrl();
            String imageUrl = "http://elimu.ai/image/" + image.getId() + "." + image.getImageFormat().toString().toLowerCase();
            SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.CONTENT_CREATION), text, iconUrl, imageUrl);
        }
        return "redirect:/content/multimedia/image/list#" + image.getId();
    }
}
Also used : Word(ai.elimu.model.content.Word) Contributor(ai.elimu.model.Contributor) IOException(java.io.IOException) Image(ai.elimu.model.content.multimedia.Image) HashSet(java.util.HashSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with Word

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

the class NumberCreateController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(HttpSession session, Model model) {
    logger.info("handleRequest");
    Number number = new Number();
    model.addAttribute("number", number);
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    List<Word> words = wordDao.readAllOrdered(contributor.getLocale());
    model.addAttribute("words", words);
    return "content/number/create";
}
Also used : Word(ai.elimu.model.content.Word) Number(ai.elimu.model.content.Number) Contributor(ai.elimu.model.Contributor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 49 with Word

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

the class NumberEditController method handleRequest.

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(HttpSession session, Model model, @PathVariable Long id) {
    logger.info("handleRequest");
    Number number = numberDao.read(id);
    model.addAttribute("number", number);
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    List<Word> words = wordDao.readAllOrdered(contributor.getLocale());
    model.addAttribute("words", words);
    return "content/number/edit";
}
Also used : Word(ai.elimu.model.content.Word) Number(ai.elimu.model.content.Number) Contributor(ai.elimu.model.Contributor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 50 with Word

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

the class NumberListController method getNumberWords.

private List<Word> getNumberWords(Locale locale, String... words) {
    List<Word> numberWords = new ArrayList<>();
    for (String word : words) {
        Word numberWord = wordDao.readByText(locale, word);
        numberWords.add(numberWord);
    }
    return numberWords;
}
Also used : Word(ai.elimu.model.content.Word) ArrayList(java.util.ArrayList)

Aggregations

Word (ai.elimu.model.content.Word)88 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)40 HashMap (java.util.HashMap)23 Emoji (ai.elimu.model.content.Emoji)22 ArrayList (java.util.ArrayList)20 Number (ai.elimu.model.content.Number)17 Letter (ai.elimu.model.content.Letter)15 Test (org.junit.Test)12 Image (ai.elimu.model.content.multimedia.Image)11 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)9 JSONArray (org.json.JSONArray)9 JSONObject (org.json.JSONObject)9 Contributor (ai.elimu.model.Contributor)8 Language (ai.elimu.model.v2.enums.Language)8 IOException (java.io.IOException)8 LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)7 Audio (ai.elimu.model.content.multimedia.Audio)7 Contributor (ai.elimu.model.contributor.Contributor)7 WordContributionEvent (ai.elimu.model.contributor.WordContributionEvent)7 WordGson (ai.elimu.model.v2.gson.content.WordGson)7