【发布时间】:2011-08-20 23:59:29
【问题描述】:
我正在尝试从 OCR 图像文本的输出中重新创建段落和缩进,如下所示:
输入(想象这是一张图片,不是输入的):
输出(有一些错误):
如您所见,没有保留段落分隔符或缩进。
使用 Python,我尝试了这样的方法,但它不起作用(失败太频繁):
代码:
def smart_format(text):
textList = text.split('\n')
temp = ''
averageLL = sum([len(line) for line in textList]) / len(textList)
for line in textList:
if (line.strip().endswith('!') or line.strip().endswith('.') or line.strip().endswith('?')) and not line.strip().endswith('-'):
if averageLL - len(line) > 7:
temp += '{{ paragraph }}' + line + '\n'
else:
temp += line + '\n'
else:
temp += line + '\n'
return temp.replace(' -\n', '').replace('-\n', '').replace(' \n', '').replace('\n', ' ').replace('{{ paragraph }}', '\n\n ')
有人对我如何重新创建此布局有任何建议吗?我正在处理旧书,所以我希望用 LaTeX 重新排版它们,因为创建 Python 脚本来做到这一点非常简单。
谢谢!
【问题讨论】:
-
澄清一下,您是否只查看文本输出并猜测应该在哪里换段?还是有别的原因?
-
我有源图像文件,但最好只处理输出的文本文件。我正在尝试在下面实现@Nick ODell 的答案,但我的 OpenCV 技能相当生疏......
-
这不会是在段落的最后一行 before 而不是 after 中插入分节符,对吗?
-
什么软件在做 OCR?有些可以配置为提供换行符。
-
@Mu Mind 好像是(捂脸)。让我们再次尝试运行它...
标签: python latex ocr tesseract