gpt4 book ai didi

R:多个连续 gsub 的函数式方法

转载 作者:行者123 更新时间:2023-12-01 05:50:42 32 4
gpt4 key购买 nike

我一直在纠结这个问题,尝试了很多种类的 map , Reduce并且到目前为止还没有成功。

我正在寻找一种功能性、优雅的方法来替换 gsub 的序列如

text_example <- c(
"I'm sure dogs are the best",
"I won't, I can't think otherwise",
"We'll be happy to discuss about dogs",
"cant do it today tho"
)

text_example %>%
gsub(pattern = "'ll", replacement = " will") %>%
gsub(pattern = "can'?t", replacement = "can not") %>%
gsub(pattern = "won'?t", replacement = "will not") %>%
gsub(pattern = "n't", replacement = " not") %>%
gsub(pattern = "'m", replacement = " am") %>%
gsub(pattern = "'s", replacement = " is") %>%
gsub(pattern = "dog", replacement = "cat") %>%

变成某种形式
text_example %>% 
???(dict$pattern, dict$replacement, gsub())

其中,为了可重现的示例, dict可以是一个data.frame,如
dict <- structure(
list(
pattern = c("'ll", "can'?t", "won'?t", "n't", "'m", "'s", "dog"),
replacement = c(" will", "can not", "will not", " not", " am", " is", "cat")
),
row.names = c(NA, -7L),
class = "data.frame"
)

(我知道执行的替换在语言上可能不正确,但这不是现在的问题)

当然,残酷的
for(i in seq(nrow(dict))) {

text_example <- gsub(dict$pattern[i], dict$replacement[i], text_example)

}

会工作,并且 我知道有许多库可以通过某些特定功能解决此问题。但我想了解如何以简单、实用的方式处理递归和此类问题 ,尽可能接近基础 R。我爱我的 lambdas!

预先感谢您的帮助。

最佳答案

您可以使用 mapply对于并行应用效果:

mapply(dict$pattern, dict$replacement, function(pttrn, rep) gsub(pttrn, rep, text_example))

(您可能想使用 SIMPLIFY=FALSE )

关于R:多个连续 gsub 的函数式方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54631393/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com