Skip to content

Commit

Permalink
Add serialization to Translation tips
Browse files Browse the repository at this point in the history
  • Loading branch information
patricksptang committed Nov 18, 2024
1 parent 21f48fc commit 92914f1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private void loadData() {
this.totalProgress += 1;
loadAugmentedStrongs(true);
this.totalProgress += 1;
loadTranslationTips();
loadTranslationTips(true);
LOGGER.info("Finished loading...");
}

Expand All @@ -225,10 +225,12 @@ public void loadAugmentedStrongs(boolean loadAugmentedFile) {
else this.strongAugmentationService.loadFromSerialization(appManager.getStepInstallFile().toString());
}

public void loadTranslationTips() {
public void loadTranslationTips(boolean loadTranslationTips) {
LOGGER.debug("Indexing translation tips");
String translationTipsPath = this.coreProperties.getProperty("test.data.path.translationtips");
this.translationTipsService.readAndLoad(translationTipsPath);
String installFile = appManager.getStepInstallFile().toString();
if (loadTranslationTips) this.translationTipsService.readAndLoad(translationTipsPath, installFile);
else this.translationTipsService.loadFromSerialization(appManager.getStepInstallFile().toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
import org.crosswire.jsword.passage.RocketPassage;
import org.crosswire.jsword.versification.system.Versifications;

import java.io.Serializable;
import java.util.HashMap;

/**
* Given a strong number, we find the augmented version in order to provide more accurate definitions and context
*/
public interface TranslationTipsService {

void readAndLoad(final String translationTipsPath);
void readAndLoad(final String translationTipsPath, final String installFile);
void loadFromSerialization(final String installFilePath);

public static class TranslationTips implements Serializable {
public BitwisePassage regularFormatedFN = new RocketPassage(Versifications.instance().getVersification("NRSV"));
public BitwisePassage alternativeFormatedFN = new RocketPassage(Versifications.instance().getVersification("NRSV"));
public HashMap<Integer, String> customFN = new HashMap<Integer, String>();

public static BitwisePassage regularFormatedFN = new RocketPassage(Versifications.instance().getVersification("NRSV"));
public static BitwisePassage alternativeFormatedFN = new RocketPassage(Versifications.instance().getVersification("NRSV"));
public static HashMap<Integer, String> customFN = new HashMap<Integer, String>();
}

public static TranslationTips translationTips = new TranslationTips();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TranslationTipsServiceImpl implements TranslationTipsService {

private static final Logger LOGGER = LoggerFactory.getLogger(StrongAugmentationServiceImpl.class);

public void loadVersesToBitwise(String translationTipsPath) {
public void readAndLoad(final String translationTipsPath, final String installFilePath) {
Reader fileReader = null;
BufferedInputStream bufferedStream = null;
InputStream stream = null;
Expand Down Expand Up @@ -81,21 +81,21 @@ public void loadVersesToBitwise(String translationTipsPath) {
if (fileNameFromURL.length() < 1) continue;
int ordinal = key.getOrdinal();
if (fileNameFromURL.equals(bookName + "-" + chapter + verse)) {
if (regularFormatedFN.store.get(ordinal)) {
if (translationTips.regularFormatedFN.store.get(ordinal)) {
System.out.println("Duplicate definition regular: " + data + ", " + ordinal);
continue;
}
regularFormatedFN.addAll(keyf.getKey(Versifications.instance().getVersification("NRSV"), parts[0]));
translationTips.regularFormatedFN.addAll(keyf.getKey(Versifications.instance().getVersification("NRSV"), parts[0]));
}
else if (fileNameFromURL.equals(bookName + "-" + chapter + "-" + verse)) {
if (regularFormatedFN.store.get(ordinal)) {
if (translationTips.regularFormatedFN.store.get(ordinal)) {
System.out.println("Duplicate definition alternate: " + data + ", " + ordinal);
continue;
}
alternativeFormatedFN.addAll(keyf.getKey(Versifications.instance().getVersification("NRSV"), parts[0]));
translationTips.alternativeFormatedFN.addAll(keyf.getKey(Versifications.instance().getVersification("NRSV"), parts[0]));
}
else {
customFN.put(ordinal, fileNameFromURL);
translationTips.customFN.put(ordinal, fileNameFromURL);
}
}
catch (Exception e) {
Expand All @@ -108,13 +108,54 @@ else if (fileNameFromURL.equals(bookName + "-" + chapter + "-" + verse)) {
LOGGER.error("Unable to read a line from the translation tip file");
throw new StepInternalException("Unable to read a line from the translation tip file ", e);
}
// Output to serialized data
String installFileFolder = "";
int pos = installFilePath.lastIndexOf('\\');
if (pos == -1)
pos = installFilePath.lastIndexOf('/');
if (pos > 1)
installFileFolder = installFilePath.substring(0, pos+1);
try {
FileOutputStream fileOut =
new FileOutputStream(installFileFolder + "translator_tips.dat");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(translationTips);
out.close();
fileOut.close();
LOGGER.info("Serialized data is saved in " + installFileFolder + "translator_tips.dat");
} catch (IOException i) {
LOGGER.error("Serialized data cannot be saved in " + installFileFolder + "translator_tips.dat");
i.printStackTrace();
}

} finally {
closeQuietly(fileReader);
closeQuietly(bufferedStream);
closeQuietly(stream);
}
}
public void readAndLoad(final String translationTipsPath) {
loadVersesToBitwise(translationTipsPath);

public void loadFromSerialization(final String installFilePath) {
String installFileFolder = "";
int pos = installFilePath.lastIndexOf('\\');
if (pos == -1)
pos = installFilePath.lastIndexOf('/');
if (pos > 1)
installFileFolder = installFilePath.substring(0, pos+1);
try {
FileInputStream fileIn = new FileInputStream(installFileFolder + "translator_tips.dat");
ObjectInputStream in = new ObjectInputStream(fileIn);
TranslationTips tipsReadIn = (TranslationTips) in.readObject();
translationTips.regularFormatedFN = tipsReadIn.regularFormatedFN;
translationTips.alternativeFormatedFN = tipsReadIn.alternativeFormatedFN;
translationTips.customFN = tipsReadIn.customFN;
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("augmented strong class not found");
c.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
import java.util.*;
import java.util.Map.Entry;

import static com.tyndalehouse.step.core.service.TranslationTipsService.regularFormatedFN;
import static com.tyndalehouse.step.core.service.TranslationTipsService.alternativeFormatedFN;
import static com.tyndalehouse.step.core.service.TranslationTipsService.customFN;
import static com.tyndalehouse.step.core.service.TranslationTipsService.*;

/**
* Provides each strong number given a verse.
* <p/>
Expand Down Expand Up @@ -120,12 +119,12 @@ private void calculateCounts(String userLanguage) {
}
else targetVersification = ntV11n;
int curOrdinal = verseInNRSV.getOrdinal();
if (regularFormatedFN.store.get(curOrdinal))
if (translationTips.regularFormatedFN.store.get(curOrdinal))
this.translationTipsFN = verseInNRSV.getBook().toString().toLowerCase() + "-" + verseInNRSV.getChapter() + verseInNRSV.getVerse();
else if (alternativeFormatedFN.store.get(curOrdinal))
else if (translationTips.alternativeFormatedFN.store.get(curOrdinal))
this.translationTipsFN = verseInNRSV.getBook().toString().toLowerCase() + "-" + verseInNRSV.getChapter() + "-" + verseInNRSV.getVerse();
else if (customFN.containsKey(curOrdinal))
this.translationTipsFN = customFN.get(curOrdinal);
else if (translationTips.customFN.containsKey(curOrdinal))
this.translationTipsFN = translationTips.customFN.get(curOrdinal);
else
this.translationTipsFN = ""; // If there are no tips, it will be an empty string
final Key key = VersificationsMapper.instance().mapVerse(curReference, targetVersification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ public void contextInitialized(final ServletContextEvent servletContextEvent) {
if (Boolean.getBoolean("step.loader")) {
getInjector().getInstance(Loader.class).init();
}
else getInjector().getInstance(Loader.class).loadAugmentedStrongs(false);
else {
getInjector().getInstance(Loader.class).loadAugmentedStrongs(false);
getInjector().getInstance(Loader.class).loadTranslationTips(false);
}

}

Expand Down

0 comments on commit 92914f1

Please sign in to comment.