Search in sources :

Example 11 with LetterSoundCorrespondence

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

the class CsvContentExtractionHelper method getLetterSoundCorrespondencesFromCsvBackup.

/**
 * For information on how the CSV files were generated, see {@link LetterSoundCorrespondenceCsvExportController#handleRequest}.
 */
public static List<LetterSoundCorrespondence> getLetterSoundCorrespondencesFromCsvBackup(File csvFile, LetterDao letterDao, SoundDao soundDao, LetterSoundCorrespondenceDao letterSoundCorrespondenceDao) {
    logger.info("getLetterSoundCorrespondencesFromCsvBackup");
    List<LetterSoundCorrespondence> letterSoundCorrespondences = new ArrayList<>();
    Path csvFilePath = Paths.get(csvFile.toURI());
    logger.info("csvFilePath: " + csvFilePath);
    try {
        Reader reader = Files.newBufferedReader(csvFilePath);
        CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "letter_ids", "letter_texts", "sound_ids", "sound_values_ipa", "usage_count").withSkipHeaderRecord();
        CSVParser csvParser = new CSVParser(reader, csvFormat);
        for (CSVRecord csvRecord : csvParser) {
            logger.info("csvRecord: " + csvRecord);
            LetterSoundCorrespondence letterSoundCorrespondence = new LetterSoundCorrespondence();
            JSONArray letterIdsJsonArray = new JSONArray(csvRecord.get("letter_ids"));
            logger.info("letterIdsJsonArray: " + letterIdsJsonArray);
            JSONArray letterTextsJsonArray = new JSONArray(csvRecord.get("letter_texts"));
            logger.info("letterTextsJsonArray: " + letterTextsJsonArray);
            List<Letter> letters = new ArrayList<>();
            for (int i = 0; i < letterTextsJsonArray.length(); i++) {
                String letterText = letterTextsJsonArray.getString(i);
                logger.info("Looking up Letter with text '" + letterText + "'");
                Letter letter = letterDao.readByText(letterText);
                logger.info("letter.getId(): " + letter.getId());
                letters.add(letter);
            }
            letterSoundCorrespondence.setLetters(letters);
            JSONArray soundIdsJsonArray = new JSONArray(csvRecord.get("sound_ids"));
            logger.info("soundIdsJsonArray: " + soundIdsJsonArray);
            JSONArray soundValuesIpaJsonArray = new JSONArray(csvRecord.get("sound_values_ipa"));
            logger.info("soundValuesIpaJsonArray: " + soundValuesIpaJsonArray);
            List<Sound> sounds = new ArrayList<>();
            for (int i = 0; i < soundValuesIpaJsonArray.length(); i++) {
                String soundValueIpa = soundValuesIpaJsonArray.getString(i);
                logger.info("Looking up Sound with IPA value /" + soundValueIpa + "/");
                Sound sound = soundDao.readByValueIpa(soundValueIpa);
                logger.info("sound.getId(): " + sound.getId());
                sounds.add(sound);
            }
            letterSoundCorrespondence.setSounds(sounds);
            Integer usageCount = Integer.valueOf(csvRecord.get("usage_count"));
            letterSoundCorrespondence.setUsageCount(usageCount);
            letterSoundCorrespondences.add(letterSoundCorrespondence);
        }
    } catch (IOException ex) {
        logger.error(ex);
    }
    return letterSoundCorrespondences;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Reader(java.io.Reader) Sound(ai.elimu.model.content.Sound) IOException(java.io.IOException) Letter(ai.elimu.model.content.Letter) CSVParser(org.apache.commons.csv.CSVParser) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence)

Example 12 with LetterSoundCorrespondence

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

the class CsvContentExtractionHelper method getWordsFromCsvBackup.

/**
 * For information on how the CSV files were generated, see {@link WordCsvExportController#handleRequest}.
 */
public static List<Word> getWordsFromCsvBackup(File csvFile, LetterDao letterDao, SoundDao soundDao, LetterSoundCorrespondenceDao letterSoundCorrespondenceDao, WordDao wordDao) {
    logger.info("getWordsFromCsvBackup");
    List<Word> words = new ArrayList<>();
    Path csvFilePath = Paths.get(csvFile.toURI());
    logger.info("csvFilePath: " + csvFilePath);
    try {
        Reader reader = Files.newBufferedReader(csvFilePath);
        CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "text", "letter_sound_correspondences", "usage_count", "word_type", "spelling_consistency", "root_word_id", "root_word_text").withSkipHeaderRecord();
        CSVParser csvParser = new CSVParser(reader, csvFormat);
        for (CSVRecord csvRecord : csvParser) {
            logger.info("csvRecord: " + csvRecord);
            Word word = new Word();
            String text = csvRecord.get("text");
            word.setText(text);
            JSONArray letterSoundCorrespondencesJsonArray = new JSONArray(csvRecord.get("letter_sound_correspondences"));
            logger.info("letterSoundCorrespondencesJsonArray: " + letterSoundCorrespondencesJsonArray);
            List<LetterSoundCorrespondence> letterSoundCorrespondences = new ArrayList<>();
            for (int i = 0; i < letterSoundCorrespondencesJsonArray.length(); i++) {
                JSONObject letterSoundCorrespondenceJsonObject = letterSoundCorrespondencesJsonArray.getJSONObject(i);
                logger.info("letterSoundCorrespondenceJsonObject: " + letterSoundCorrespondenceJsonObject);
                List<Letter> letters = new ArrayList<>();
                JSONArray lettersJsonArray = letterSoundCorrespondenceJsonObject.getJSONArray("letters");
                for (int j = 0; j < lettersJsonArray.length(); j++) {
                    Letter letter = letterDao.readByText(lettersJsonArray.getString(j));
                    letters.add(letter);
                }
                List<Sound> sounds = new ArrayList<>();
                JSONArray soundsJsonArray = letterSoundCorrespondenceJsonObject.getJSONArray("sounds");
                for (int j = 0; j < soundsJsonArray.length(); j++) {
                    Sound sound = soundDao.readByValueIpa(soundsJsonArray.getString(j));
                    sounds.add(sound);
                }
                LetterSoundCorrespondence letterSoundCorrespondence = letterSoundCorrespondenceDao.read(letters, sounds);
                logger.info("letterSoundCorrespondence.getId(): " + letterSoundCorrespondence.getId());
                letterSoundCorrespondences.add(letterSoundCorrespondence);
            }
            word.setLetterSoundCorrespondences(letterSoundCorrespondences);
            Integer usageCount = Integer.valueOf(csvRecord.get("usage_count"));
            word.setUsageCount(usageCount);
            if (StringUtils.isNotBlank(csvRecord.get("word_type"))) {
                WordType wordType = WordType.valueOf(csvRecord.get("word_type"));
                word.setWordType(wordType);
            }
            if (StringUtils.isNotBlank(csvRecord.get("spelling_consistency"))) {
                SpellingConsistency spellingConsistency = SpellingConsistency.valueOf(csvRecord.get("spelling_consistency"));
                word.setSpellingConsistency(spellingConsistency);
            }
            // TODO: Store rootWords _after_ all Words have been stored
            // if (StringUtils.isNotBlank(csvRecord.get("root_word_text"))) {
            // String rootWordText = csvRecord.get("root_word_text");
            // Word rootWord = wordDao.readByText(language, rootWordText);
            // word.setRootWord(rootWord);
            // }
            words.add(word);
        }
    } catch (IOException ex) {
        logger.error(ex);
    }
    return words;
}
Also used : Path(java.nio.file.Path) Word(ai.elimu.model.content.Word) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Reader(java.io.Reader) Sound(ai.elimu.model.content.Sound) IOException(java.io.IOException) WordType(ai.elimu.model.v2.enums.content.WordType) Letter(ai.elimu.model.content.Letter) SpellingConsistency(ai.elimu.model.v2.enums.content.SpellingConsistency) JSONObject(org.json.JSONObject) CSVParser(org.apache.commons.csv.CSVParser) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence)

Example 13 with LetterSoundCorrespondence

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

the class LetterSoundCorrespondenceCsvExportController method handleRequest.

@RequestMapping(value = "/letter-sound-correspondences.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) throws IOException {
    logger.info("handleRequest");
    List<LetterSoundCorrespondence> letterSoundCorrespondences = letterSoundCorrespondenceDao.readAllOrderedByUsage();
    logger.info("letterSoundCorrespondences.size(): " + letterSoundCorrespondences.size());
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "letter_ids", "letter_texts", "sound_ids", "sound_values_ipa", "usage_count");
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    for (LetterSoundCorrespondence letterSoundCorrespondence : letterSoundCorrespondences) {
        logger.info("letterSoundCorrespondence.getId(): \"" + letterSoundCorrespondence.getId() + "\"");
        JSONArray letterIdsJsonArray = new JSONArray();
        int index = 0;
        for (Letter letter : letterSoundCorrespondence.getLetters()) {
            letterIdsJsonArray.put(index, letter.getId());
            index++;
        }
        JSONArray letterTextsJsonArray = new JSONArray();
        index = 0;
        for (Letter letter : letterSoundCorrespondence.getLetters()) {
            letterTextsJsonArray.put(index, letter.getText());
            index++;
        }
        JSONArray soundIdsJsonArray = new JSONArray();
        index = 0;
        for (Sound sound : letterSoundCorrespondence.getSounds()) {
            soundIdsJsonArray.put(index, sound.getId());
            index++;
        }
        JSONArray soundValuesIpaJsonArray = new JSONArray();
        index = 0;
        for (Sound sound : letterSoundCorrespondence.getSounds()) {
            soundValuesIpaJsonArray.put(index, sound.getValueIpa());
            index++;
        }
        csvPrinter.printRecord(letterSoundCorrespondence.getId(), letterIdsJsonArray, letterTextsJsonArray, soundIdsJsonArray, soundValuesIpaJsonArray, letterSoundCorrespondence.getUsageCount());
        csvPrinter.flush();
    }
    String csvFileContent = stringWriter.toString();
    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 : CSVPrinter(org.apache.commons.csv.CSVPrinter) Letter(ai.elimu.model.content.Letter) StringWriter(java.io.StringWriter) JSONArray(org.json.JSONArray) CSVFormat(org.apache.commons.csv.CSVFormat) Sound(ai.elimu.model.content.Sound) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with LetterSoundCorrespondence

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

the class LetterSoundCorrespondenceListController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model) {
    logger.info("handleRequest");
    List<LetterSoundCorrespondence> letterSoundCorrespondences = letterSoundCorrespondenceDao.readAllOrderedByUsage();
    model.addAttribute("letterSoundCorrespondences", letterSoundCorrespondences);
    int maxUsageCount = 0;
    for (LetterSoundCorrespondence letterSoundCorrespondence : letterSoundCorrespondences) {
        if (letterSoundCorrespondence.getUsageCount() > maxUsageCount) {
            maxUsageCount = letterSoundCorrespondence.getUsageCount();
        }
    }
    model.addAttribute("maxUsageCount", maxUsageCount);
    return "content/letter-sound-correspondence/list";
}
Also used : LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with LetterSoundCorrespondence

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

the class LetterSoundCorrespondenceEditController method handleSubmit.

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(HttpServletRequest request, HttpSession session, @Valid LetterSoundCorrespondence letterSoundCorrespondence, BindingResult result, Model model) {
    logger.info("handleSubmit");
    // Check if the LetterSoundCorrespondence already exists
    LetterSoundCorrespondence existingLetterSoundCorrespondence = letterSoundCorrespondenceDao.read(letterSoundCorrespondence.getLetters(), letterSoundCorrespondence.getSounds());
    if ((existingLetterSoundCorrespondence != null) && !existingLetterSoundCorrespondence.getId().equals(letterSoundCorrespondence.getId())) {
        result.rejectValue("letters", "NonUnique");
    }
    if (result.hasErrors()) {
        model.addAttribute("letterSoundCorrespondence", letterSoundCorrespondence);
        model.addAttribute("timeStart", System.currentTimeMillis());
        List<Letter> letters = letterDao.readAllOrdered();
        model.addAttribute("letters", letters);
        List<Sound> sounds = soundDao.readAllOrdered();
        model.addAttribute("sounds", sounds);
        model.addAttribute("letterSoundCorrespondenceContributionEvents", letterSoundCorrespondenceContributionEventDao.readAll(letterSoundCorrespondence));
        model.addAttribute("letterSoundCorrespondencePeerReviewEvents", letterSoundCorrespondencePeerReviewEventDao.readAll(letterSoundCorrespondence));
        return "content/letter-sound-correspondence/edit";
    } else {
        letterSoundCorrespondence.setTimeLastUpdate(Calendar.getInstance());
        letterSoundCorrespondence.setRevisionNumber(letterSoundCorrespondence.getRevisionNumber() + 1);
        letterSoundCorrespondenceDao.update(letterSoundCorrespondence);
        LetterSoundCorrespondenceContributionEvent letterSoundCorrespondenceContributionEvent = new LetterSoundCorrespondenceContributionEvent();
        letterSoundCorrespondenceContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
        letterSoundCorrespondenceContributionEvent.setTime(Calendar.getInstance());
        letterSoundCorrespondenceContributionEvent.setLetterSoundCorrespondence(letterSoundCorrespondence);
        letterSoundCorrespondenceContributionEvent.setRevisionNumber(letterSoundCorrespondence.getRevisionNumber());
        letterSoundCorrespondenceContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 1000));
        letterSoundCorrespondenceContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
        letterSoundCorrespondenceContributionEvent.setPlatform(Platform.WEBAPP);
        letterSoundCorrespondenceContributionEventDao.create(letterSoundCorrespondenceContributionEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/letter-sound-correspondence/edit/" + letterSoundCorrespondence.getId();
        DiscordHelper.sendChannelMessage("Letter-sound correspondence edited: " + contentUrl, "\"" + letterSoundCorrespondence.getLetters().stream().map(Letter::getText).collect(Collectors.joining()) + "\"", "Comment: \"" + letterSoundCorrespondenceContributionEvent.getComment() + "\"", null, null);
        return "redirect:/content/letter-sound-correspondence/list#" + letterSoundCorrespondence.getId();
    }
}
Also used : Letter(ai.elimu.model.content.Letter) LetterSoundCorrespondenceContributionEvent(ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent) Sound(ai.elimu.model.content.Sound) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)21 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Letter (ai.elimu.model.content.Letter)10 Sound (ai.elimu.model.content.Sound)10 Word (ai.elimu.model.content.Word)7 ArrayList (java.util.ArrayList)7 JSONArray (org.json.JSONArray)6 JSONObject (org.json.JSONObject)5 LetterSoundCorrespondenceContributionEvent (ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent)4 LetterSoundCorrespondenceGson (ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson)4 IOException (java.io.IOException)4 CSVFormat (org.apache.commons.csv.CSVFormat)4 Contributor (ai.elimu.model.contributor.Contributor)3 WordGson (ai.elimu.model.v2.gson.content.WordGson)3 Gson (com.google.gson.Gson)3 WordContributionEvent (ai.elimu.model.contributor.WordContributionEvent)2 WordContributionEventGson (ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson)2 Reader (java.io.Reader)2 StringWriter (java.io.StringWriter)2 Path (java.nio.file.Path)2