Search in sources :

Example 16 with WordType

use of eu.transkribus.core.model.beans.pagecontent.WordType in project TranskribusCore by Transkribus.

the class TrpElementCoordinatesComparator method compare.

@Override
public int compare(T o1, T o2) {
    // if (!isRegionLineOrWord(o1) || !isRegionLineOrWord(o2))
    // return 0;
    logger.trace("compare in TrpElementCoordinatesComparator");
    // try {
    String coords1 = "", coords2 = "";
    if (o1 instanceof RegionType) {
        RegionType r1 = (RegionType) o1;
        RegionType r2 = (RegionType) o2;
        if (r1.getCoords() != null && r2.getCoords() != null) {
            coords1 = r1.getCoords().getPoints();
            coords2 = r2.getCoords().getPoints();
        }
    } else if (TextLineType.class.isAssignableFrom(o1.getClass())) {
        // if existing, take baseline to compare position of lines
        if (((TextLineType) o1).getBaseline() != null && ((TextLineType) o2).getBaseline() != null) {
            coords1 = ((TextLineType) o1).getBaseline().getPoints();
            coords2 = ((TextLineType) o2).getBaseline().getPoints();
        } else {
            // fall back if there are no baselines
            coords1 = ((TextLineType) o1).getCoords().getPoints();
            coords2 = ((TextLineType) o2).getCoords().getPoints();
        }
    } else if (o1 instanceof TrpBaselineType) {
        coords1 = ((TrpBaselineType) o1).getPoints();
        coords2 = ((TrpBaselineType) o2).getPoints();
    } else if (WordType.class.isAssignableFrom(o1.getClass())) {
        WordType w1 = (WordType) o1;
        WordType w2 = (WordType) o2;
        if (w1.getCoords() != null && w2.getCoords() != null) {
            coords1 = w1.getCoords().getPoints();
            coords2 = w2.getCoords().getPoints();
        }
    }
    // if (coords1.isEmpty() || coords2.isEmpty()) {
    // throw new Exception("No coordinates in one of the objects - should not happen!");
    // }
    // determine orientation of (parent) text regions
    Float orientation = null;
    if (o1 instanceof ITrpShapeType && o2 instanceof ITrpShapeType && !(o1 instanceof RegionType) && !(o2 instanceof RegionType)) {
        TrpTextRegionType tr1 = TrpShapeTypeUtils.getTextRegion((ITrpShapeType) o1);
        TrpTextRegionType tr2 = TrpShapeTypeUtils.getTextRegion((ITrpShapeType) o2);
        if (tr1 != null && tr2 != null && StringUtils.equals(tr1.getId(), tr2.getId()) && tr1.getOrientation() != null) {
            orientation = tr1.getOrientation();
        }
    }
    // --------------------------
    java.awt.Polygon p1 = new java.awt.Polygon();
    try {
        for (java.awt.Point p : PointStrUtils.parsePoints(coords1)) p1.addPoint(p.x, p.y);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    java.awt.Polygon p2 = new java.awt.Polygon();
    try {
        for (java.awt.Point p : PointStrUtils.parsePoints(coords2)) p2.addPoint(p.x, p.y);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    Rectangle b1 = p1.getBounds();
    Rectangle b2 = p2.getBounds();
    Point pt1 = new Point(b1.x, b1.y);
    Point pt2 = new Point(b2.x, b2.y);
    if (orientation != null) {
        pt1 = GeomUtils.rotate(pt1, orientation);
        pt2 = GeomUtils.rotate(pt2, orientation);
        logger.trace("orientation set: " + orientation + " rotated points: " + pt1 + ", " + pt2);
    }
    if (compareByYX == null) {
        // if compareByYX was not set by constructor, determine via shape
        compareByYX = !WordType.class.isAssignableFrom(o1.getClass());
    }
    if (!compareByYX) {
        // return compareByXY(b1.x, b2.x, b1.y, b2.y);
        return compareByXY(pt1.x, pt2.x, pt1.y, pt2.y);
    } else {
        return compareByYX(pt1.x, pt2.x, pt1.y, pt2.y);
    // return compareByYX(b1.x, b2.x, b1.y, b2.y);
    // return compareBy_YOverlap_X(b1, b2);
    }
// }
// catch (Exception e) {
// e.printStackTrace();
// return 0;
// }
}
Also used : RegionType(eu.transkribus.core.model.beans.pagecontent.RegionType) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Point(java.awt.Point) WordType(eu.transkribus.core.model.beans.pagecontent.WordType) TextLineType(eu.transkribus.core.model.beans.pagecontent.TextLineType)

Example 17 with WordType

use of eu.transkribus.core.model.beans.pagecontent.WordType in project TranskribusCore by Transkribus.

the class TrpPageTypeUtils method assignUniqueIDs.

/**
 * Assigns unique IDs to the elements in the page using the current order of the elements.
 */
public static void assignUniqueIDs(PageType page) {
    int i = 1;
    for (RegionType r : page.getTextRegionOrImageRegionOrLineDrawingRegion()) {
        if (r instanceof TextRegionType) {
            TextRegionType region = (TextRegionType) r;
            String rid = "r" + i;
            region.setId(rid);
            int j = 1;
            for (TextLineType l : region.getTextLine()) {
                String lid = rid + "l" + j;
                l.setId(lid);
                int k = 1;
                for (WordType word : l.getWord()) {
                    String wid = lid + "w" + k;
                    word.setId(wid);
                    k++;
                }
                ++j;
            }
            ++i;
        }
    }
}
Also used : TextRegionType(eu.transkribus.core.model.beans.pagecontent.TextRegionType) RegionType(eu.transkribus.core.model.beans.pagecontent.RegionType) TextRegionType(eu.transkribus.core.model.beans.pagecontent.TextRegionType) TextLineType(eu.transkribus.core.model.beans.pagecontent.TextLineType) WordType(eu.transkribus.core.model.beans.pagecontent.WordType)

Aggregations

WordType (eu.transkribus.core.model.beans.pagecontent.WordType)17 TextLineType (eu.transkribus.core.model.beans.pagecontent.TextLineType)12 TrpTextLineType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType)9 TrpWordType (eu.transkribus.core.model.beans.pagecontent_trp.TrpWordType)9 TrpTextRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType)8 RegionType (eu.transkribus.core.model.beans.pagecontent.RegionType)4 TextRegionType (eu.transkribus.core.model.beans.pagecontent.TextRegionType)4 TrpPage (eu.transkribus.core.model.beans.TrpPage)3 TrpPageType (eu.transkribus.core.model.beans.pagecontent_trp.TrpPageType)3 Rectangle (java.awt.Rectangle)3 ArrayList (java.util.ArrayList)3 JAXBPageTranscript (eu.transkribus.core.model.beans.JAXBPageTranscript)2 TrpTranscriptMetadata (eu.transkribus.core.model.beans.TrpTranscriptMetadata)2 TrpBaselineType (eu.transkribus.core.model.beans.pagecontent_trp.TrpBaselineType)2 TrpRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType)2 Point (java.awt.Point)2 IOException (java.io.IOException)2 Chunk (com.itextpdf.text.Chunk)1 Phrase (com.itextpdf.text.Phrase)1 RtfText (com.tutego.jrtf.RtfText)1