首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >匹配并放入类别-R的正则表达式

匹配并放入类别-R的正则表达式
EN

Stack Overflow用户
提问于 2018-11-07 12:09:51
回答 2查看 355关注 0票数 0

我有三个矢量。一个包含文本或实际单词/句子(文本),一个向量包含我要搜索的单词(xreg),第三个向量(类别)包含每个文本如果找到匹配应该属于的类别。以下是三个向量:

代码语言:javascript
运行
复制
text <- c("Sole Service here", "Freedom to Include","Freedom to Incl","Premier Reg",
"Bankhall","Bankhall","Premier Regiona","St James Play",
"Premier Regional","Health online","Premier Regional",
"Tenet","Health on line","Tenet","Nations","Woolwich",
"Premier Regional","Lifesearch","Nations","Bankhall",
"Premier Regional","Sole Service her","Lifesearch",
"Premier Regional","Sole Service","Nations",
"Sole Service","First Money service","Sole Service",
"Nations wide","Sole Service","Premier Region")

text <- tolower(text)

xreg <- c("sole","freedom","premier","bankhall","james","health","tennet",
          "nations","woolwich","life","money")

categories <- c("SS", "FD", "PR", "BK", "JM", "HT", "TT", "NT", "WW", "LF", "MY")

我想根据‘xreg’向量中的搜索词搜索‘文本’向量。然后,在找到匹配项后,我想将这些单词放入“类别”向量中提到的类别中。

比如,查找单词‘state’,在有匹配的地方,记下该单词的索引,或者简单地用单词创建一个数据框架,然后单独一个列来说明它应该属于的类别。在“独家”的情况下,把它放在“SS”类别中。“自由”把它归为“FD”类等等。

到目前为止,解决方案:,我可以逐个搜索每个关键字,它会告诉我它在哪里找到匹配的索引。

代码语言:javascript
运行
复制
 reg_func <- function(x){grep(x,text)  
    }
    reg_func("sole")
reg_func("freedom")

这将为每个匹配的单词提供索引,然后我可以使用这些索引来更新类别。有什么办法能让我做得更快吗?而不是一次只搜索一个词?谢谢

EN

Stack Overflow用户

发布于 2018-11-12 15:46:25

解释@Andre Elrico答案中使用的函数

代码语言:javascript
运行
复制
apply(
  sapply(xreg, function(x) {grepl(x, text, ignore.case = T)}), 1, function(x) xreg[x]
)

# Apply each xreg pattern to the text vector and see if there's a match  
# result is TRUE or FALSE gives each index where there is a match
sapply(xreg, function(x) {grepl(x, text, ignore.case = T)})

结果

代码语言:javascript
运行
复制
      sole freedom premier bankhall james health tennet nations woolwich  life money
[1,]  TRUE   FALSE    TRUE    FALSE FALSE  FALSE  FALSE   FALSE    FALSE FALSE FALSE
[2,] FALSE    TRUE   FALSE    FALSE FALSE  FALSE  FALSE   FALSE    FALSE FALSE FALSE
[3,] FALSE    TRUE   FALSE    FALSE FALSE  FALSE  FALSE   FALSE    FALSE FALSE FALSE
[4,] FALSE   FALSE    TRUE    FALSE FALSE  FALSE  FALSE   FALSE    FALSE FALSE FALSE
[5,] FALSE   FALSE   FALSE     TRUE FALSE  FALSE  FALSE   FALSE    FALSE FALSE FALSE
[6,] FALSE   FALSE   FALSE     TRUE FALSE  FALSE  FALSE   FALSE    FALSE FALSE FALSE

# Now apply each xreg element to the TRUE's from the previous result 
# and see which element of xreg it matches with
apply(
  sapply(xreg, function(x) {grepl(x, text, ignore.case = T)}), 1, function(x) xreg[x]
)

结果

代码语言:javascript
运行
复制
[[1]]
[1] "sole"    "premier"

[[2]]
[1] "freedom"

[[3]]
[1] "freedom"

[[4]]
[1] "premier"

[[5]]
[1] "bankhall"

[[6]]
[1] "bankhall"

现在,获取每个匹配项的类别(Regex)

代码语言:javascript
运行
复制
sapply(ans$xreg_m, function(x) unique(unname( categories[x] )))

上面写着:

代码语言:javascript
运行
复制
# Take each element of xreg_m (our matched terms) and 
# see which element in the categories vector it matches with 
#  Then unname the result so you only get the category
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53189208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档