-
Notifications
You must be signed in to change notification settings - Fork 79
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
ADD script to create a simplified version of hocr-files #152
Open
JKamlah
wants to merge
15
commits into
ocropus:master
Choose a base branch
from
JKamlah:simplify
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ba74b3e
ADD script to create a simplified version of hocr-files.
JKamlah 7385e5a
Refactored code.
JKamlah 4f0a271
Style fixes.
JKamlah 9160877
Added ws and removed another.
JKamlah 50f4855
ADD test case for hocr-simplify
JKamlah e264c2f
FIX remove properties, ADD meta information correction, ADD remove ch…
be4bb77
FIX char encoding, ADD remove-empty-contents which removes empty cont…
4fb6a4c
ADD remove choices, which removes all lstm_choices (tesseract only),F…
95fa53f
FIX max char in line.
009d746
REWORK help messages and comments.
a762023
FIX encoding read and write.
4c44dea
ADD new tests and testfiles.
cbc78fa
README update
JKamlah a050fbc
README add EOL
JKamlah 6a4e4ff
README DEL two ws.
JKamlah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#!/usr/bin/env python | ||
|
||
# Create a simplipfied hocr-version by: | ||
# change level of typesetting | ||
# remove properties | ||
# remove attributes | ||
# remove empty contents | ||
# remove character alternatives (choices) | ||
|
||
|
||
from __future__ import print_function | ||
import argparse | ||
import re | ||
import os | ||
from io import open | ||
import sys | ||
|
||
from lxml import etree, html | ||
|
||
parser = argparse.ArgumentParser( | ||
description=('change level of typesetting and/or' | ||
'remove properties to create' | ||
'a simplified hocr-version')) | ||
|
||
properties = ['baseline', 'bbox', 'cflow', 'cuts', 'hardbreak', 'image', | ||
'imagemd5', 'lpageno', 'ppageno', 'nlp', 'order', 'poly', | ||
'scan_res', 'textangle', 'x_booxes', 'x_font', 'x_fsize', | ||
'x_confs', 'x_scanner', 'x_source', 'x_wconf'] | ||
|
||
typesettings = ['ocrx_word', 'ocr_line', 'ocr_par', 'ocr_carea', 'ocr_page'] | ||
|
||
parser.add_argument('file', nargs='?', default=sys.stdin) | ||
parser.add_argument('-t', '--typesetting', type=str, | ||
choices=typesettings, | ||
help='Sets a new minimum typesetting level.\n' | ||
'List of typesetting: {}'.format(','.join(typesettings))) | ||
parser.add_argument('-a', '--remove-attributes', nargs='+', | ||
help='Removes attributes, e.g. id') | ||
parser.add_argument('-c', '--remove-choices', action='store_true', | ||
help='Removes character alternatives (tesseract outputs only)') | ||
parser.add_argument('-e', '--remove-empty-contents', action='store_true', | ||
help='Removes contents which are empty or contains whitespaces only') | ||
parser.add_argument('-p', '--remove-properties', nargs='+', | ||
help='List of properties: {}'.format(','.join(properties))) | ||
parser.add_argument('fileout', nargs='?', | ||
help="Output path, default: print to terminal") | ||
parser.add_argument('-v', '--verbose', | ||
action='store_true', help='Verbose, default: %(default)s') | ||
|
||
args = parser.parse_args() | ||
|
||
with open(args.file,"r",encoding="utf-8") as f: | ||
doc = html.parse(f) | ||
|
||
# delete all nodes where the id-attribute contain lstm_choices | ||
if args.remove_choices: | ||
for node in doc.xpath('.//*[contains(@id,"lstm_choices")]'): | ||
node.getparent().remove(node) | ||
|
||
# change level of typesetting | ||
if args.typesetting: | ||
# update meta content | ||
node = doc.find("//*[@name='ocr-capabilities']") | ||
if node is not None: | ||
content = node.get("content") | ||
if content is not None and args.typesetting in content: | ||
node.set("content", content.split(args.typesetting)[0] + args.typesetting) | ||
if args.verbose: | ||
print(node.get("content")) | ||
|
||
# apply new level of typesetting | ||
for typesetting in typesettings: | ||
for node in doc.xpath("//*[@class='{}']".format(typesetting)): | ||
if args.verbose and typesetting == args.typesetting: | ||
print(re.sub(r'\s+', '\x20', node.text_content()).strip()) | ||
text_content = node.text_content() | ||
seperator = "\n" | ||
if "word" in typesetting: | ||
seperator = "" | ||
elif "line" in typesetting: | ||
seperator = " " | ||
node.text = seperator.join([text.strip().replace("\n","") for text in | ||
text_content.splitlines() if | ||
not text.strip() != "\n" and | ||
args.remove_empty_contents or text.strip() != ""]) | ||
for child in list(node): | ||
node.remove(child) | ||
if typesetting == args.typesetting: | ||
break | ||
|
||
# remove properties | ||
if args.remove_properties: | ||
for node in doc.xpath("//*[@title]"): | ||
title = node.get("title") | ||
node.set('title', ';'.join([prop.replace("\"","'") for prop in | ||
title.split(";") if | ||
prop.strip().split(None, 1)[0] not in | ||
args.remove_properties])) | ||
if args.verbose: | ||
print("Replaced :{}".format(title)) | ||
else: | ||
# Replace double quotation marks with single | ||
for node in doc.xpath("//*[@title]"): | ||
node.set("title",node.get("title").replace("\"","'")) | ||
|
||
# remove attributes | ||
if args.remove_attributes: | ||
for attr in args.remove_attributes: | ||
for node in doc.xpath("//*[@{}]".format(attr)): | ||
node.attrib.pop("{}".format(attr)) | ||
|
||
# if no output path is given, print to terminal | ||
if args.fileout is None: | ||
encoding = "utf-8" | ||
if sys.version_info[0] > 2: | ||
encoding = str | ||
print(etree.tostring(doc, pretty_print=True,encoding=encoding)) | ||
|
||
else: | ||
# create output path if needed | ||
if not os.path.isdir(os.path.dirname(args.fileout)): | ||
os.makedirs(os.path.dirname(args.fileout)) | ||
|
||
# write new hocr file | ||
with open(args.fileout, "wb") as f: | ||
f.write(etree.tostring(doc, pretty_print=True,encoding="utf-8")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env tsht | ||
TESTDATA="../testdata" | ||
SIMPLEFILE="./tess.simple.hocr" | ||
|
||
plan 3 | ||
|
||
after () { | ||
rm -f "$SIMPLEFILE" | ||
} | ||
hocr-simplify "$TESTDATA/tess.hocr" -t ocr_page > "$SIMPLEFILE" || fail 'hocr-simplify' | ||
equals 3268 $(ls -l "$SIMPLEFILE" | cut -d " " -f5 ) 'filesize == 3268' | ||
|
||
hocr-simplify "$TESTDATA/tess_choices.hocr" -c -t ocr_line > "$SIMPLEFILE" || fail 'hocr-simplify' | ||
equals 9691 $(ls -l "$SIMPLEFILE" | cut -d " " -f5 ) 'filesize == 9691' | ||
|
||
hocr-simplify "$TESTDATA/tess_choices_charboxes.hocr" -c -t ocrx_word > "$SIMPLEFILE" || fail 'hocr-simplify' | ||
equals 58622 $(ls -l "$SIMPLEFILE" | cut -d " " -f5 ) 'filesize == 58622' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#!/usr/bin/env tsht | ||
|
||
for f in check combine eval eval-geom eval-lines extract-g1000 extract-images lines merge-dc pdf split;do | ||
for f in check combine eval eval-geom eval-lines extract-g1000 extract-images lines merge-dc pdf split simplify;do | ||
exec_ok "hocr-$f" "--help" | ||
exec_ok "hocr-$f" "-h" | ||
done |
Empty file.
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have also an option to delete
id
and/ordir
parameter, but they are on their own.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing attributes is now implemented