【发布时间】:2020-07-04 10:11:05
【问题描述】:
我有一个句子的数据框和一个关键词及其同义词的数据框。我想查看句子的每一行并将找到的任何同义词替换为适当的关键字。在过去的几天里,我一直在努力解决这个问题,但运气不佳。因此,您可以提供任何建议将不胜感激!
样本数据:
sentences <- data.frame( ID = c( "1", "2", "3", "4"),
text = c("the kitten in the hat",
"a dog with a bone",
"this is a category",
"their cat has no hat"),
stringsAsFactors=FALSE)
lookup <- data.frame( key = c("cat", "a", "has"),
synonym = c("kitten", "the", "with"),
stringsAsFactors=FALSE)
我想将数据作为数据框取回,就像原始“句子”一样,只是替换了同义词。例如:
ID text
1 a cat in a hat
2 a dog has a bone
3 this is a category
4 their cat has no hat
实际数据由 2016 个句子组成,每个句子在 200-500 个单词之间。查找表包含大约 200,000 行单词和短语。我已经想出了如何轻松地替换单个单词和短语,但我不知道如何使用查找表来完成。
另一个让我感到悲伤的注意事项:我需要匹配包括特殊字符在内的确切单词/短语。例如,“adison's disease”应该匹配“adison's disease”,而不是“adisons disease”。 "cotton-roll" 应该匹配 "cotton-roll" 但它不应该匹配 "cottonroll" 或 "cotton roll"。
我正在使用 R 版本 3.6.2 (2019-12-12) 平台:x86_64-w64-mingw32/x64(64位) 运行于:Windows 10 x64(内部版本 18362)
【问题讨论】: