Search in sources :

Example 1 with VersificationFormat

use of biblemulticonverter.versification.VersificationFormat 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)

Aggregations

Versification (biblemulticonverter.data.Versification)1 Reference (biblemulticonverter.data.Versification.Reference)1 VersificationMapping (biblemulticonverter.data.VersificationMapping)1 VersificationSet (biblemulticonverter.data.VersificationSet)1 VersificationFormat (biblemulticonverter.versification.VersificationFormat)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1