根据 ocr 图像结尾的段落将字符串拆分为列

2024-01-04

我正在开发一个项目,将打字机写的战争日记笔记从 PDF 扫描转换为文本。我可以成功地(对于原始的未调整大小的文件,可能是 90%)提取我首先裁剪的主要文本。

Reprex 数据:您可以从头开始尝试使用图像或我在下面提供的文本。

我的挑战是保持文本的“每日”结构,该结构有 7 个段落或部分,每天一个,并且按“\n”或“\n\n”分割并不完全正确。

我在该项目中使用了 pdftools/stringr/tesseract/magick 的组合:

library(tesseract)
library(dplyr)
library(stringr)
library(pdftools)
library(readr)
library(magick)

Using:

image <- image_read("./test-data/page_1.png") #change to your path


text -> image %>%
  image_crop(geometry_area(width = 1220, height = 900,
                           y_off = 260, x_off = 355)) %>% 
  image_resize("2000x") %>%
  image_convert(type = 'Grayscale') %>%
  image_trim(fuzz = 40) %>%
  image_write(format = 'png', density = '300x300') %>%
  tesseract::ocr() 

给出一个字符串:

[1] "Weather clear all day. A smaii arms inspection hela at i400 hrs. A recce party went\nlout consisting of Coy Comds and Lt Col Nicklin, I.0. and Asst Adjt. An Orders group\nheld in the evening. Pay parade for HQ and Bn HQ was at 1900 hrs. A movie was shown\nlfor B Coy personnel by our YMCA Supervisor. o\nWeather clear and cold all day. Personnel packed equipment early in the morning and |-~\nwere ready to move at 0830 hrs. Unit embussed at 0900 hrs and moved to Rochefort, MR\n2076, Sheet 105, 1/25000, arriving at 1390 hrs. Coys were in position at 1600 hrs. |,.\nPW brought in by A Coy at 1900 hrs. PW was a deserter from 304 Regt 2 Pz division. | .\n© other activity during the day. Patrols were sent out during the night by all coys,.\nCold all day. Very quiet all morning. A Coy moved forward. Coy HQ set up at Chatea\nVieux de Rochefort. Slight opposition met by A Coy on advance. Opposition met at\n\\Croic St Jean. A Coy was in position at 1700 hrs. Advance started at 1500 hrs. OP\nset up at 1900 hrs at MR 207753. Patrols sent out by all Coys. 7\nHeather wet all day. Snowed most of the day. 1 Pl from C Coy guarding bridge MR\n204767. A Coy sent a fighting patrol to clear Powder Mill woods MR 2074. Recce\natrols sent out byall coys. 7\neather fair all day. No enemy was seen during the day. A Coy sent out patrols during\ntthe day and night but no opposition memt. B Coy moved forward to MR 195771. Orders\nGroup held at 2000 hrs and orders were given to have all personnel ready to move to\nnew location by 1200 hrs on the 6 of Jan 1945. YMCA was to show a movie in the evenp\ning but the CO cancelled it. Two Polish deserters from the German army walked into\nCoy lines. 4\neather clear all day. CO, Coy Comds, Sig Officer and Vickers Officer left to recce\new location at 0830 hrs. Unit started to move to new location at 1200 hrs. Unit a\nrived at AYE MR 2683, Sheet 91, 1\" to mile at 1500 hrs. Personnel were shown to\ntheir areas and billets. | «\neather clear all day. Observation Post set up by the Intelligence Sec at MR 253813.) ,\nQuiet all day. No enemy activity during the day.\neather overcast and snowing. Intelligence Section set up another OP at MR 268814,\no enemy activity during thle day. At 2300 hrs orders were received that all personnel\nere to be ready to move to new area on the morning of the 9th Jan, 1945. ;\nWeather clear and cold. Bn started to move at 0830 hrs. Bn reached Champlon ‘\nFamenine, MR qibe at 1230 hrs. Bn relieved the HLI. Coys immediately took up\npositions or all around defence.\n"

Using stringr,这可以大致按句子结尾分割:


stringr::str_split(text, pattern = "\n")

[[1]]
 [1] "Weather clear all day. A smaii arms inspection hela at i400 hrs. A recce party went"    
 [2] "lout consisting of Coy Comds and Lt Col Nicklin, I.0. and Asst Adjt. An Orders group"   
 [3] "held in the evening. Pay parade for HQ and Bn HQ was at 1900 hrs. A movie was shown"    
 [4] "lfor B Coy personnel by our YMCA Supervisor. o"                                         
 [5] "Weather clear and cold all day. Personnel packed equipment early in the morning and |-~"
 [6] "were ready to move at 0830 hrs. Unit embussed at 0900 hrs and moved to Rochefort, MR"   
 [7] "2076, Sheet 105, 1/25000, arriving at 1390 hrs. Coys were in position at 1600 hrs. |,." 
 [8] "PW brought in by A Coy at 1900 hrs. PW was a deserter from 304 Regt 2 Pz division. | ." 
 [9] "© other activity during the day. Patrols were sent out during the night by all coys,."  
[10] "Cold all day. Very quiet all morning. A Coy moved forward. Coy HQ set up at Chatea"     
[11] "Vieux de Rochefort. Slight opposition met by A Coy on advance. Opposition met at"       
[12] "\\Croic St Jean. A Coy was in position at 1700 hrs. Advance started at 1500 hrs. OP"    
[13] "set up at 1900 hrs at MR 207753. Patrols sent out by all Coys. 7"                       
[14] "Heather wet all day. Snowed most of the day. 1 Pl from C Coy guarding bridge MR"        
[15] "204767. A Coy sent a fighting patrol to clear Powder Mill woods MR 2074. Recce"         
[16] "atrols sent out byall coys. 7"                                                          
[17] "eather fair all day. No enemy was seen during the day. A Coy sent out patrols during"   
[18] "tthe day and night but no opposition memt. B Coy moved forward to MR 195771. Orders"    
[19] "Group held at 2000 hrs and orders were given to have all personnel ready to move to"    
[20] "new location by 1200 hrs on the 6 of Jan 1945. YMCA was to show a movie in the evenp"   
[21] "ing but the CO cancelled it. Two Polish deserters from the German army walked into"     
[22] "Coy lines. 4"                                                                           
[23] "eather clear all day. CO, Coy Comds, Sig Officer and Vickers Officer left to recce"     
[24] "ew location at 0830 hrs. Unit started to move to new location at 1200 hrs. Unit a"      
[25] "rived at AYE MR 2683, Sheet 91, 1\" to mile at 1500 hrs. Personnel were shown to"       
[26] "their areas and billets. | «"                                                           
[27] "eather clear all day. Observation Post set up by the Intelligence Sec at MR 253813.) ," 
[28] "Quiet all day. No enemy activity during the day."                                       
[29] "eather overcast and snowing. Intelligence Section set up another OP at MR 268814,"      
[30] "o enemy activity during thle day. At 2300 hrs orders were received that all personnel"  
[31] "ere to be ready to move to new area on the morning of the 9th Jan, 1945. ;"             
[32] "Weather clear and cold. Bn started to move at 0830 hrs. Bn reached Champlon ‘"          
[33] "Famenine, MR qibe at 1230 hrs. Bn relieved the HLI. Coys immediately took up"           
[34] "positions or all around defence."                                                       
[35] ""          

有什么想法可以改进它以在每个末尾找到特定的模式吗?

我可以直接导出分割内容,然后从这里手动复制/粘贴到word中。

非常感谢!!


一个选择是使用preserve_interword_spaces以保留段落之间的大量间距。然后,您可以使用stringr分割一定数量或更大的空间

text <- image %>%
  image_crop(geometry_area(width = 1220, height = 900,
                           y_off = 260, x_off = 355)) %>% 
  image_resize("2000x") %>%
  image_convert(type = 'Grayscale') %>%
  image_trim(fuzz = 40) %>%
  image_write(format = 'png', density = '300x300') %>%
  tesseract::ocr(tesseract(options = list(preserve_interword_spaces = 1)))

stringr::str_split(text, pattern = "\\s{5,}")

这将粗略地拆分示例中的大部分段落:

[[1]]
[1] "Weather clear all day. A smail arms inspection heia at 1400 hrs. A recce party went\nlout consisting of Coy Comds and Lt Col Nicklin, I.0. and Asst Adjt. An Orders group\nheld in the evening. Pay parade for HQ and Bn HQ was at 1900 hrs. A movie was shown\nlfor B Coy personnel by our YMCA Supervisor."                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
[2] "“\nWeather clear and cold all day. Personnel packed equipment early in the morning and |-~\nwere ready to move at 0830 hrs. Unit embussed at 0900 hrs and moved to Rochefort, MR\n2076, Sheet 105, 1/25000, arriving at 1390 hrs. Coys were in position at 1600 hrs. |,\nPw brought in by A Coy at 1800 hrs. PW was a deserter from 304 Regt 2 Pz division. | .\n© other activity during the day. Patrols were sent out during the night by all coys,.\nCold all day. Very quiet all morning. A Coy moved forward. Coy HQ set up at Chatea\nVieux de Rochefort. Slight opposition met by A Coy on advance. Opposition met at\n(Croic St Jean. A Coy was in position at 1700 hrs. Advance started at 1500 hrs. OP\nset up at 1900 hrs at MR 207753. Patrols sent out by all Coys."
[3] "i\nHeather wet all day. Snowed most of the day. 1 Pl from C Coy guarding bridge IR\n204767. A Coy sent a fighting patrol to clear Powder Mill woods MR 2074. Recce\npatrols sent out byall coys."                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
[4] "‘\nWeather fair all day. No enemy was seen during the day. A Coy sent out patrols duri\nthe day and night but no opposition meat. B Coy moved forward to MR 195771. Orders\nGroup held at 2000 hrs and orders were given to have all personnel ready to move to\nnew location by 1200 hrs on the 6 of Jan 1945. YMCA was to show a movie in the evenp\ning but the CO cancelled it. Two Polish deserters from the German army walked into\nCoy lines."                                                                                                                                                                                                                                                                                                                           
[5] "4\neather clear all day. CO, Coy Comds, Sig Officer and Vickers Officer left to recce\new location at 0830 hrs. Unit started to move to new location at 1200 hrs. Unit    4\nrived at AYE MR 2683, Sheet 91, 1\" to mile at 1500 hrs. Personnel were shown to\ntheir areas and billets."                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
[6] ":\neather clear all day. Observation Post set up by the Intelligence Sec at MR 253813.\nQuiet all day. No enemy activity during the day.\nleather overcast and snowing. Intelligence Section set up another OP at MR 268814.\n© enemy activity during thle day. At 2300 hrs orders were received that all personnel\nere to be ready to move to new area on the morning of the 9th Jan, 1945.\nather clear and cold. Bn started to move at 0830 hrs. Bn reached Champlon"                                                                                                                                                                                                                                                                                                        
[7] "‘\nFPamenine, MR qis2 at 1230 hrs. Bn relieved the HLI. Coys immediately took up\npositions  or all around defence.\n"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

根据 ocr 图像结尾的段落将字符串拆分为列 的相关文章

  • 运行 R.exe 会创建临时文件吗?

    我在想 是否启动 R exewindows创建临时文件并 是否解释类似x lt 5写入那些临时文件 如果创建了临时文件 它们存储在哪里 如果我启动多个 R exe 实例会发生什么情况 他们会共享并覆盖彼此的临时文件吗 R 的每个实例都有自己
  • 加拿大人口普查地图分区 R

    我对 R 和映射非常陌生 我想创建某些数据的映射 我有一组名为 D Montreal 的数据 显示 2010 年前往蒙特利尔的加拿大人口普查部门游客来自哪个国家 我想使用此数据创建一个地图 以显示有多少人来自不同地区 也许可以通过对根据人数
  • 在闪亮的应用程序和多个页面中进行身份验证

    在我正在开发的系统中 我有 3 个不同的参与者 用户 管理员 支持团队 使用 Shiny App 我想知道如何向这三个参与者进行身份验证 每个参与者只能访问他们的页面 我发现使用闪亮的服务器专业版可以实现这一点 但它不是免费的 有什么方法可
  • R:如何添加具有从矩阵的每一行中随机选择的值的列?

    我会先说我是一个 R 菜鸟 我认为这可能有一个简单的解决方案 但我正在努力寻找它 我有一个 2 列 1 000 行的矩阵 保持行固定 我想创建一个新变量 从两列中随机选择一个元素 例如制作一个简单的矩阵 matrix c 1 1 4 6 1
  • 将阿拉伯文本分配给 R 变量

    R 无法正确显示阿拉伯文本 当我使用阿拉伯语时 我得到了非常奇怪的东西 这是一个屏幕截图 问题是我想创建一个带有阿拉伯文本的词云 我需要首先解决这个问题 R 版本 R 2 15 2 GUI 1 53 Leopard 版本 64 位 6335
  • ggmap 错误:GeomRasterAnn 是使用不兼容版本的 ggproto 构建的

    我正在使用 ggmap 并收到以下错误 Error GeomRasterAnn was built with an incompatible version of ggproto Please reinstall the package t
  • 从 data.frame 创建新列

    我有一个长格式的数据集 其中测量 时间 嵌套在 Networkpartners NP 中 而 Networkpartners NP 又嵌套在人员 ID 中 下面是它的示例 真实数据集有数千行 ID NP Time Outcome 1 11
  • 在 R 中使用 gamlss::lms 选择百分位数曲线

    我正在使用 gamlss 包中的示例代码来绘制百分位数曲线 library gamlss data abdom lms y x data abdom n cyc 30 它正在绘制自己的一组百分位数曲线 如何选择只绘制第 10 50 和 90
  • 为什么安装的“igraph”包在加载后返回“libicui18n.so.58:无法打开共享对象文件”?

    我试图使用命令 install packages igraph 在 R 中安装 igraph 包 安装后 在测试阶段出现以下错误 测试是否可以加载已安装的包错误 包或命名空间 dyn load file DLLpath DLLpath 中的
  • 网页抓取(R 语言?)

    我想获取中间栏中的公司名称this http www consumercomplaints in bysubcategory mobile service providers page 1 html页面 以蓝色粗体书写 以及登记投诉者的位置
  • R 的 dplyr 切片中的奇怪行为

    打电话时slice df i 在 R 的 dplyr 包中 如果我要求的行索引不存在 nrows lt i 它似乎返回除组中的第一行之外的所有行 就像我调用的那样slice df 1 例如 library dplyr c1 lt c a b
  • (R 错误)错误:cons 内存耗尽(达到限制?)

    我正在处理大数据 并且有一个 70GB 的 JSON 文件 我正在使用 jsonlite 库将文件加载到内存中 我尝试过 AWS EC2 x1 16large 机器 976 GB RAM 来执行此负载 但 R 因错误而中断 Error co
  • r 连接两个列表并对它们的值求和

    我有两个列表 x y gt x carlo monte simulation model quantum 31 31 9 6 6 gt y model system temperature quantum simulation proble
  • 如何用月份的全名替换数字月份

    使用 tidyverse 包将月份的列更改为完整的实际月份名称 请记住 尽管这些数据只有四个月 但我的真实数据集包含一年中的所有实际月份 我是 tidyverse 的新手 mydata lt tibble camp c Platinum 2
  • 如何对工作区中的元素运行循环?

    gt ls 1 A anorex 1 anorexia B byMonth C clotting 8 counts d D d AD DelayByDay delayed glm D93 15 glmout groups h housing
  • plot xts if (on == "years") { 中的错误:缺少 TRUE/FALSE 需要的值

    我正在尝试绘制 xts 对象 但出现有关年份的错误 xts 对象只有一个数值和一个 POSIXct 索引 下面的代码显示了 xts 和尝试绘图时的错误 关于需要对 xts 对象做什么才能正确绘制的任何想法 xTest lt as xts 3
  • Tesseract 是否会忽略扫描文档中的任何非文本区域?

    我正在使用 Tesseract 但我不知道它是否忽略任何非文本区域并仅针对文本 我是否必须删除任何非文本区域作为预处理步骤以获得更好的输出 Tesseract 有一个非常好的算法来检测文本 但它最终会给出误报匹配 理想情况下 您应该在将图像
  • dplyr 总结小计

    Excel 中数据透视表的一大优点是它们会自动提供小计 首先 我想知道 dplyr 中是否已经创建了任何可以实现此目的的东西 如果没有 实现它的最简单方法是什么 在下面的示例中 我按气缸和化油器的数量显示了平均排量 对于每组气缸 4 6 8
  • 如何在r中进行左连接[重复]

    这个问题在这里已经有答案了 我有两个数据集一和二 数据集一 a b c 111 a 1 112 b 2 113 c 3 114 d 4 115 e 5 数据集二 e d g 222 ss 11 111 ff 22 113 ww 33 114
  • 删除绘图轴值

    我只是想知道是否有一种方法可以消除 r 图中的轴值 分别是 x 轴或 y 轴 我知道axes false将摆脱整个轴 但我只想摆脱编号 删除 x 轴或 y 轴上的编号 plot 1 10 xaxt n plot 1 10 yaxt n 如果

随机推荐

  • IE7清除浮动问题

    您好 这是我在使用 IE7 时遇到的问题的简化版本 基本上 清除的 div 绿色 之后的 div 不会按预期运行 在 IE7 中 它在 Safari FF 等和 IE8 中按预期工作 有人有任何修复建议吗 谢谢你的帮助
  • Angular Material 更改应用程序背景和前景主题

    这个问题 https stackoverflow com questions 43919927 关于在 Angular Material 中设置背景颜色的问题于 2017 年发布 答案已过时 最新版本的 Angular Material 1
  • JPQL:将 varchar 转换为数字

    如何将 varchar 转换为 JPQL 中的数字 我设法在 sql Oracle 中使用以下查询来做到这一点 SELECT Decode Upper column name Lower column name To Number colu
  • 处理 argparse 冲突

    如果我导入一个Pythonmodule https github com paulcalabro api kickstart blob master examples python config py已经在使用argparse 但是 我想用
  • 按具有自定义排序优先级的列对数组的数组进行排序(不按字母顺序)

    假设我有这个数组 array array array id gt 7867867 animal gt Dog array id gt 3452342 animal gt Lion array id gt 1231233 animal gt
  • PySpark:当列是列表时向 DataFrame 添加列

    我读过类似的问题 但找不到解决我的具体问题的方法 我有一个清单 l 1 2 3 和一个数据框 df sc parallelize p1 a p2 b p3 c toDF product name 我想获得一个新的 DataFrame 其中列
  • struts 2 中的 beans 属性和 modelDriven

    我是 struts 2 的新手 我创建了一个操作类 它使用以下命令将数据从 JSP 页面插入到 beanModelDriven界面 问题是我有一些非 字符串 properties in the bean likeLong 日期 等 问题是
  • Chrome 多次发送请求

    我有一个 Rails 3 应用程序 刚刚注意到当使用 Chrome 在 Mac 上 访问该应用程序时 网站上的每个页面都会被请求两次 这发生在开发和生产中 Heroku Firefox 和 Safari 仅发送一次请求 即使我删除该操作的所
  • Pandas:将一系列字典保存到磁盘

    我有一个 python pandas 系列词典 id dicts 1 5 1 8 20 1800 2 2 2 2 8 1 1000 25 1651 1 20000000 2 1 10 20 字典中的 key value 代表 feature
  • Ant 公然忽略 JAVA_HOME 环境变量

    我刚刚获取了最新版本的 Android SDK 并开始尝试使用它 与几乎其他人提出这个问题不同 我运行的是 Linux 即 Linux Mint 13 我目前正在尝试使用 ant 编译一个 Hello World 程序 通过安装 sudo
  • HeaderViewListAdapter 随机崩溃

    我在 crashlytics 中遇到随机崩溃 但无法模拟它 它在 Android V2 和 V4 上都会随机崩溃 但是非常非常罕见 java lang IndexOutOfBoundsException Invalid index 3 si
  • CLion 在运行可执行文件时找不到共享库

    我正在做一个项目 到目前为止 我一直在使用一个简单的编辑器和我自己的 Makefile 来构建它 不过 我想切换到 CLion 根据这个问题 https stackoverflow com questions 26918459 using
  • 使用 XMLHttpRequest 发送 POST 数据

    我想在 JavaScript 中使用 XMLHttpRequest 发送一些数据 假设我有以下 HTML 表单
  • 在我的笔记本电脑上运行 MPI 代码

    我是并行计算世界的新手 你能告诉我是否可以在我的双核笔记本电脑上使用 MPI 例程运行 C 代码 或者是否有任何模拟器 仿真器可以做到这一点 大多数 MPI 实现使用共享内存在位于同一主机上的队列之间进行通信 设置笔记本电脑不需要任何特殊操
  • Spring Boot 中用户主目录的环境特定属性

    我正在开发一个 Spring Boot 应用程序 我必须在其中设置Environment specific properties从用户主文件夹 我在 Google 上找到了同样的结果 发现我们可以将不同的属性文件 开发 测试 生产 放在资源
  • COM 无法启动编译为 AnyCPU 的进程外 .Net 服务器

    我正在尝试让 COM 启动我的进程外 NET COM 服务器 如果服务器进程是用 x64 编译的 它就可以工作 但如果我使用 AnyCPU 这就是我想要的 那么它会挂起一段时间 最终失败并显示 0x80080005 CO E SERVER
  • 如何向 VisualVM 提供 JVM 参数?

    我使用 JDK 1 6 0 26 中的 VisualVM 来分析在 Tomcat 下运行的 Java Web 应用程序 但 VisualVM 经常告诉我它没有足够的内存来拍摄快照 并使用 Xmx 开关为 Netbeans 提供更多内存 问题
  • appcompat_v7 abc_activity_chooser_view.xml 中的重复 id @+id/image:58

    创建 apk 时出现以下错误 重复的 id id image 已在此布局中先前定义 abc activity chooser view xml 布局中的 58 appcompat v7 于是我就清理了一下 还是这样 该文件中确实存在重复的
  • 括号检查器代码的测试用例不正确。对于“(()”输出应该是“不平衡”,但我正在“平衡”[重复]

    这个问题在这里已经有答案了 给定一个表达式字符串 exp 检查是否是对和顺序 表达式中是正确的 例如 程序应该打印 balanced for exp and not balanced for exp Input 输入的第一行包含一个整数 T
  • 根据 ocr 图像结尾的段落将字符串拆分为列

    我正在开发一个项目 将打字机写的战争日记笔记从 PDF 扫描转换为文本 我可以成功地 对于原始的未调整大小的文件 可能是 90 提取我首先裁剪的主要文本 Reprex 数据 您可以从头开始尝试使用图像或我在下面提供的文本 我的挑战是保持文本