use of eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType in project TranskribusCore by Transkribus.
the class CustomTagListTest method testAddBothOverlap.
// @Ignore
@Test
public void testAddBothOverlap() {
TrpTextLineType line = new TrpTextLineType(new TrpTextRegionType(new TrpPageType()));
line.setUnicodeText("Hello world and all parallel worlds!", null);
// line.setUnicodeText("hello", who);
CustomTagList tl = new CustomTagList(line);
// int l=10;
TextStyleTag ts1 = new TextStyleTag(2, 3);
ts1.setBold(true);
ts1.setItalic(true);
tl.addOrMergeTag(ts1, null);
logger.debug("tl1 = " + tl);
TextStyleTag ts2 = new TextStyleTag(6, 2);
ts2.setBold(true);
ts2.setSuperscript(true);
tl.addOrMergeTag(ts2, null);
logger.debug("tl2 = " + tl);
TextStyleTag ts3 = new TextStyleTag(9, 11);
tl.addOrMergeTag(ts3, null);
logger.debug("tl3 = " + tl);
Assert.assertEquals("Nr. of merged elements must be 2 --> empty tags shall be removed!", 2, tl.getTags().size());
}
use of eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType in project TranskribusCore by Transkribus.
the class TrpXlsxBuilder method writeXlsxForDoc.
public static void writeXlsxForDoc(TrpDoc doc, boolean wordBased, File exportFile, Set<Integer> pageIndices, IProgressMonitor monitor, ExportCache cache) throws NoTagsException, Exception {
if (cache == null) {
throw new IllegalArgumentException("ExportCache must not be null.");
}
if (cache.getCustomTagMapForDoc().isEmpty()) {
logger.info("No tags to store -> Xlsx export cancelled");
throw new NoTagsException("No tags available to store into Xlsx");
}
List<TrpPage> pages = doc.getPages();
String exportPath = exportFile.getPath();
Set<String> selectedTags = cache.getOnlySelectedTagnames(ExportUtils.getOnlyWantedTagnames(CustomTagFactory.getRegisteredTagNames()));
int totalPages = pageIndices == null ? pages.size() : pageIndices.size();
if (monitor != null) {
monitor.beginTask("Exporting to Excel", totalPages);
}
wb = new XSSFWorkbook();
int c = 0;
for (int i = 0; i < pages.size(); ++i) {
if (pageIndices != null && !pageIndices.contains(i))
continue;
if (monitor != null) {
if (monitor.isCanceled()) {
throw new InterruptedException("Export was canceled by user");
// logger.debug("Xlsx export cancelled!");
// return;
}
monitor.subTask("Processing page " + (c + 1));
}
TrpPage page = pages.get(i);
// try to get previously loaded JAXB transcript
JAXBPageTranscript tr = null;
if (cache != null) {
tr = cache.getPageTranscriptAtIndex(i);
}
if (tr == null) {
TrpTranscriptMetadata md = page.getCurrentTranscript();
tr = new JAXBPageTranscript(md);
tr.build();
}
// old version
// TrpPage page = pages.get(i);
// TrpTranscriptMetadata md = page.getCurrentTranscript();
// JAXBPageTranscript tr = new JAXBPageTranscript(md);
// tr.build();
TrpPageType trpPage = tr.getPage();
logger.debug("writing xlsx for page " + (i + 1) + "/" + doc.getNPages());
List<TrpTextRegionType> textRegions = trpPage.getTextRegions(true);
for (int j = 0; j < textRegions.size(); ++j) {
TrpTextRegionType r = textRegions.get(j);
List<TextLineType> lines = r.getTextLine();
for (int k = 0; k < lines.size(); ++k) {
TrpTextLineType trpL = (TrpTextLineType) lines.get(k);
List<WordType> words = trpL.getWord();
if (wordBased) {
for (int l = 0; l < words.size(); ++l) {
TrpWordType w = (TrpWordType) words.get(l);
writeTagsForShapeElement(w, trpL.getUnicodeText(), String.valueOf(doc.getId()), String.valueOf(page.getPageNr()), r.getId(), trpL.getId(), w.getId(), selectedTags);
}
} else {
writeTagsForShapeElement(trpL, trpL.getUnicodeText(), String.valueOf(doc.getId()), String.valueOf(page.getPageNr()), r.getId(), trpL.getId(), "", selectedTags);
}
}
}
++c;
if (monitor != null) {
monitor.worked(c);
}
}
/*
* auto size the columns
*/
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
int numberOfCells = 0;
Iterator rowIterator = wb.getSheetAt(i).rowIterator();
/**
* Escape the header row *
*/
if (rowIterator.hasNext()) {
Row headerRow = (Row) rowIterator.next();
// get the number of cells in the header row
numberOfCells = headerRow.getPhysicalNumberOfCells();
for (int j = 0; j < numberOfCells; j++) {
wb.getSheetAt(i).autoSizeColumn(j);
}
}
}
FileOutputStream fOut;
try {
// means no tags at all
if (wb.getNumberOfSheets() == 0) {
throw new IOException("Sorry - No tags available for export");
}
fOut = new FileOutputStream(exportPath);
wb.write(fOut);
fOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}
logger.info("wrote xlsx to: " + exportPath);
}
use of eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType in project TranskribusCore by Transkribus.
the class TrpPdfDocument method hasSmallerColumn.
/*
* checks if there is at least one text region on the left of the actual one
* But: if text region is completely contained in the other it should not have an effect
*/
private boolean hasSmallerColumn(List<TrpRegionType> regions, TextRegionType regionToCompare) throws DocumentException, IOException {
float minX = 0;
float minY = 0;
float maxX = 0;
float maxY = 0;
float meanX = 0;
float meanY = 0;
// java.awt.Rectangle compareBlock = PageXmlUtils.buildPolygon(regionToCompare.getCoords().getPoints()).getBounds();
java.awt.Rectangle compareBlock = regionToCompare.getBoundingBox();
float compareMinX = (float) compareBlock.getMinX();
float compareMinY = (float) compareBlock.getMinY();
float compareMaxX = (float) compareBlock.getMaxX();
float compareMaxY = (float) compareBlock.getMaxY();
float compareMeanX = compareMinX + (compareMaxX - compareMinX) / 2;
float compareMeanY = compareMinY + (compareMaxY - compareMinY) / 2;
boolean foundSmallerColumn = false;
smallerRegionMaxX = 0;
if (regions.size() == 1) {
return false;
} else {
for (RegionType r : regions) {
// TODO add paths for tables etc.
if (r instanceof TextRegionType && r.getId() != regionToCompare.getId()) {
TextRegionType tr = (TextRegionType) r;
// empty region can be ignored
if (tr.getTextLine().isEmpty())
continue;
else {
// region with empty lines can also be ignored
boolean textFound = false;
for (TextLineType tlt : tr.getTextLine()) {
TrpTextLineType l = (TrpTextLineType) tlt;
textFound = !l.getUnicodeText().isEmpty();
if (textFound) {
break;
}
}
// no text in region -> go to next region
if (!textFound) {
continue;
}
}
// logger.debug("tr id " + tr.getId());
// compute average text region start
// java.awt.Rectangle block = PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds();
java.awt.Rectangle block = tr.getBoundingBox();
minX = (float) block.getMinX();
maxX = (float) block.getMaxX();
minY = (float) block.getMinY();
maxY = (float) block.getMaxY();
// meanX = minX+(maxX - minX)/2;
meanY = minY + (maxY - minY) / 2;
if (((meanY > compareMinY && meanY < compareMaxY) || (compareMeanY > minY && compareMeanY < maxY)) && (maxX < compareMeanX)) {
// to find the biggest maxX if there are several smaller columns
if (maxX > smallerRegionMaxX) {
smallerRegionMaxX = maxX;
}
foundSmallerColumn = true;
}
}
}
}
return foundSmallerColumn;
}
use of eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType in project TranskribusCore by Transkribus.
the class TrpTeiStringBuilder method writeZonesForTextRegion.
void writeZonesForTextRegion(SebisStringBuilder sb, TextRegionType r, int pageNr) {
String facsId = FACS_ID_PREFIX + pageNr;
if (pars.regionZones) {
writeZoneForShape(sb, (TrpTextRegionType) r, facsId, !pars.lineZones && !pars.wordZones);
}
if (!pars.lineZones && !pars.wordZones)
return;
for (TextLineType tl : r.getTextLine()) {
TrpTextLineType ttl = (TrpTextLineType) tl;
if (pars.lineZones) {
writeZoneForShape(sb, ttl, facsId, !pars.wordZones);
}
if (pars.wordZones) {
for (WordType w : ttl.getWord()) {
TrpWordType tw = (TrpWordType) w;
writeZoneForShape(sb, tw, facsId, true);
}
if (pars.lineZones) {
closeElement(sb, "zone");
}
}
}
if (pars.regionZones) {
closeElement(sb, "zone");
}
}
use of eu.transkribus.core.model.beans.pagecontent_trp.TrpTextLineType in project TranskribusCore by Transkribus.
the class TrpTeiStringBuilder method writeTextForTextRegion.
void writeTextForTextRegion(SebisStringBuilder sb, TextRegionType r, int pageNr) {
String facsId = FACS_ID_PREFIX + pageNr;
if (r.getTextLine().isEmpty()) {
logger.warn("skipping empty region: " + r.getId());
return;
}
writeTextRegion(sb, r, facsId);
for (TextLineType tl : r.getTextLine()) {
TrpTextLineType ttl = (TrpTextLineType) tl;
if (!commonPars.isWriteTextOnWordLevel()) {
writeLineOrWord(sb, ttl, facsId);
} else {
String lStart = getLineOrWordStart(ttl, facsId);
sb.incIndent();
sb.addLine(lStart);
// TODO: write text for words???
for (WordType w : ttl.getWord()) {
writeLineOrWord(sb, (TrpWordType) w, facsId);
}
String lEnd = getLineOrWordEnd(ttl, facsId);
sb.addLine(lEnd);
// sb.append("\n");
sb.decIndent();
}
}
closeTextRegion(sb);
}
Aggregations