use of biblemulticonverter.data.VersificationMapping 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.VersificationMapping 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.VersificationMapping in project BibleMultiConverter by schierlm.
the class VersificationMappedDiffable method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
String outputFile = exportArgs[0];
VersificationSet vs = new VersificationSet(new File(exportArgs[1]));
VersificationMapping vm = vs.findMapping(exportArgs[2]);
boolean dropUnmapped = false, showNumbers = false, addTags = false;
;
for (int i = 3; i < exportArgs.length; i++) {
if (exportArgs[i].equals("DropUnmapped"))
dropUnmapped = true;
else if (exportArgs[i].equals("ShowNumbers"))
showNumbers = true;
else if (exportArgs[i].equals("AddTags"))
addTags = true;
else
throw new IllegalArgumentException("Unsupported option: " + exportArgs[i]);
}
Map<BookID, String> abbrMap = new EnumMap<>(BookID.class);
for (Book book : bible.getBooks()) {
abbrMap.put(book.getId(), book.getAbbr());
}
Bible newBible = new Bible(bible.getName());
Map<BookID, Book> newBooks = new EnumMap<>(BookID.class);
for (Book book : bible.getBooks()) {
if (book.getId().getZefID() < 1) {
// metadata book, introduction or appendix
newBible.getBooks().add(book);
continue;
}
int cnumber = 0;
for (Chapter chap : book.getChapters()) {
cnumber++;
if (chap.getProlog() != null && chap.getVerses().isEmpty()) {
System.out.println("WARNING: Prolog for " + book.getAbbr() + " " + cnumber + " got lost as chapter contains no verses.");
}
for (int j = 0; j < chap.getVerses().size(); j++) {
Verse oldVerse = chap.getVerses().get(j);
Reference ref = new Reference(book.getId(), cnumber, oldVerse.getNumber()), newRef;
List<Reference> newRefs = vm.getMapping(ref);
if ((newRefs == null || newRefs.isEmpty()) && dropUnmapped) {
if (j == 0 && chap.getProlog() != null) {
System.out.println("WARNING: Prolog for " + book.getAbbr() + " " + cnumber + " got lost as first verse of it is unmapped.");
}
continue;
}
if (newRefs == null || newRefs.contains(ref) || newRefs.isEmpty())
newRef = ref;
else
newRef = newRefs.get(0);
if (!newBooks.containsKey(newRef.getBook())) {
Book newBook = null;
for (Book oldBook : bible.getBooks()) {
if (oldBook.getId() == newRef.getBook()) {
newBook = new Book(oldBook.getAbbr(), newRef.getBook(), oldBook.getShortName(), oldBook.getLongName());
break;
}
}
if (newBook == null)
newBook = new Book(newRef.getBook().getOsisID(), newRef.getBook(), newRef.getBook().getEnglishName(), newRef.getBook().getEnglishName());
newBooks.put(newRef.getBook(), newBook);
newBible.getBooks().add(newBook);
}
Book newBook = newBooks.get(newRef.getBook());
while (newBook.getChapters().size() < newRef.getChapter()) newBook.getChapters().add(new Chapter());
Chapter newChapter = newBook.getChapters().get(newRef.getChapter() - 1);
if (j == 0 && chap.getProlog() != null) {
FormattedText newProlog = new FormattedText();
if (newChapter.getProlog() != null) {
newChapter.getProlog().accept(newProlog.getAppendVisitor());
newProlog.getAppendVisitor().visitLineBreak(LineBreakKind.PARAGRAPH);
}
chap.getProlog().accept(new MapXrefVisitor(newProlog.getAppendVisitor(), vm, dropUnmapped, abbrMap));
newProlog.finished();
newChapter.setProlog(newProlog);
}
Verse newVerse = null;
for (Verse v : newChapter.getVerses()) {
if (v.getNumber().equals(newRef.getVerse())) {
newVerse = v;
break;
}
}
boolean needSpace = true;
if (newVerse == null) {
newVerse = new Verse(newRef.getVerse());
newChapter.getVerses().add(newVerse);
needSpace = false;
}
if (needSpace || !ref.equals(newRef)) {
Visitor<RuntimeException> v = newVerse.getAppendVisitor();
if (addTags)
v = v.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "v11n", "origverse", ref.getBook().getOsisID() + "--" + ref.getChapter() + "--" + ref.getVerse());
if (needSpace)
v.visitText(" ");
if (showNumbers) {
String verseNumber;
if (!ref.getBook().equals(newRef.getBook())) {
verseNumber = ref.getBook().getOsisID() + " " + ref.getChapter() + ":" + ref.getVerse();
} else if (ref.getChapter() != newRef.getChapter()) {
verseNumber = ref.getChapter() + ":" + ref.getVerse();
} else {
verseNumber = ref.getVerse();
}
v.visitFormattingInstruction(FormattingInstructionKind.BOLD).visitText("(" + verseNumber + ")");
v.visitText(" ");
}
}
oldVerse.accept(new MapXrefVisitor(newVerse.getAppendVisitor(), vm, dropUnmapped, abbrMap));
}
}
}
for (Book bk : newBible.getBooks()) {
for (Chapter ch : bk.getChapters()) {
for (Verse v : ch.getVerses()) {
v.finished();
}
}
}
bible = newBible;
new Diffable().doExport(bible, new String[] { outputFile });
}
Aggregations