Skip to content

Commit

Permalink
refactor: Rename LetterSoundCorrespondence to LetterSound and add DB …
Browse files Browse the repository at this point in the history
…migration script elimu-ai#1677
  • Loading branch information
Sneha65 committed Jul 7, 2024
1 parent 3cf962a commit 6e9c8b4
Show file tree
Hide file tree
Showing 27 changed files with 173 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ai.elimu.dao;

import ai.elimu.model.content.LetterSoundCorrespondence;
import ai.elimu.model.content.LetterSound;
import ai.elimu.model.contributor.Contributor;
import ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent;
import java.util.List;
Expand All @@ -10,7 +10,7 @@ public interface LetterSoundContributionEventDao extends GenericDao<LetterSoundC

List<LetterSoundCorrespondenceContributionEvent> readAllOrderedByTimeDesc() throws DataAccessException;

List<LetterSoundCorrespondenceContributionEvent> readAll(LetterSoundCorrespondence letterSound) throws DataAccessException;
List<LetterSoundCorrespondenceContributionEvent> readAll(LetterSound letterSound) throws DataAccessException;

List<LetterSoundCorrespondenceContributionEvent> readAll(Contributor contributor) throws DataAccessException;

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/ai/elimu/dao/LetterSoundDao.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package ai.elimu.dao;

import ai.elimu.model.content.LetterSound;
import ai.elimu.model.content.Sound;
import ai.elimu.model.content.Letter;
import ai.elimu.model.content.LetterSoundCorrespondence;

import java.util.List;

import org.springframework.dao.DataAccessException;

public interface LetterSoundDao extends GenericDao<LetterSoundCorrespondence> {
public interface LetterSoundDao extends GenericDao<LetterSound> {

LetterSoundCorrespondence read(List<Letter> letters, List<Sound> sounds) throws DataAccessException;
LetterSound read(List<Letter> letters, List<Sound> sounds) throws DataAccessException;

List<LetterSoundCorrespondence> readAllOrderedByUsage() throws DataAccessException;
List<LetterSound> readAllOrderedByUsage() throws DataAccessException;

List<LetterSoundCorrespondence> readAllOrderedByLettersLength() throws DataAccessException;
List<LetterSound> readAllOrderedByLettersLength() throws DataAccessException;
}
4 changes: 2 additions & 2 deletions src/main/java/ai/elimu/dao/LetterSoundPeerReviewEventDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ai.elimu.dao;

import ai.elimu.model.content.LetterSoundCorrespondence;
import ai.elimu.model.content.LetterSound;
import ai.elimu.model.contributor.Contributor;
import ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent;
import ai.elimu.model.contributor.LetterSoundCorrespondencePeerReviewEvent;
Expand All @@ -11,7 +11,7 @@ public interface LetterSoundPeerReviewEventDao extends GenericDao<LetterSoundCor

List<LetterSoundCorrespondencePeerReviewEvent> readAll(LetterSoundCorrespondenceContributionEvent letterSoundCorrespondenceContributionEvent, Contributor contributor) throws DataAccessException;

List<LetterSoundCorrespondencePeerReviewEvent> readAll(LetterSoundCorrespondence letterSoundCorrespondence) throws DataAccessException;
List<LetterSoundCorrespondencePeerReviewEvent> readAll(LetterSound letterSoundCorrespondence) throws DataAccessException;

List<LetterSoundCorrespondencePeerReviewEvent> readAll(Contributor contributor) throws DataAccessException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ai.elimu.dao.jpa;

import ai.elimu.model.content.LetterSound;
import ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent;
import ai.elimu.dao.LetterSoundContributionEventDao;
import ai.elimu.model.content.LetterSoundCorrespondence;
import ai.elimu.model.contributor.Contributor;
import java.util.List;
import org.springframework.dao.DataAccessException;
Expand All @@ -19,7 +19,7 @@ public List<LetterSoundCorrespondenceContributionEvent> readAllOrderedByTimeDesc
}

@Override
public List<LetterSoundCorrespondenceContributionEvent> readAll(LetterSoundCorrespondence letterSound) throws DataAccessException {
public List<LetterSoundCorrespondenceContributionEvent> readAll(LetterSound letterSound) throws DataAccessException {
return em.createQuery(
"SELECT e " +
"FROM LetterSoundCorrespondenceContributionEvent e " +
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/ai/elimu/dao/jpa/LetterSoundDaoJpa.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package ai.elimu.dao.jpa;

import ai.elimu.model.content.LetterSound;
import ai.elimu.model.content.Sound;
import ai.elimu.model.content.Letter;
import ai.elimu.model.content.LetterSoundCorrespondence;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.dao.DataAccessException;
import ai.elimu.dao.LetterSoundDao;

public class LetterSoundDaoJpa extends GenericDaoJpa<LetterSoundCorrespondence> implements LetterSoundDao {
public class LetterSoundDaoJpa extends GenericDaoJpa<LetterSound> implements LetterSoundDao {

@Override
public LetterSoundCorrespondence read(List<Letter> letters, List<Sound> sounds) throws DataAccessException {
public LetterSound read(List<Letter> letters, List<Sound> sounds) throws DataAccessException {
// TODO: implement usage of CriteriaQuery/CriteriaQuery

String letterSoundCorrespondenceLetters = letters.stream().map(Letter::getText).collect(Collectors.joining());
String letterSoundCorrespondenceSounds = sounds.stream().map(Sound::getValueIpa).collect(Collectors.joining());
for (LetterSoundCorrespondence letterSoundCorrespondence : readAllOrderedByUsage()) {
for (LetterSound letterSoundCorrespondence : readAllOrderedByUsage()) {
String lettersAsString = letterSoundCorrespondence.getLetters().stream().map(Letter::getText).collect(Collectors.joining());
String soundsAsString = letterSoundCorrespondence.getSounds().stream().map(Sound::getValueIpa).collect(Collectors.joining());
if (lettersAsString.equals(letterSoundCorrespondenceLetters) && soundsAsString.equals(letterSoundCorrespondenceSounds)) {
Expand All @@ -28,19 +29,19 @@ public LetterSoundCorrespondence read(List<Letter> letters, List<Sound> sounds)
}

@Override
public List<LetterSoundCorrespondence> readAllOrderedByUsage() throws DataAccessException {
public List<LetterSound> readAllOrderedByUsage() throws DataAccessException {
return em.createQuery(
"SELECT lsc " +
"FROM LetterSoundCorrespondence lsc " +
"FROM LetterSound lsc " +
"ORDER BY lsc.usageCount DESC")
.getResultList();
}

@Override
public List<LetterSoundCorrespondence> readAllOrderedByLettersLength() throws DataAccessException {
public List<LetterSound> readAllOrderedByLettersLength() throws DataAccessException {
return em.createQuery(
"SELECT lsc " +
"FROM LetterSoundCorrespondence lsc " +
"FROM LetterSound lsc " +
"ORDER BY lsc.letters.size DESC, lsc.usageCount DESC")
.getResultList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ai.elimu.dao.jpa;

import ai.elimu.dao.LetterSoundPeerReviewEventDao;
import ai.elimu.model.content.LetterSoundCorrespondence;
import ai.elimu.model.content.LetterSound;
import ai.elimu.model.contributor.Contributor;
import ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent;
import ai.elimu.model.contributor.LetterSoundCorrespondencePeerReviewEvent;
Expand All @@ -24,7 +24,7 @@ public List<LetterSoundCorrespondencePeerReviewEvent> readAll(LetterSoundCorresp
}

@Override
public List<LetterSoundCorrespondencePeerReviewEvent> readAll(LetterSoundCorrespondence letterSound) throws DataAccessException {
public List<LetterSoundCorrespondencePeerReviewEvent> readAll(LetterSound letterSound) throws DataAccessException {
return em.createQuery(
"SELECT event " +
"FROM LetterSoundCorrespondencePeerReviewEvent event " +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package ai.elimu.logic.converters;

import org.apache.commons.lang.StringUtils;
import ai.elimu.model.content.LetterSoundCorrespondence;
import ai.elimu.model.content.LetterSound;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import ai.elimu.dao.LetterSoundDao;

public class StringToLetterSoundCorrespondenceConverter implements Converter<String, LetterSoundCorrespondence> {
public class StringToLetterSoundCorrespondenceConverter implements Converter<String, LetterSound> {

@Autowired
private LetterSoundDao letterSoundDao;

/**
* Convert LetterSoundCorrespondence id to LetterSoundCorrespondence entity
* Convert LetterSound id to LetterSound entity
*/
public LetterSoundCorrespondence convert(String id) {
public LetterSound convert(String id) {
if (StringUtils.isBlank(id)) {
return null;
} else {
Long letterSoundCorrespondenceId = Long.parseLong(id);
LetterSoundCorrespondence letterSoundCorrespondence = letterSoundDao.read(letterSoundCorrespondenceId);
LetterSound letterSoundCorrespondence = letterSoundDao.read(letterSoundCorrespondenceId);
return letterSoundCorrespondence;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.OrderColumn;
import org.hibernate.validator.constraints.NotEmpty;

/**
* Contains information about the various sounds a letter (or letter combination) can represent.
*/
@Entity
public class LetterSoundCorrespondence extends Content {
public class LetterSound extends Content {

// @NotEmpty
@OrderColumn
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ai/elimu/model/content/Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Word extends Content {
@NotEmpty
@OrderColumn
@ManyToMany(fetch = FetchType.EAGER)
private List<LetterSoundCorrespondence> letterSoundCorrespondences;
private List<LetterSound> letterSoundCorrespondences;

/**
* As an example, the verb "reading" will be linked to the root verb "read".
Expand All @@ -48,11 +48,11 @@ public void setText(String text) {
this.text = text;
}

public List<LetterSoundCorrespondence> getLetterSoundCorrespondences() {
public List<LetterSound> getLetterSoundCorrespondences() {
return letterSoundCorrespondences;
}

public void setLetterSoundCorrespondences(List<LetterSoundCorrespondence> letterSoundCorrespondences) {
public void setLetterSoundCorrespondences(List<LetterSound> letterSoundCorrespondences) {
this.letterSoundCorrespondences = letterSoundCorrespondences;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ai.elimu.model.contributor;

import ai.elimu.model.content.LetterSoundCorrespondence;
import ai.elimu.model.content.LetterSound;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
Expand All @@ -10,13 +11,13 @@ public class LetterSoundCorrespondenceContributionEvent extends ContributionEven

@NotNull
@ManyToOne
private LetterSoundCorrespondence letterSoundCorrespondence;
private LetterSound letterSoundCorrespondence;

public LetterSoundCorrespondence getLetterSoundCorrespondence() {
public LetterSound getLetterSoundCorrespondence() {
return letterSoundCorrespondence;
}

public void setLetterSoundCorrespondence(LetterSoundCorrespondence letterSoundCorrespondence) {
public void setLetterSoundCorrespondence(LetterSound letterSoundCorrespondence) {
this.letterSoundCorrespondence = letterSoundCorrespondence;
}
}
6 changes: 3 additions & 3 deletions src/main/java/ai/elimu/rest/v2/JpaToGsonConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ai.elimu.model.content.Sound;
import ai.elimu.model.content.Emoji;
import ai.elimu.model.content.Letter;
import ai.elimu.model.content.LetterSoundCorrespondence;
import ai.elimu.model.content.LetterSound;
import ai.elimu.model.content.StoryBook;
import ai.elimu.model.content.StoryBookChapter;
import ai.elimu.model.content.StoryBookParagraph;
Expand Down Expand Up @@ -82,7 +82,7 @@ public static SoundGson getSoundGson(Sound sound) {
}
}

public static LetterSoundGson getLetterSoundGson(LetterSoundCorrespondence letterSound) {
public static LetterSoundGson getLetterSoundGson(LetterSound letterSound) {
if (letterSound == null) {
return null;
} else {
Expand Down Expand Up @@ -129,7 +129,7 @@ public static WordGson getWordGson(Word word) {
// Word
wordGson.setText(word.getText());
List<LetterSoundGson> letterSounds = new ArrayList<>();
for (LetterSoundCorrespondence letterSound : word.getLetterSoundCorrespondences()) {
for (LetterSound letterSound : word.getLetterSoundCorrespondences()) {
LetterSoundGson letterSoundGson = getLetterSoundGson(letterSound);
letterSounds.add(letterSoundGson);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ai.elimu.rest.v2.content;

import ai.elimu.model.content.LetterSoundCorrespondence;
import ai.elimu.model.content.LetterSound;
import ai.elimu.model.v2.gson.content.LetterSoundGson;
import ai.elimu.rest.v2.JpaToGsonConverter;
import com.google.gson.Gson;
Expand Down Expand Up @@ -29,7 +29,7 @@ public String handleGetRequest() {
logger.info("handleGetRequest");

JSONArray letterSoundsJsonArray = new JSONArray();
for (LetterSoundCorrespondence letterSound : letterSoundDao.readAllOrderedByUsage()) {
for (LetterSound letterSound : letterSoundDao.readAllOrderedByUsage()) {
LetterSoundGson letterSoundGson = JpaToGsonConverter.getLetterSoundGson(letterSound);
String json = new Gson().toJson(letterSoundGson);
letterSoundsJsonArray.put(new JSONObject(json));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ai.elimu.model.content.LetterSound;
import org.apache.logging.log4j.Logger;
import ai.elimu.dao.WordDao;
import ai.elimu.model.content.Sound;
import ai.elimu.model.content.Letter;
import ai.elimu.model.content.LetterSoundCorrespondence;
import ai.elimu.model.content.Word;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -40,14 +41,14 @@ public synchronized void execute() {
logger.info("words.size(): " + words.size());
for (Word word : words) {
logger.info("word.getText(): " + word.getText());
for (LetterSoundCorrespondence letterSoundCorrespondence : word.getLetterSoundCorrespondences()) {
for (LetterSound letterSoundCorrespondence : word.getLetterSoundCorrespondences()) {
letterSoundCorrespondenceFrequencyMap.put(letterSoundCorrespondence.getId(),
letterSoundCorrespondenceFrequencyMap.getOrDefault(letterSoundCorrespondence.getId(), 0) + word.getUsageCount());
}
}

// Update the values previously stored in the database
for (LetterSoundCorrespondence letterSoundCorrespondence : letterSoundDao.readAll()) {
for (LetterSound letterSoundCorrespondence : letterSoundDao.readAll()) {
logger.info("letterSoundCorrespondence.getId(): " + letterSoundCorrespondence.getId());
logger.info("letterSoundCorrespondence Letters: \"" + letterSoundCorrespondence.getLetters().stream().map(Letter::getText).collect(Collectors.joining()) + "\"");
logger.info("letterSoundCorrespondence Sounds: /" + letterSoundCorrespondence.getSounds().stream().map(Sound::getValueIpa).collect(Collectors.joining()) + "/");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ai/elimu/tasks/SoundUsageCountScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.apache.logging.log4j.Logger;
import ai.elimu.dao.WordDao;
import ai.elimu.model.content.Sound;
import ai.elimu.model.content.LetterSoundCorrespondence;
import ai.elimu.model.content.LetterSound;
import ai.elimu.model.content.Word;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -42,12 +42,12 @@ public synchronized void execute() {
// Integer = Usage count
Map<Long, Integer> soundFrequencyMap = new HashMap<>();

// Summarize the usage count of each Word's Sounds based on the LetterSoundCorrespondence's
// Summarize the usage count of each Word's Sounds based on the LetterSound's
// usage count (see LetterSoundCorrespondenceUsageCountScheduler).
List<Word> words = wordDao.readAllOrdered();
logger.info("words.size(): " + words.size());
for (Word word : words) {
for (LetterSoundCorrespondence letterSoundCorrespondence : word.getLetterSoundCorrespondences()) {
for (LetterSound letterSoundCorrespondence : word.getLetterSoundCorrespondences()) {
for (Sound sound : letterSoundCorrespondence.getSounds()) {
soundFrequencyMap.put(sound.getId(), soundFrequencyMap.getOrDefault(sound.getId(), 0) + letterSoundCorrespondence.getUsageCount());
}
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/ai/elimu/util/csv/CsvContentExtractionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
import ai.elimu.dao.LetterSoundDao;
import ai.elimu.dao.SoundDao;
import ai.elimu.dao.WordDao;
import ai.elimu.model.content.Emoji;
import ai.elimu.model.content.Letter;
import ai.elimu.model.content.LetterSoundCorrespondence;
import ai.elimu.model.content.*;
import ai.elimu.model.content.LetterSound;
import ai.elimu.model.content.Number;
import ai.elimu.model.content.Sound;
import ai.elimu.model.content.Word;
import ai.elimu.model.content.multimedia.Image;
import ai.elimu.model.enums.ContentLicense;
import ai.elimu.model.v2.enums.ReadingLevel;
Expand Down Expand Up @@ -56,10 +53,10 @@ public class CsvContentExtractionHelper {
/**
* For information on how the CSV files were generated, see {@link LetterSoundCsvExportController#handleRequest}.
*/
public static List<LetterSoundCorrespondence> getLetterSoundCorrespondencesFromCsvBackup(File csvFile, LetterDao letterDao, SoundDao soundDao, LetterSoundDao letterSoundDao) {
public static List<LetterSound> getLetterSoundCorrespondencesFromCsvBackup(File csvFile, LetterDao letterDao, SoundDao soundDao, LetterSoundDao letterSoundDao) {
logger.info("getLetterSoundCorrespondencesFromCsvBackup");

List<LetterSoundCorrespondence> letterSoundCorrespondences = new ArrayList<>();
List<LetterSound> letterSoundCorrespondences = new ArrayList<>();

Path csvFilePath = Paths.get(csvFile.toURI());
logger.info("csvFilePath: " + csvFilePath);
Expand All @@ -79,7 +76,7 @@ public static List<LetterSoundCorrespondence> getLetterSoundCorrespondencesFromC
for (CSVRecord csvRecord : csvParser) {
logger.info("csvRecord: " + csvRecord);

LetterSoundCorrespondence letterSoundCorrespondence = new LetterSoundCorrespondence();
LetterSound letterSoundCorrespondence = new LetterSound();

JSONArray letterIdsJsonArray = new JSONArray(csvRecord.get("letter_ids"));
logger.info("letterIdsJsonArray: " + letterIdsJsonArray);
Expand Down Expand Up @@ -158,7 +155,7 @@ public static List<Word> getWordsFromCsvBackup(File csvFile, LetterDao letterDao

JSONArray letterSoundCorrespondencesJsonArray = new JSONArray(csvRecord.get("letter_sound_correspondences"));
logger.info("letterSoundCorrespondencesJsonArray: " + letterSoundCorrespondencesJsonArray);
List<LetterSoundCorrespondence> letterSoundCorrespondences = new ArrayList<>();
List<LetterSound> letterSoundCorrespondences = new ArrayList<>();
for (int i = 0; i < letterSoundCorrespondencesJsonArray.length(); i++) {
JSONObject letterSoundCorrespondenceJsonObject = letterSoundCorrespondencesJsonArray.getJSONObject(i);
logger.info("letterSoundCorrespondenceJsonObject: " + letterSoundCorrespondenceJsonObject);
Expand All @@ -174,7 +171,7 @@ public static List<Word> getWordsFromCsvBackup(File csvFile, LetterDao letterDao
Sound sound = soundDao.readByValueIpa(soundsJsonArray.getString(j));
sounds.add(sound);
}
LetterSoundCorrespondence letterSoundCorrespondence = letterSoundDao.read(letters, sounds);
LetterSound letterSoundCorrespondence = letterSoundDao.read(letters, sounds);
logger.info("letterSoundCorrespondence.getId(): " + letterSoundCorrespondence.getId());
letterSoundCorrespondences.add(letterSoundCorrespondence);
}
Expand Down
Loading

0 comments on commit 6e9c8b4

Please sign in to comment.