Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(dao): Rename letterSoundCorrespondences #1713

Merged
merged 8 commits into from
Jul 13, 2024
10 changes: 5 additions & 5 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<LetterSoundCorrespondence> letterSounds;

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

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

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

public Word getRootWord() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ai/elimu/rest/v2/JpaToGsonConverter.java
Original file line number Diff line number Diff line change
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 (LetterSoundCorrespondence letterSound : word.getLetterSounds()) {
LetterSoundGson letterSoundGson = getLetterSoundGson(letterSound);
letterSounds.add(letterSoundGson);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ 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()) {
letterSoundCorrespondenceFrequencyMap.put(letterSoundCorrespondence.getId(),
letterSoundCorrespondenceFrequencyMap.getOrDefault(letterSoundCorrespondence.getId(), 0) + word.getUsageCount());
for (LetterSoundCorrespondence letterSound : word.getLetterSounds()) {
letterSoundCorrespondenceFrequencyMap.put(letterSound.getId(),
letterSoundCorrespondenceFrequencyMap.getOrDefault(letterSound.getId(), 0) + word.getUsageCount());
}
}

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 @@ -47,9 +47,9 @@ public synchronized void execute() {
List<Word> words = wordDao.readAllOrdered();
logger.info("words.size(): " + words.size());
for (Word word : words) {
for (LetterSoundCorrespondence letterSoundCorrespondence : word.getLetterSoundCorrespondences()) {
for (Sound sound : letterSoundCorrespondence.getSounds()) {
soundFrequencyMap.put(sound.getId(), soundFrequencyMap.getOrDefault(sound.getId(), 0) + letterSoundCorrespondence.getUsageCount());
for (LetterSoundCorrespondence letterSound : word.getLetterSounds()) {
for (Sound sound : letterSound.getSounds()) {
soundFrequencyMap.put(sound.getId(), soundFrequencyMap.getOrDefault(sound.getId(), 0) + letterSound.getUsageCount());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public synchronized void execute() {
existingWord.setUsageCount(wordFrequencyMap.get(word));

// Temporary fix for "javax.validation.ConstraintViolationException"
if (existingWord.getLetterSoundCorrespondences().isEmpty()) {
if (existingWord.getLetterSounds().isEmpty()) {
logger.warn("Letter-sound correspondences not yet added. Skipping usage count update for word...");
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,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<LetterSoundCorrespondence> letterSounds = new ArrayList<>();
for (int i = 0; i < letterSoundCorrespondencesJsonArray.length(); i++) {
JSONObject letterSoundCorrespondenceJsonObject = letterSoundCorrespondencesJsonArray.getJSONObject(i);
logger.info("letterSoundCorrespondenceJsonObject: " + letterSoundCorrespondenceJsonObject);
Expand All @@ -176,9 +176,9 @@ public static List<Word> getWordsFromCsvBackup(File csvFile, LetterDao letterDao
}
LetterSoundCorrespondence letterSoundCorrespondence = letterSoundDao.read(letters, sounds);
logger.info("letterSoundCorrespondence.getId(): " + letterSoundCorrespondence.getId());
letterSoundCorrespondences.add(letterSoundCorrespondence);
letterSounds.add(letterSoundCorrespondence);
}
word.setLetterSoundCorrespondences(letterSoundCorrespondences);
word.setLetterSounds(letterSounds);

Integer usageCount = Integer.valueOf(csvRecord.get("usage_count"));
word.setUsageCount(usageCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,6 @@ private void autoSelectLetterSoundCorrespondences(Word word) {
}
}

word.setLetterSoundCorrespondences(letterSoundCorrespondences);
word.setLetterSounds(letterSoundCorrespondences);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ public void handleRequest(

JSONArray letterSoundCorrespondencesJsonArray = new JSONArray();
int index = 0;
for (LetterSoundCorrespondence letterSoundCorrespondence : word.getLetterSoundCorrespondences()) {
for (LetterSoundCorrespondence letterSound : word.getLetterSounds()) {
JSONObject letterSoundCorrespondenceJsonObject = new JSONObject();
letterSoundCorrespondenceJsonObject.put("id", letterSoundCorrespondence.getId());
String[] lettersArray = new String[letterSoundCorrespondence.getLetters().size()];
letterSoundCorrespondenceJsonObject.put("id", letterSound.getId());
String[] lettersArray = new String[letterSound.getLetters().size()];
for (int i = 0; i < lettersArray.length; i++) {
lettersArray[i] = letterSoundCorrespondence.getLetters().get(i).getText();
lettersArray[i] = letterSound.getLetters().get(i).getText();
}
letterSoundCorrespondenceJsonObject.put("letters", lettersArray);
String[] soundsArray = new String[letterSoundCorrespondence.getSounds().size()];
String[] soundsArray = new String[letterSound.getSounds().size()];
for (int i = 0; i < soundsArray.length; i++) {
soundsArray[i] = letterSoundCorrespondence.getSounds().get(i).getValueIpa();
soundsArray[i] = letterSound.getSounds().get(i).getValueIpa();
}
letterSoundCorrespondenceJsonObject.put("sounds", soundsArray);
letterSoundCorrespondenceJsonObject.put("usageCount", letterSoundCorrespondence.getUsageCount());
letterSoundCorrespondenceJsonObject.put("usageCount", letterSound.getUsageCount());
letterSoundCorrespondencesJsonArray.put(index, letterSoundCorrespondenceJsonObject);
index++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public String handleRequest(

Word word = wordDao.read(id);

if (word.getLetterSoundCorrespondences().isEmpty()) {
if (word.getLetterSounds().isEmpty()) {
autoSelectLetterSoundCorrespondences(word);
// TODO: display information message to the Contributor that the letter-sound correspondences were auto-selected, and that they should be verified
}
Expand Down Expand Up @@ -232,7 +232,7 @@ private void autoSelectLetterSoundCorrespondences(Word word) {

String wordText = word.getText();

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

List<LetterSoundCorrespondence> allLetterSoundCorrespondencesOrderedByLettersLength = letterSoundDao.readAllOrderedByLettersLength();
while (StringUtils.isNotBlank(wordText)) {
Expand All @@ -246,7 +246,7 @@ private void autoSelectLetterSoundCorrespondences(Word word) {
if (wordText.startsWith(letterSoundCorrespondenceLetters)) {
isMatch = true;
logger.info("Found match at the beginning of \"" + wordText + "\"");
letterSoundCorrespondences.add(letterSoundCorrespondence);
letterSounds.add(letterSoundCorrespondence);

// Remove the match from the word
wordText = wordText.substring(letterSoundCorrespondenceLetters.length());
Expand All @@ -260,6 +260,6 @@ private void autoSelectLetterSoundCorrespondences(Word word) {
}
}

word.setLetterSoundCorrespondences(letterSoundCorrespondences);
word.setLetterSounds(letterSounds);
}
}
10 changes: 5 additions & 5 deletions src/main/resources/META-INF/jpa-schema-export.sql
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,9 @@

create table Word_LetterSoundCorrespondence (
Word_id bigint not null,
letterSoundCorrespondences_id bigint not null,
letterSoundCorrespondences_ORDER integer not null,
primary key (Word_id, letterSoundCorrespondences_ORDER)
letterSounds_id bigint not null,
letterSounds_ORDER integer not null,
primary key (Word_id, letterSounds_ORDER)
) engine=MyISAM;

create table WordContributionEvent (
Expand Down Expand Up @@ -1078,8 +1078,8 @@
references Word (id);

alter table Word_LetterSoundCorrespondence
add constraint FKf6r3yfdc6quwa0b13mln5uuc8
foreign key (letterSoundCorrespondences_id)
Comment on lines -1081 to -1082
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a mismatch between the jpa-schema-export.sql and the production database:

MariaDB [webapp-HIN]> SHOW CREATE TABLE Word_LetterSoundCorrespondence;
| Word_LetterSoundCorrespondence | CREATE TABLE `Word_LetterSoundCorrespondence` (
  `Word_id` bigint(20) NOT NULL,
  `letterSoundCorrespondences_id` bigint(20) NOT NULL,
  `letterSoundCorrespondences_ORDER` int(11) NOT NULL,
  PRIMARY KEY (`Word_id`,`letterSoundCorrespondences_ORDER`),
  KEY `FK_aegghkhls25m6pr0e42f1csvi` (`letterSoundCorrespondences_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign key remained the same after the DB migration:

MariaDB [webapp-HIN]> SHOW CREATE TABLE Word_LetterSoundCorrespondence;
| Word_LetterSoundCorrespondence | CREATE TABLE `Word_LetterSoundCorrespondence` (
  `Word_id` bigint(20) NOT NULL,
  `letterSounds_id` bigint(20) NOT NULL,
  `letterSounds_ORDER` int(11) NOT NULL,
  PRIMARY KEY (`Word_id`,`letterSounds_ORDER`),
  KEY `FK_aegghkhls25m6pr0e42f1csvi` (`letterSounds_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the DROP FOREIGN KEY from the migration script: aafe7dd

add constraint FK1ln49ylh4w15nddf9h41wjupt
foreign key (letterSounds_id)
references LetterSoundCorrespondence (id);

alter table Word_LetterSoundCorrespondence
Expand Down
15 changes: 15 additions & 0 deletions src/main/resources/db/migration/2004006.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 2.4.6

# Word "letterSoundCorrespondences" → Word "letterSounds"

ALTER TABLE `Word_LetterSoundCorrespondence` DROP COLUMN `letterSounds_id`;
ALTER TABLE `Word_LetterSoundCorrespondence` CHANGE `letterSoundCorrespondences_id` `letterSounds_id` bigint(20) NOT NULL;

ALTER TABLE `Word_LetterSoundCorrespondence` DROP COLUMN `letterSounds_ORDER`;
ALTER TABLE `Word_LetterSoundCorrespondence` CHANGE `letterSoundCorrespondences_ORDER` `letterSounds_ORDER` int(11) NOT NULL;

# Reset primary key
ALTER TABLE `Word_LetterSoundCorrespondence` DROP PRIMARY KEY, ADD PRIMARY KEY(Word_id, letterSounds_ORDER);

# Delete obsolete foreign key constraint for "letterSoundCorrespondences_id"
ALTER TABLE `Word_LetterSoundCorrespondence` DROP FOREIGN KEY `FKf6r3yfdc6quwa0b13mln5uuc8`;