r - 替换 R 中的单词
问题描述
我反对他们的同义词。在不同的数据框中,我有句子。我想从其他数据框中搜索同义词。如果找到,将其替换为找到同义词的单词。
dt = read.table(header = TRUE,
text ="Word Synonyms
Use 'employ, utilize, exhaust, spend, expend, consume, exercise'
Come 'advance, approach, arrive, near, reach'
Go 'depart, disappear, fade, move, proceed, recede, travel'
Run 'dash, escape, elope, flee, hasten, hurry, race, rush, speed, sprint'
Hurry 'rush, run, speed, race, hasten, urge, accelerate, bustle'
Hide 'conceal, cover, mask, cloak, camouflage, screen, shroud, veil'
", stringsAsFactors= F)
mydf = read.table(header = TRUE, , stringsAsFactors= F,
text ="sentence
'I can utilize this file'
'I can cover these things'
")
所需的输出看起来像 -
I can Use this file
I can Hide these things
以上只是一个示例。在我的真实数据集中,我有超过 10000 个句子。
解决方案
可以将,
in替换为dt$Synonyms
,|
以便将其用作 的pattern
参数gsub
。现在,dt$Synonyms
用作模式并将出现的任何单词(由 分隔|
)替换为dt$word
. 可以使用sapply
和gsub
作为:
已编辑:gsub
按照 OP 的建议添加了字边界检查(作为模式的一部分)。
# First replace `, ` with `|` in dt$Synonyms. Now dt$Synonyms can be
# used 'pattern' argument of `gsub`.
dt$Synonyms <- paste("\\b",gsub(", ","\\\\b|\\\\b",dt$Synonyms),"\\b", sep = "")
# Loop through each row of 'dt' to replace Synonyms with word using sapply
mydf$sentence <- sapply(mydf$sentence, function(x){
for(row in 1:nrow(dt)){
x = gsub(dt$Synonyms[row],dt$Word[row], x)
}
x
})
mydf
# sentence
# 1 I can Use this file
# 2 I can Hide these things
推荐阅读
- python - 使用 sqlite3 的带有 UUID 字段的 Django ORM
- pandas - 提取一位和两位数小时时间熊猫
- ffmpeg - 并排合并两个视频ffmpeg
- laravel - 如何在#myTickets div 中显示注册信息?
- python - 如何连接 1d 和 0d numpy 数组的混合列表?
- javascript - React 的 IE 11 问题
- python - 为什么在加载时序列化的 numpy random_state 对象不同?
- assembly - IDA pro 不显示像 ollyDBG 这样的汇编代码
- ios - 某些 iPad 设备返回此代码不提供的生物识别信息,为什么?
- jenkins - Jenkins:获取在管道内触发的作业的内部版本号