如何使用 R 提取包含特定人名的句子

2024-04-24

我正在使用 R 来提取包含特定人名的句子来自文本,这是一个示例段落:

在蒂宾根,他作为改革家遭到反对,但在他的叔公约翰·罗伊克林的推荐下,他接受了马丁·路德的召唤,进入维滕贝格大学。梅兰希顿 21 岁时成为维滕贝格的希腊语教授。他研究圣经,尤其是保罗的圣经和福音派教义。他作为旁观者出席了莱比锡辩论(1519),但通过评论参与其中。约翰·埃克(Johann Eck)攻击了他的观点,梅兰希顿(Melanchthon)根据《辩护反对约翰·埃克乌姆》(Defensio contra Johannem Eckium)中圣经的权威进行了回应。

在这短短的一段话里,有好几个人名,比如:约翰·罗伊克林, 梅兰希顿, 约翰·艾克。在...的帮助下openNLP包裹,三个人的名字马丁路德, Paul and 梅兰希顿可以正确提取并识别。然后我有两个问题:

  1. 我怎样才能提取包含这些名字的句子?
  2. 由于命名实体识别器的输出不太有希望,如果我将“[[ ]]”添加到每个名称,例如 [[Johann Reuchlin]]、[[Melanchthon]],我该如何提取包含这些名称表达的句子[[A]]、[[B]] ...?

Using `strsplit` and `grep`, first I set made an object `para` which was your paragraph.

toMatch <- c("Martin Luther", "Paul", "Melanchthon")

unlist(strsplit(para,split="\\."))[grep(paste(toMatch, collapse="|"),unlist(strsplit(para,split="\\.")))]


> unlist(strsplit(para,split="\\."))[grep(paste(toMatch, collapse="|"),unlist(strsplit(para,split="\\.")))]
[1] "Opposed as a reformer at Tübingen, he accepted a call to the University of Wittenberg by Martin Luther, recommended by his great-uncle Johann Reuchlin"
[2] " Melanchthon became professor of the Greek language in Wittenberg at the age of 21"                                                                    
[3] " He studied the Scripture, especially of Paul, and Evangelical doctrine"                                                                               
[4] " Johann Eck having attacked his views, Melanchthon replied based on the authority of Scripture in his Defensio contra Johannem Eckium"    

或者更干净一点:

sentences<-unlist(strsplit(para,split="\\."))
sentences[grep(paste(toMatch, collapse="|"),sentences)]

如果您正在寻找每个人所在的句子作为单独的返回,那么:

toMatch <- c("Martin Luther", "Paul", "Melanchthon")
sentences<-unlist(strsplit(para,split="\\."))
foo<-function(Match){sentences[grep(Match,sentences)]}
lapply(toMatch,foo)

[[1]]
[1] "Opposed as a reformer at Tübingen, he accepted a call to the University of Wittenberg by Martin Luther, recommended by his great-uncle Johann Reuchlin"

[[2]]
[1] " He studied the Scripture, especially of Paul, and Evangelical doctrine"

[[3]]
[1] " Melanchthon became professor of the Greek language in Wittenberg at the age of 21"                                                   
[2] " Johann Eck having attacked his views, Melanchthon replied based on the authority of Scripture in his Defensio contra Johannem Eckium"

编辑 3:要添加每个人的姓名,请执行一些简单的操作,例如:

foo<-function(Match){c(Match,sentences[grep(Match,sentences)])}

EDIT 4:

如果您想查找包含多个人/地点/事物(单词)的句子,则只需为这两个添加一个参数,例如:

toMatch <- c("Martin Luther", "Paul", "Melanchthon","(?=.*Melanchthon)(?=.*Scripture)")

并改变perl to TRUE:

foo<-function(Match){c(Match,sentences[grep(Match,sentences,perl = T)])}


> lapply(toMatch,foo)
[[1]]
[1] "Martin Luther"                                                                                                                                         
[2] "Opposed as a reformer at Tübingen, he accepted a call to the University of Wittenberg by Martin Luther, recommended by his great-uncle Johann Reuchlin"

[[2]]
[1] "Paul"                                                                   
[2] " He studied the Scripture, especially of Paul, and Evangelical doctrine"

[[3]]
[1] "Melanchthon"                                                                                                                          
[2] " Melanchthon became professor of the Greek language in Wittenberg at the age of 21"                                                   
[3] " Johann Eck having attacked his views, Melanchthon replied based on the authority of Scripture in his Defensio contra Johannem Eckium"

[[4]]
[1] "(?=.*Melanchthon)(?=.*Scripture)"                                                                                                     
[2] " Johann Eck having attacked his views, Melanchthon replied based on the authority of Scripture in his Defensio contra Johannem Eckium"

编辑5:回答你的其他问题:

Given:

sentenceR<-"Opposed as a reformer at [[Tübingen]], he accepted a call to the University of [[Wittenberg]] by [[Martin Luther]], recommended by his great-uncle [[Johann Reuchlin]]"

gsub("\\[\\[|\\]\\]", "", regmatches(sentenceR, gregexpr("\\[\\[.*?\\]\\]", sentenceR))[[1]])

会给你双括号内的单词。

> gsub("\\[\\[|\\]\\]", "", regmatches(sentenceR, gregexpr("\\[\\[.*?\\]\\]", sentenceR))[[1]])
[1] "Tübingen"        "Wittenberg"      "Martin Luther"   "Johann Reuchlin"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 R 提取包含特定人名的句子 的相关文章

  • 将 RDS 文件从网络(即 URL)直接加载到 R 中?

    read csv 具有直接从 url 读取的出色能力 readRDS 才不是 我想将 RDS 文件从 Internet 移动到我的 R 环境 我看到有几种方法 Method 1 此方法会用下载的文件弄乱工作目录 myurl lt https
  • jupyter 中的 r 图形 - 无法启动 png() 设备

    我在 Jupyter 中使用 R 但无法在笔记本本身中绘制图表 这是一个可重现的示例 set seed 123 mat as matrix x rnorm 100 y rnorm 100 plot mat 在朱皮特中 Error in pn
  • 需要 RegEx 返回第一段或前 n 个单词

    我正在寻找一个正则表达式来返回段落中的前 n 个单词 或者如果该段落包含少于 n 个单词 则返回完整的段落 例如 假设我最多需要前 7 个单词 p one two p
  • 访问 R 工作区中的数据[重复]

    这个问题在这里已经有答案了 我是自学 R 的 可能有一些非常基本的东西我可能不熟悉 如果是这样我道歉 我正在尝试访问外部来源提供给我的数据 它作为一个工作空间出现 我的流程如下 gt ls 1 2003OHT HR gt attach 20
  • 如何使用 sprintf 函数在字符中添加前导“0”而不是空格?

    我正在尝试使用sprintf函数为字符添加前导 0 并使所有字符长度相同 然而我得到的是领先空间 My code a lt c 12 123 1234 sprintf 04s a 1 12 123 1234 我试图得到什么 1 0012 0
  • 正则表达式允许零,只要它不是第一个数字[重复]

    这个问题在这里已经有答案了 昨天我在这里发布了一个问题正则表达式允许 null 或 1 到 9 数字 https stackoverflow com questions 40354842 regular expression allow n
  • 如何有效地将多个光栅 (.tif) 文件导入 R

    我是 R 新手 尤其是在空间数据方面 我正在尝试找到一种方法来有效地将多个 600 单波段栅格 tif 文件导入到 R 中 所有文件都存储在同一文件夹中 不确定这是否重要 但请注意 在我的 Mac 和 Windows 并行 VM 上的文件夹
  • 将所有分号替换为空格 pt2

    我尝试对 2000 多行关键字的列表运行文本分析 但它们的列出方式如下 战略 管理风格 组织 所以当我使用 tm 删除标点符号时 它就变成了 组织的战略管理风格 我认为这在某种程度上破坏了我常用术语的分析 我尝试过使用 vector lt
  • 如何从 pandas 数据框中的列中删除字符串值

    我正在尝试编写一些代码 以逗号分隔数据帧列中的字符串 因此它成为一个列表 并从该列表中删除某个字符串 如果存在 删除不需要的字符串后 我想再次以逗号加入列表元素 我的数据框如下所示 df Column1 Column2 0 a a b c
  • 为每个因素级别添加日期时间序列

    我有一个带有因子列的数据框 s lt data frame id 901 910 s id lt as factor s id 我有一个日期时间序列 library lubridate start lt now as difftime 2
  • 你能挽救我的负面回顾示例来传达数字吗?

    在 高级正则表达式 一章中掌握 Perl http oreilly com catalog 9780596527242 我有一个损坏的示例 我无法找到一个很好的修复方法 这个例子可能为了自己的利益而试图变得太聪明 但也许有人可以帮我解决它
  • R data.table 多个条件连接

    我设计了一种解决方案 用于从两个单独数据表的多个列中查找值 并添加基于新列的值计算 多个条件比较 代码如下 它涉及在计算两个表中的值时使用 data table 和联接 但是 这些表没有联接在我正在比较的列上 因此我怀疑我可能无法获得 da
  • 根据 R 数据框中的名称对列进行平均

    我想知道是否有一种有效的方法来获取每组的平均值类似命名的列谁的名字结尾为 1S and 2S ex ex1S ex2S at time 1并取每组的平均值类似命名的列谁的名字结尾为 1C or 2C ex ex1C ex2C at time
  • 如何读取 R 中的每个 .csv 文件并将其导出到单个大文件中

    你好 我有以下格式的数据 101 20130826T155649 3 1 round 0 10552 180 yellow 12002 1 round 1 19502 150 yellow 22452 1 round 2 28957 130
  • 根据 row_number() 过滤 data.frame

    更新 自从提出这个问题以来 dplyr 已经更新 现在按照 OP 的要求执行 我正在尝试获取第二行到第七行data frame using dplyr 我正在这样做 require dplyr df lt data frame id 1 1
  • 在 Java 正则表达式中获取多个模式的重叠匹配

    我有同样的问题这个链接 https stackoverflow com questions 18751486 matching one string multiple times using regex in java 但有多种模式 我的正
  • 空间数据xyz到矩阵

    我有一个大数据框 100 000 行 其中包含 LON LAT VALUE 我想将其转换为矩阵 EPSG 中的坐标 3035 我使用以下命令尝试了 reshape2 包 acast df lon lat value var value 效果
  • Python re无限执行

    我正在尝试执行这段代码 import re pattern r w w s re compiled re compile pattern results re compiled search COPRO HORIZON 2000 HOR p
  • 为什么无法从 WEB-INF 文件夹内加载 POSModel 文件?

    我在我的 Web 项目中使用 Spring MVC 我将模型文件放在 WEB INF 目录中 String taggerModelPath WEB INF lib en pos maxent bin String chunkerModelP
  • 通过 R 中的数据子集执行计算

    我想对数据框的 PERMNO 列中的每个公司编号进行计算 其摘要可以在此处查看 gt summary companydataRETS PERMNO RET Min 10000 Min 0 971698 1st Qu 32716 1st Qu

随机推荐