use of biblemulticonverter.data.Versification.Reference in project BibleMultiConverter by schierlm.
the class VersificationTool method run.
@Override
public void run(String... args) throws Exception {
File dbFile = new File(args[0]);
VersificationSet vs = dbFile.exists() ? new VersificationSet(dbFile) : new VersificationSet();
boolean save = false;
switch(args[1]) {
case "list":
if (args.length == 2) {
for (Versification v : vs.getVersifications()) printInfo(v);
Map<String, Integer> mappingCounts = new HashMap<>();
for (VersificationMapping m : vs.getMappings()) {
String namePrefix = m.getFrom().getName() + "/" + m.getTo().getName() + "/";
printInfo(namePrefix + countOccurrence(mappingCounts, namePrefix), m);
}
} else {
for (int i = 2; i < args.length; i++) {
if (args[i].contains("/"))
printInfo(args[i], vs.findMapping(args[i]));
else
printInfo(vs.findVersification(args[i]));
}
}
break;
case "import":
Module<VersificationFormat> importModule = Main.versificationFormats.get(args[2]);
importModule.getImplementationClass().newInstance().doImport(vs, Arrays.copyOfRange(args, 3, args.length));
save = true;
break;
case "export":
Module<VersificationFormat> exportModule = Main.versificationFormats.get(args[2]);
List<Versification> vv = args.length == 4 ? vs.getVersifications() : new ArrayList<Versification>();
List<VersificationMapping> mm = args.length == 4 ? vs.getMappings() : new ArrayList<VersificationMapping>();
for (int i = 4; i < args.length; i++) {
if (args[i].contains("/"))
mm.add(vs.findMapping(args[i]));
else
vv.add(vs.findVersification(args[i]));
}
exportModule.getImplementationClass().newInstance().doExport(new File(args[3]), vv, mm);
break;
case "rename":
vs.findVersification(args[2]).setName(args[3]);
save = true;
break;
case "remove":
List<Versification> vvv = new ArrayList<>();
List<VersificationMapping> mmm = new ArrayList<>();
for (int i = 2; i < args.length; i++) {
if (args[i].contains("/"))
mmm.add(vs.findMapping(args[i]));
else
vvv.add(vs.findVersification(args[i]));
}
for (Versification v : vvv) {
vs.getVersifications().remove(v);
}
for (VersificationMapping m : mmm) vs.getMappings().remove(m);
save = true;
break;
case "join":
VersificationMapping m1 = vs.findMapping(args[2]);
for (int i = 3; i < args.length; i++) {
VersificationMapping m2 = vs.findMapping(args[i]);
if (m1.getTo() != m2.getFrom())
throw new IllegalStateException("Cannot join, versification mismatch: " + m1.getTo().getName() + " != " + m2.getFrom().getName());
Map<Reference, List<Reference>> map = new HashMap<>();
for (int j = 0; j < m1.getFrom().getVerseCount(); j++) {
Reference r1 = m1.getFrom().getReference(j);
List<Reference> r3 = new ArrayList<>();
for (Reference r2 : m1.getMapping(r1)) {
r3.addAll(m2.getMapping(r2));
}
for (int k = 0; k < r3.size() - 1; k++) {
if (r3.get(k).equals(r3.get(k + 1))) {
r3.remove(k);
k--;
}
}
if (!r3.isEmpty())
map.put(r1, r3);
}
m1 = VersificationMapping.build(m1.getFrom(), m2.getTo(), map);
}
vs.getMappings().add(m1);
save = true;
break;
case "compare":
if (args[2].contains("/")) {
VersificationMapping vm1 = vs.findMapping(args[2]);
VersificationMapping vm2 = vs.findMapping(args[3]);
if (vm1.getFrom() != vm2.getFrom() || vm1.getTo() != vm2.getTo())
throw new IllegalArgumentException("Versification mappings need to have same source/destination versification to be compared");
EnumMap<MappingVerseCompareVariant, Integer> variants = new EnumMap<>(MappingVerseCompareVariant.class);
for (int i = 0; i < vm1.getFrom().getVerseCount(); i++) {
Reference r = vm1.getFrom().getReference(i);
MappingVerseCompareVariant cv = MappingVerseCompareVariant.get(vm1.getMapping(r), vm2.getMapping(r));
variants.put(cv, variants.containsKey(cv) ? variants.get(cv) + 1 : 1);
}
for (Map.Entry<MappingVerseCompareVariant, Integer> entry : variants.entrySet()) {
System.out.println(entry.getKey().toString() + ": " + entry.getValue());
}
} else {
Versification v1 = vs.findVersification(args[2]);
Set<Reference> v1r = new HashSet<>();
for (int i = 0; i < v1.getVerseCount(); i++) v1r.add(v1.getReference(i));
Versification v2 = vs.findVersification(args[3]);
Set<Reference> v2r = new HashSet<>();
for (int i = 0; i < v2.getVerseCount(); i++) v2r.add(v2.getReference(i));
if (v1.getVerseCount() == v2.getVerseCount() && v1r.equals(v2r)) {
boolean sameOrder = true;
for (int i = 0; i < v1.getVerseCount(); i++) {
if (!v1.getReference(i).equals(v2.getReference(i))) {
sameOrder = false;
break;
}
}
System.out.println("Versifications contain same verses" + (sameOrder ? " in same order" : ""));
} else if (v1r.containsAll(v2r)) {
System.out.println("Right versification is a subset of left versification");
} else if (v2r.containsAll(v1r)) {
System.out.println("Left versification is a subset of right versification");
} else {
Set<Reference> intersect = new HashSet<Reference>();
intersect.addAll(v1r);
intersect.retainAll(v2r);
if (intersect.isEmpty())
System.out.println("Versifications are disjoint");
else
System.out.println("Versifications have " + intersect.size() + " verses in common");
}
}
break;
default:
System.out.println("Unsupported command: " + args[1]);
}
if (save) {
try (Writer w = new OutputStreamWriter(new FileOutputStream(dbFile), StandardCharsets.UTF_8)) {
vs.saveTo(w);
}
}
}
use of biblemulticonverter.data.Versification.Reference in project BibleMultiConverter by schierlm.
the class CCEL method doImport.
@Override
public void doImport(VersificationSet vset, String... importArgs) throws Exception {
JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller u = ctx.createUnmarshaller();
for (String filename : importArgs) {
File inputFile = new File(filename);
ValidateXML.validateFileBeforeParsing(getSchema(), inputFile);
RefSys doc = (RefSys) u.unmarshal(inputFile);
List<String> aliases = new ArrayList<>();
for (Alias alias : doc.getAlias()) {
aliases.add(alias.getCode());
}
List<Versification.Reference> refs = new ArrayList<>();
Set<Versification.Reference> refsSeen = new HashSet<>();
kingdoms = null;
for (OsisID osisid : doc.getOsisIDs().getOsisID()) {
Reference r = parseReference(osisid.getCode());
if (r != null) {
if (!refsSeen.add(r))
System.out.println("WARNING: Same verse referenced twice in versification: " + r);
else
refs.add(r);
}
}
if (kingdoms == null)
kingdoms = false;
if (kingdoms) {
aliases.add(doc.getCode() + "__-_KINGDOMS");
}
vset.getVersifications().add(Versification.fromReferenceList(doc.getCode(), doc.getName(), aliases.isEmpty() ? null : (String[]) aliases.toArray(new String[aliases.size()]), refs));
for (RefMap refmap : doc.getRefMap()) {
Versification from = vset.findVersification(refmap.getFrom());
Versification to = vset.findVersification(refmap.getTo());
System.out.println("-- " + from.getName() + " -> " + to.getName() + " --");
Map<BookID, BookID> bookMap = new EnumMap<>(BookID.class);
Map<Reference, Reference> chapterMap = new HashMap<>();
Map<Reference, List<Reference>> mapMap = new HashMap<>();
for (RefMap.Map map : refmap.getMap()) {
int partsCount = map.getFrom().split("\\.").length;
if (partsCount == 1) {
kingdoms = isKingdoms(from);
BookID ff = parseBook(map.getFrom());
kingdoms = isKingdoms(to);
;
BookID tt = map.getTo().isEmpty() ? BookID.DICTIONARY_ENTRY : parseBook(map.getTo());
if (ff == null || tt == null)
continue;
bookMap.put(ff, tt);
} else if (partsCount == 2) {
kingdoms = isKingdoms(from);
Reference ff = parseReference(map.getFrom() + ".1");
kingdoms = isKingdoms(to);
Reference tt = parseReference(map.getTo() + ".1");
if (ff == null || tt == null)
continue;
chapterMap.put(ff, tt);
} else {
kingdoms = isKingdoms(from);
Reference ff = parseReference(map.getFrom());
if (ff == null)
continue;
if (!from.containsReference(ff)) {
System.out.println("WARNING: Versification " + from.getName() + " does not contain verse " + ff + "; skipping");
continue;
}
if (map.getTo().isEmpty()) {
mapMap.put(ff, new ArrayList<Reference>());
continue;
}
kingdoms = isKingdoms(to);
List<Reference> list = new ArrayList<>();
if (map.getTo().contains(" ")) {
for (String rrr : map.getTo().split(" ")) {
Reference r1 = parseReference(rrr);
if (r1 == null)
continue;
if (!to.containsReference(r1)) {
System.out.println("WARNING: Verse " + r1 + " does not exist in " + to.getName() + "; skipping");
continue;
}
list.add(r1);
}
} else {
String[] parts = map.getTo().split("-", 2);
Reference r1 = parseReference(parts[0]);
if (r1 == null)
continue;
if (!to.containsReference(r1)) {
System.out.println("WARNING: Mapping maps to verse " + r1 + " not contained in destination mapping " + to.getName());
continue;
}
if (parts.length == 1) {
list.add(r1);
} else {
Reference r2 = parseReference(parts[1]);
int i1 = to.getIndexForReference(r1);
int i2 = to.getIndexForReference(r2);
for (int i = i1; i <= i2; i++) {
list.add(to.getReference(i));
}
}
}
mapMap.put(ff, list);
}
}
for (int i = 0; i < from.getVerseCount(); i++) {
final Reference ff = from.getReference(i), tt;
if (mapMap.containsKey(ff))
continue;
Reference chapterMapped = chapterMap.get(new Reference(ff.getBook(), ff.getChapter(), "1"));
if (chapterMapped != null) {
tt = new Reference(chapterMapped.getBook(), chapterMapped.getChapter(), ff.getVerse());
} else if (bookMap.containsKey(ff.getBook())) {
tt = new Reference(bookMap.get(ff.getBook()), ff.getChapter(), ff.getVerse());
} else {
tt = ff;
}
if (to.containsReference(tt)) {
List<Reference> list = new ArrayList<>();
list.add(tt);
mapMap.put(ff, list);
}
}
for (Iterator<Map.Entry<Reference, List<Reference>>> it = mapMap.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<Reference, List<Reference>> reference = it.next();
if (reference.getValue().isEmpty())
it.remove();
}
vset.getMappings().add(VersificationMapping.build(from, to, mapMap));
}
}
}
use of biblemulticonverter.data.Versification.Reference in project BibleMultiConverter by schierlm.
the class CCEL method doExport.
@Override
public void doExport(File outputFile, List<Versification> versifications, List<VersificationMapping> mappings) throws Exception {
if (versifications.size() != 1)
throw new IllegalArgumentException("CCEL files have to contain exactly one versification");
Versification versification = versifications.get(0);
ObjectFactory of = new ObjectFactory();
RefSys refsys = of.createRefSys();
kingdoms = false;
if (versification.getAliases() != null) {
for (String a : versification.getAliases()) {
if (a.equals(versification.getName() + "__-_KINGDOMS")) {
kingdoms = true;
} else {
Alias alias = of.createRefSysAlias();
alias.setCode(a);
refsys.getAlias().add(alias);
}
}
}
refsys.setCode(versification.getName());
if (versification.getDescription() != null)
refsys.setName(versification.getDescription());
refsys.setOsisIDs(of.createRefSysOsisIDs());
for (int i = 0; i < versification.getVerseCount(); i++) {
Reference r = versification.getReference(i);
OsisID oi = of.createRefSysOsisIDsOsisID();
oi.setCode(formatReference(r, kingdoms));
refsys.getOsisIDs().getOsisID().add(oi);
}
for (VersificationMapping vm : mappings) {
Map<BookID, List<List<Reference>>> destReferencesByBook = new EnumMap<>(BookID.class);
Map<Reference, List<List<Reference>>> destReferencesByChapter = new HashMap<>();
for (int i = 0; i < vm.getFrom().getVerseCount(); i++) {
Reference from = vm.getFrom().getReference(i);
List<Reference> to = vm.getMapping(from);
if (to.isEmpty())
continue;
if (!destReferencesByBook.containsKey(from.getBook()))
destReferencesByBook.put(from.getBook(), new ArrayList<List<Reference>>());
destReferencesByBook.get(from.getBook()).add(to);
Reference fromChapter = new Reference(from.getBook(), from.getChapter(), "1");
if (!destReferencesByChapter.containsKey(fromChapter))
destReferencesByChapter.put(fromChapter, new ArrayList<List<Reference>>());
destReferencesByChapter.get(fromChapter).add(to);
}
Map<BookID, BookID> bookMappings = new EnumMap<>(BookID.class);
for (Entry<BookID, List<List<Reference>>> byBook : destReferencesByBook.entrySet()) {
if (byBook.getValue().size() < 2)
continue;
BookID sameBook = byBook.getValue().get(0).get(0).getBook();
for (List<Reference> rr : byBook.getValue()) {
for (Reference r : rr) {
if (sameBook != r.getBook()) {
sameBook = null;
break;
}
}
if (sameBook == null)
break;
}
if (sameBook != null && sameBook != byBook.getKey())
bookMappings.put(byBook.getKey(), sameBook);
}
Map<Reference, Reference> chapterMappings = new HashMap<>();
for (Entry<Reference, List<List<Reference>>> byChapter : destReferencesByChapter.entrySet()) {
if (byChapter.getValue().size() < 2)
continue;
Reference sameChapter = byChapter.getValue().get(0).get(0);
sameChapter = new Reference(sameChapter.getBook(), sameChapter.getChapter(), "1");
for (List<Reference> rr : byChapter.getValue()) {
for (Reference r : rr) {
Reference thisChapter = new Reference(r.getBook(), r.getChapter(), "1");
if (sameChapter != null && !sameChapter.equals(thisChapter)) {
sameChapter = null;
break;
}
}
if (sameChapter == null)
break;
}
if (sameChapter == null)
continue;
BookID keyBook = byChapter.getKey().getBook();
if (bookMappings.containsKey(keyBook)) {
keyBook = bookMappings.get(keyBook);
;
}
if (sameChapter.getBook() != keyBook || sameChapter.getChapter() != byChapter.getKey().getChapter()) {
chapterMappings.put(new Reference(byChapter.getKey().getBook(), byChapter.getKey().getChapter(), "1"), sameChapter);
}
}
RefMap refmap = of.createRefSysRefMap();
refmap.setFrom(vm.getFrom().getName());
refmap.setTo(vm.getTo().getName());
boolean kingdomsFrom = isKingdoms(vm.getFrom());
boolean kingdomsTo = isKingdoms(vm.getTo());
Set<BookID> bookMappingsDumped = EnumSet.noneOf(BookID.class);
Set<Reference> chapterMappingsDumped = new HashSet<>();
for (int i = 0; i < vm.getFrom().getVerseCount(); i++) {
Reference from = vm.getFrom().getReference(i);
Reference mappedFrom = from;
List<Reference> to = vm.getMapping(from);
if (bookMappings.containsKey(from.getBook())) {
if (!bookMappingsDumped.contains(from.getBook())) {
addMapping(refmap, formatBook(from.getBook(), kingdomsFrom), formatBook(bookMappings.get(from.getBook()), kingdomsTo));
bookMappingsDumped.add(from.getBook());
}
mappedFrom = new Reference(bookMappings.get(from.getBook()), from.getChapter(), from.getVerse());
} else if (kingdomsFrom != kingdomsTo && KINGS_IDS.contains(from.getBook().getOsisID())) {
if (!bookMappingsDumped.contains(from.getBook())) {
addMapping(refmap, formatBook(from.getBook(), kingdomsFrom), formatBook(from.getBook(), kingdomsTo));
bookMappingsDumped.add(from.getBook());
}
}
Reference chapRef = new Reference(from.getBook(), from.getChapter(), "1");
Reference mapped = chapterMappings.get(chapRef);
if (mapped != null) {
if (!chapterMappingsDumped.contains(chapRef)) {
addMapping(refmap, formatBook(chapRef.getBook(), kingdomsFrom) + "." + chapRef.getChapter(), formatBook(mapped.getBook(), kingdomsTo) + "." + mapped.getChapter());
chapterMappingsDumped.add(chapRef);
}
mappedFrom = new Reference(mapped.getBook(), mapped.getChapter(), from.getVerse());
}
String formattedFrom = formatReference(from, kingdomsFrom);
if (to.size() == 0) {
addMapping(refmap, formattedFrom, "");
} else if (to.size() == 1) {
if (!to.get(0).equals(mappedFrom)) {
addMapping(refmap, formattedFrom, formatReference(to.get(0), kingdomsTo));
}
} else {
boolean consecutive = true;
int base = vm.getTo().getIndexForReference(to.get(0));
for (int j = 1; j < to.size(); j++) {
if (vm.getTo().getIndexForReference(to.get(j)) != base + j) {
consecutive = false;
break;
}
}
if (consecutive) {
addMapping(refmap, formattedFrom, formatReference(to.get(0), kingdomsTo) + "-" + formatReference(to.get(to.size() - 1), kingdomsTo));
} else {
StringBuilder formattedTo = new StringBuilder();
for (Reference r : to) {
if (formattedTo.length() > 0)
formattedTo.append(" ");
formattedTo.append(formatReference(r, kingdomsTo));
}
addMapping(refmap, formattedFrom, formattedTo.toString());
}
}
}
refsys.getRefMap().add(refmap);
}
JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class);
Marshaller m = ctx.createMarshaller();
m.setSchema(getSchema());
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
m.marshal(refsys, doc);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(new DOMSource(doc), new StreamResult(outputFile));
}
use of biblemulticonverter.data.Versification.Reference in project BibleMultiConverter by schierlm.
the class VersificationSet method findMapping.
public VersificationMapping findMapping(String from, String to, int number) {
Versification fromVersification = findVersification(from);
Versification toVersification = findVersification(to);
if (number == -1) {
Map<Reference, List<Reference>> map = new HashMap<>();
for (int i = 0; i < fromVersification.getVerseCount(); i++) {
Reference r = fromVersification.getReference(i);
if (toVersification.containsReference(r))
map.put(r, Arrays.asList(r));
}
return VersificationMapping.build(fromVersification, toVersification, map);
}
List<VersificationMapping> candidates = new ArrayList<>();
for (VersificationMapping mapping : mappings) {
if (mapping.getFrom().getName().equals(from) && mapping.getTo().getName().equals(to))
candidates.add(mapping);
}
if (candidates.isEmpty())
throw new NoSuchElementException("No mapping found from " + from + " to " + to);
if (number == 0 && mappings.size() == 1) {
number = 1;
} else if (number == 0) {
Map<Reference, List<Reference>> map = new HashMap<>();
for (int i = 0; i < fromVersification.getVerseCount(); i++) {
Reference r = fromVersification.getReference(i);
List<Reference> bestMapping = null;
for (VersificationMapping candidate : candidates) {
List<Reference> thisMapping = candidate.getMapping(r);
if (thisMapping.isEmpty())
thisMapping = null;
if (bestMapping == null) {
bestMapping = thisMapping;
} else if (thisMapping != null) {
bestMapping.retainAll(thisMapping);
if (bestMapping.isEmpty())
throw new NoSuchElementException("Unable to build best match from " + from + " to " + to + ": No common subset found for " + r);
}
}
if (bestMapping != null) {
map.put(r, bestMapping);
}
}
return VersificationMapping.build(fromVersification, toVersification, map);
} else if (number < 1 || number > candidates.size()) {
throw new NoSuchElementException("Mapping " + number + " from " + from + " to " + to + " does not exist.");
}
return candidates.get(number - 1);
}
use of biblemulticonverter.data.Versification.Reference in project BibleMultiConverter by schierlm.
the class SWORDVersification method buildMapping.
private void buildMapping(VersificationSet versifications, String fromName, org.crosswire.jsword.versification.Versification fromV11n, String toName, org.crosswire.jsword.versification.Versification toV11n, Map<BookID, BibleBook> reverseBookMapping) {
System.out.println("\t" + fromName + " -> " + toName);
Versification from = versifications.findVersification("SWORD_" + fromName);
Versification to = versifications.findVersification("SWORD_" + toName);
Map<Reference, List<Reference>> map = new HashMap<>();
for (int i = 0; i < from.getVerseCount(); i++) {
Reference ref = from.getReference(i);
Verse v = new Verse(fromV11n, reverseBookMapping.get(ref.getBook()), ref.getChapter(), Integer.parseInt(ref.getVerse()));
VerseKey<?> vk = VersificationsMapper.instance().mapVerse(v, toV11n);
List<Reference> refs = new ArrayList<>();
for (String verse : vk.getOsisID().split(" ")) {
if (verse.isEmpty())
continue;
String[] verseParts = verse.split("\\.");
BookID book = BookID.fromOsisId(verseParts[0]);
int chapter = Integer.parseInt(verseParts[1]);
int verseNum = Integer.parseInt(verseParts[2]);
if (verseNum == 0)
verseNum = 1;
refs.add(new Reference(book, chapter, "" + verseNum));
}
if (!refs.isEmpty())
map.put(ref, refs);
}
versifications.getMappings().add(VersificationMapping.build(from, to, map));
}
Aggregations