Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

miniocr: Make page fragment parsing more robust (#447) #464

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.digitalcollections</groupId>
<artifactId>solr-ocrhighlighting</artifactId>
<version>0.9.1</version>
<version>0.9.2-SNAPSHOT</version>

<name>Solr OCR Highlighting Plugin</name>
<description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
import javax.xml.stream.XMLStreamException;

public class MiniOcrFormat implements OcrFormat {
private static final Pattern pagePat =
Pattern.compile(
"<p (?:xml)?:id=(\"|')(?<pageId>.+?)(\"|') ?(?:wh=(\"|')(?<width>\\d+) (?<height>\\d+)(\"|'))?>");
private static final Pattern pageIdPat =
Pattern.compile("(?:xml)?:id=[\"'](?<pageId>.+?)[\"']");
private static final Pattern pageDimPat =
Pattern.compile("wh=[\"'](?<width>\\d+) (?<height>\\d+)[\"']");
private static final Map<OcrBlock, String> blockTagMapping =
ImmutableMap.of(
OcrBlock.PAGE, "p",
Expand Down Expand Up @@ -50,15 +51,16 @@ public OcrParser getParser(Reader input, OcrParser.ParsingFeature... features) {

@Override
public OcrPage parsePageFragment(String pageFragment) {
Matcher m = pagePat.matcher(pageFragment);
if (!m.find()) {
return null;
}
String pageId = null;
Dimension dims = null;
if (m.group("width") != null && m.group("height") != null) {
Matcher m = pageIdPat.matcher(pageFragment);
if (m.find()) {
pageId = m.group("pageId");
}
m = pageDimPat.matcher(pageFragment);
if (m.find()) {
dims = new Dimension(Integer.parseInt(m.group("width")), Integer.parseInt(m.group("height")));
}
String pageId = m.group("pageId");
return new OcrPage(pageId, dims);
}

Expand Down
Loading