我想知道是否有人有一个快速的解决方案来从推文中提取主题标签R.例如,给定以下字符串,如何解析它以使用#标签提取单词?
string <- 'Crowdsourcing is awesome. #stackoverflow'
Run Code Online (Sandbox Code Playgroud)
与HTML不同,我希望您可以使用正则表达式解析主题标签.
library(stringr)
string <- "#hashtag Crowd#sourcing is awesome. #stackoverflow #question"
# I don't use Twitter, so maybe this regex is not right
# for the set of allowable hashtag characters.
hashtag.regex <- perl("(?<=^|\\s)#\\S+")
hashtags <- str_extract_all(string, hashtag.regex)
Run Code Online (Sandbox Code Playgroud)
产量:
> print(hashtags)
[[1]]
[1] "#hashtag" "#stackoverflow" "#question"
Run Code Online (Sandbox Code Playgroud)
请注意,如果string实际上是许多推文的向量,这也可以不修改.它返回一个字符向量列表.