【发布时间】:2017-02-14 16:11:25
【问题描述】:
问题:我正在使用标记器进行文本挖掘,并希望限制输入数据中字符串的长度。以下代码保留 如果包含单词,则为整个字符串。
#create data frame with data
dd <- data.frame(
text = c("hello how are you doing thank
you for helping me with this
problem","junk","junk"), stringsAsFactors = F)
#keep string that only include term "how"
dd <- filter(dd, grepl('how', text))
问题:如何修改代码,只保留关键字后的 N 个单词。
例如
如果 N =1 那么 dd 将包括:how are
如果 N =2 那么 dd 将包括:你好吗
如果 N =3 那么 dd 将包括:你好吗
...
如果我还在保留中包含其他单词,我需要可以工作的代码:
#keep string that only include terms "how" and "with"
dd <- filter(dd, grepl('how|with', text))
【问题讨论】:
-
不是真正的正则表达式大师,但这可能会对您有所帮助:regex101.com/r/95g7yT/1
-
如果您指定两个术语,例如
"how"和"with"和 N = 3,您希望看到什么结果?另外,如果两个词之间的距离小于 3 个词,比如我们选择了"hello"和"are",该怎么办? -
1 - 使用两个术语“如何”或“与”和 N=3 我想生成:“你如何解决这个问题”。第二个术语“with”的结果只会将单词保留到字符串的末尾。 2 - 如果术语是“你好”和“是”,如果可能的话,我想保留:“你好,你好吗,谢谢”。
标签: r regex text-mining