Skip to content

Commit

Permalink
TranslatorsAmalgamated: Improve handling of shifts
Browse files Browse the repository at this point in the history
When a word with original index `m` is shifted by `±n`, position it
*after* the word with index `m±n`. If multiple words end up after the
same index, sort them by original index.
  • Loading branch information
schierlm committed Nov 6, 2024
1 parent 8e1ede4 commit 0ad40c6
Showing 1 changed file with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -254,6 +255,7 @@ public Bible doImport(File directory) throws Exception {
wi.attributes.add(new String[] { "tahot", "english", "gloss", lemmaGloss[1] });
wi.strongs = strong;
wi.rmac = morph;
wi.order = Integer.parseInt(fields[0].split("[#=]")[1]);

String[] editions = fields[5].split("\\+");
wi.editions = EnumSet.noneOf(Edition.class);
Expand All @@ -263,17 +265,15 @@ public Bible doImport(File directory) throws Exception {
String[] parts = edition.split("»");
Edition edi = getEdition(parts[0]);
WordItem wii = new WordItem(wi, edi);
wii.shift = parts[1].contains(".") ? 0 : Integer.parseInt(parts[1]);
int index = items.size();
while (wii.shift < 0 && index > 0) {
if (items.get(index - 1).editions.contains(edi)) {
wii.shift++;
}
index--;
int shift = parts[1].contains(".") ? 0 : Integer.parseInt(parts[1]);
if (shift != 0) {
wii.unshiftedOrder = wi.order;
wii.order = wi.order + shift;
}
if (wii.shift < 0) {
int index = items.size();
if (wii.order < 1) {
System.out.println("WARNING: Shifted word left out of verse: " + currVersePrefix);
wii.shift = 0;
wii.order = 1;
}
items.add(index, wii);
} else {
Expand Down Expand Up @@ -301,26 +301,20 @@ private Edition getEdition(String name) {

private void writeGreek(Verse verse, List<WordItem> items, String currVersePrefix) {
Visitor<RuntimeException> vv = verse.getAppendVisitor();
boolean first = true;
for (int i = 0; i < items.size(); i++) {
WordItem wi = items.get(i);
if (wi.shift > 0) {
int j = i;
while (wi.shift > 0 && j < items.size() - 1) {
WordItem next = items.get(j + 1);
items.set(j, next);
j++;
if (next.editions.containsAll(wi.editions))
wi.shift--;
}
if (wi.shift > 0) {
boolean needSort = false;
for (WordItem wi : items) {
if (wi.unshiftedOrder != 0) {
needSort = true;
if (wi.order > items.size()) {
System.out.println("WARNING: Shifted word right out of verse: " + currVersePrefix);
wi.shift = 0;
wi.order = items.size();
}
items.set(j, wi);
i--;
}
}
if (needSort) {
items.sort(Comparator.<WordItem, Integer> comparing(wi -> wi.order).thenComparing(wi -> wi.unshiftedOrder));
}
boolean first = true;
for (WordItem wi : items) {
if (!first)
vv.visitText(" ");
Expand Down Expand Up @@ -375,7 +369,7 @@ private static class WordItem {
EnumSet<Edition> editions;
String[] strongs;
String[] rmac;
int shift = 0;
int order, unshiftedOrder = 0;

public WordItem() {
}
Expand Down

0 comments on commit 0ad40c6

Please sign in to comment.