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;
}
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;
}
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);
}
}
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";
}
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();
}
}
Aggregations