Search in sources :

Example 1 with VersificationSet

use of biblemulticonverter.data.VersificationSet 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);
        }
    }
}
Also used : VersificationMapping(biblemulticonverter.data.VersificationMapping) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VersificationSet(biblemulticonverter.data.VersificationSet) ArrayList(java.util.ArrayList) List(java.util.List) EnumMap(java.util.EnumMap) HashSet(java.util.HashSet) Reference(biblemulticonverter.data.Versification.Reference) Versification(biblemulticonverter.data.Versification) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) VersificationFormat(biblemulticonverter.versification.VersificationFormat) File(java.io.File) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with VersificationSet

use of biblemulticonverter.data.VersificationSet 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 });
}
Also used : VersificationMapping(biblemulticonverter.data.VersificationMapping) Reference(biblemulticonverter.data.Versification.Reference) Bible(biblemulticonverter.data.Bible) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) VersificationSet(biblemulticonverter.data.VersificationSet) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) File(java.io.File) EnumMap(java.util.EnumMap) Verse(biblemulticonverter.data.Verse)

Example 3 with VersificationSet

use of biblemulticonverter.data.VersificationSet in project BibleMultiConverter by schierlm.

the class VersificationDetector method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    vs = new VersificationSet(new File(exportArgs[0]));
    useRanges = (exportArgs.length > 1 && exportArgs[1].equals("-range"));
    super.doExport(bible, Arrays.copyOfRange(exportArgs, useRanges ? 2 : 1, exportArgs.length));
}
Also used : VersificationSet(biblemulticonverter.data.VersificationSet) File(java.io.File)

Example 4 with VersificationSet

use of biblemulticonverter.data.VersificationSet in project BibleMultiConverter by schierlm.

the class BMCV method doExport.

@Override
public void doExport(File outputFile, List<Versification> versifications, List<VersificationMapping> mappings) throws Exception {
    VersificationSet set = new VersificationSet();
    set.getVersifications().addAll(versifications);
    set.getMappings().addAll(mappings);
    try (Writer w = new OutputStreamWriter(new FileOutputStream(outputFile), StandardCharsets.UTF_8)) {
        set.saveTo(w);
    }
}
Also used : VersificationSet(biblemulticonverter.data.VersificationSet) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

VersificationSet (biblemulticonverter.data.VersificationSet)4 File (java.io.File)3 Reference (biblemulticonverter.data.Versification.Reference)2 VersificationMapping (biblemulticonverter.data.VersificationMapping)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 EnumMap (java.util.EnumMap)2 Bible (biblemulticonverter.data.Bible)1 Book (biblemulticonverter.data.Book)1 BookID (biblemulticonverter.data.BookID)1 Chapter (biblemulticonverter.data.Chapter)1 FormattedText (biblemulticonverter.data.FormattedText)1 Verse (biblemulticonverter.data.Verse)1 Versification (biblemulticonverter.data.Versification)1 VersificationFormat (biblemulticonverter.versification.VersificationFormat)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1