Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
从R列中提取字符串的变体_R_Nlp_Text Mining - Fatal编程技术网

从R列中提取字符串的变体

从R列中提取字符串的变体,r,nlp,text-mining,R,Nlp,Text Mining,我有一个关键字列表 keywords=c("Minister", "President","Secretary") 我有一个列,在不同的行中有不同的文本 column=c("he is general Secretary of Ozon group", "He is vice president of our college", "He is health minister", "He is education minister") 是否有任何方法可以根据关键字提取列中的变体 我正在寻找的

我有一个关键字列表

keywords=c("Minister", "President","Secretary")
我有一个列,在不同的行中有不同的文本

column=c("he is general Secretary of Ozon group", "He is vice president of 
our college", "He is health minister", "He is education minister")
是否有任何方法可以根据关键字提取列中的变体

我正在寻找的输出是

output=c("general Secretary","vice president", "education minister", "health minister")

如果您试图提取关键字+任何前面的单词,您可以这样做:

pat <- paste0("\\w+\\s(", paste(keywords, collapse = "|"), ")")
regmatches(column, gregexpr(pat, column, ignore.case = TRUE))
#[[1]]
#[1] "general Secretary"
#
#[[2]]
#[1] "vice president"
#
#[[3]]
#[1] "health minister"
#
#[[4]]
#[1] "education minister"

pat通常是您想要与关键字一起捕获的“前任部长/总统”这个词吗?是的……但是对于其他关键字,例如“首相秘书”,它也可以是尾随词,那么您肯定需要提供一些逻辑来识别您想要捕获的其他词extract@docendodiscimus你能帮我吗。我刚从R中的文本开始,发现很难思考如何决定是否要用关键字捕获前面的单词或后面的单词。有什么规则来决定这件事?
library(stringr)
pat <- paste0("\\w+\\s(", paste(tolower(keywords), collapse = "|"), ")")
str_extract_all(tolower(column), pat)