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;
}
Aggregations