Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex R正则表达式:提取脚本中的说话人_Regex_R_Text Mining - Fatal编程技术网

Regex R正则表达式:提取脚本中的说话人

Regex R正则表达式:提取脚本中的说话人,regex,r,text-mining,Regex,R,Text Mining,我想使用R从如下示例中格式的脚本中提取说话人: “第六幕:第二个领主:不,好的,我的主人,让他去吧;让他按自己的方式去做。第一领主:如果你的领主发现他不是一个希尔丁,请不要再尊敬我了。第二主:我的生命,我的主,一个泡沫。伯特伦:你认为我在他身上受骗了吗?二主:相信吧,我的主,据我自己所知,毫无恶意,但说他是我的亲属,他是一个最著名的懦夫,一个无穷无尽的骗子,一个每小时都在违背诺言的人,没有一个品质好的人值得阁下娱乐。” 在这个例子中,我想摘录:(“第二领主”、“第一领主”、“第二领主”、“伯特伦

我想使用R从如下示例中格式的脚本中提取说话人:

“第六幕:第二个领主:不,好的,我的主人,让他去吧;让他按自己的方式去做。第一领主:如果你的领主发现他不是一个希尔丁,请不要再尊敬我了。第二主:我的生命,我的主,一个泡沫。伯特伦:你认为我在他身上受骗了吗?二主:相信吧,我的主,据我自己所知,毫无恶意,但说他是我的亲属,他是一个最著名的懦夫,一个无穷无尽的骗子,一个每小时都在违背诺言的人,没有一个品质好的人值得阁下娱乐。”

在这个例子中,我想摘录:(“第二领主”、“第一领主”、“第二领主”、“伯特伦”、“第二领主”)。规则很明显:它是位于句子末尾和半列之间的一组单词


我如何用R写这个?

可能是这样的:

library(stringr)  
body <- "Scene 6: Second Lord: Nay, good my lord, put him to't; let him have his way. First Lord: If your lordship find him not a hilding, hold me no more in your respect. Second Lord: On my life, my lord, a bubble. BERTRAM: Do you think I am so far deceived in him? Second Lord: Believe it, my lord, in mine own direct knowledge, without any malice, but to speak of him as my kinsman, he's a most notable coward, an infinite and endless liar, an hourly promise-breaker, the owner of no one good quality worthy your lordship's entertainment." 
p <- str_extract_all(body, "[:.?] [A-z ]*:")

# and get rid of extra signs
p <- str_replace_all(p[[1]], "[?:.]", "")
# strip white spaces
p <- str_trim(p)
p
"Second Lord" "First Lord"  "Second Lord" "BERTRAM"     "Second Lord"

# unique players
unique(p)
[1] "Second Lord" "First Lord"  "BERTRAM"  

gsubfn/strapplyc

尝试此操作,其中
x
是输入字符串。此处
Straplyc
返回括号内的匹配部分:

> library(gsubfn)
> strapplyc(x, "[.?:] *([^:]+):", simplify = c)
[1] "Second Lord" "First Lord"  "Second Lord" "BERTRAM"     "Second Lord"
gregexpr

这里是第二种方法。它不使用外部包。这里我们计算开始和结束位置(
start.pos
end.pos
),然后提取它们定义的字符串:

> pos <- gregexpr("[.?:] [^:]+:", x)[[1]]
> start.pos <- pos + 2
> end.pos <- start.pos + attr(pos, "match.length") - 4
> substring(x, start.pos, end.pos)
[1] "Second Lord" "First Lord"  "Second Lord" "BERTRAM"     "Second Lord"
>pos start.pos end.pos子字符串(x,start.pos,end.pos)
[1] “二等勋爵”“一等勋爵”“二等勋爵”“伯特伦”“二等勋爵”

至少在这种情况下,更好的解决方案是以更结构化的形式搜索文本。挖掘结构化文档几乎总是比非结构化文档更容易。因为源代码是莎士比亚,所以互联网上有很多副本

script_url <- "http://www.opensourceshakespeare.org/views/plays/play_view.php?WorkID=allswell&Act=3&Scene=6&Scope=scene"
doc <- htmlParse(script_url)
character_links <- xpathApply(doc, '//li[@class="playtext"]/strong/a')
characters <- unique(sapply(character_links, xmlValue))
#[1] "Second Lord" "First Lord"  "Bertram"     "Parolles"

script\u url非常感谢!还有一件事:如何获取表达式在字符串中的位置?是否愿意解释regex语句的作用?@cafe876我认为这需要另一个问题。提示:
gregexpr(“第二主:”,body)
。非常感谢添加这两种方法!非常感谢。
script_url <- "http://www.opensourceshakespeare.org/views/plays/play_view.php?WorkID=allswell&Act=3&Scene=6&Scope=scene"
doc <- htmlParse(script_url)
character_links <- xpathApply(doc, '//li[@class="playtext"]/strong/a')
characters <- unique(sapply(character_links, xmlValue))
#[1] "Second Lord" "First Lord"  "Bertram"     "Parolles"
script_url2 <- "http://www.bartleby.com/70/2236.html"
doc2 <- htmlParse(script_url2)
tbl <- xpathApply(doc2, '//table[@width="100%"]')[[1]]
italics <- xpathApply(tbl, '//tr/td/i')
characters2 <- unique(sapply(italics, xmlValue))
#[1] "First Lord." "Sec. Lord."  "Ber."        "Par."        "hic jacet."  "Exit."      
#[7] "Ber"         "Exeunt."