Search in sources :

Example 1 with WordType

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

Aggregations

Letter (ai.elimu.model.content.Letter)1 LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)1 Sound (ai.elimu.model.content.Sound)1 Word (ai.elimu.model.content.Word)1 SpellingConsistency (ai.elimu.model.v2.enums.content.SpellingConsistency)1 WordType (ai.elimu.model.v2.enums.content.WordType)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 CSVParser (org.apache.commons.csv.CSVParser)1 CSVRecord (org.apache.commons.csv.CSVRecord)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1