我想知道是否有人有一个快速的解决方案来从R的推文中提取标签。例如,给定以下字符串,我如何解析它以提取带有hashtag的单词?
string <- 'Crowdsourcing is awesome. #stackoverflow'发布于 2012-07-19 06:25:18
与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)这会产生:
> print(hashtags)
[[1]]
[1] "#hashtag" "#stackoverflow" "#question" 请注意,如果string实际上是许多tweet的向量,则也可以不加修改地工作。它返回一个字符向量列表。
发布于 2012-07-19 06:21:01
像这样的东西?
string <- c('Crowdsourcing is awesome. #stackoverflow #answer',
"another #tag in this tweet")
step1 <- strsplit(string, "#")
step2 <- lapply(step1, tail, -1)
result <- lapply(step2, function(x){
sapply(strsplit(x, " "), head, 1)
})https://stackoverflow.com/questions/11551065
复制相似问题