从推文中提取主题标签

时间:2014-11-27 10:23:43

标签: r statistics analytics hashtag sentiment-analysis

我正在尝试进行情绪分析并面临一个小问题。我正在使用一个包含主题标签和其他垃圾值的字典(如下所示)。它还具有相关联的标签权重。我想只将标签及其相应的权重提取到一个新的数据框中。有没有简单的方法呢? 我已经尝试过使用regmatches,但有些是如何以列表格式提供输出并且搞乱了。 输入:

            V1    V2
1    #fabulous 7.526
2   #excellent 7.247
3      superb 7.199
4  #perfection 7.099
5    #terrific 6.922
6 #magnificent 6.672

输出:

            V1    V2
1    #fabulous 7.526
2   #excellent 7.247
3  #perfection 7.099
4    #terrific 6.922
5 #magnificent 6.672

2 个答案:

答案 0 :(得分:5)

要仅选择作为主题标签的条目,您可以使用简单正则表达式^#(意思是“以#开头的任何内容”):

> input[grepl("^#",input[,1]),]
            V1    V2
1    #fabulous 7.526
2   #excellent 7.247
4  #perfection 7.099
5    #terrific 6.922
6 #magnificent 6.672

除原始数据外,正则表达式#[[:alnum:]]+(意思是:“一个标签,后跟一个或多个字母数字字符”)可以帮助您获取主题标签:

> tweets <- c("New R job: Statistical and Methodological Consultant at the Center for Open Science http://www.r-users.com/jobs/statistical-methodological-consultant-center-open-science/ … #rstats #jobs","New R job: Research Engineer/Applied Researcher at eBay http://www.r-users.com/jobs/research-engineerapplied-researcher-ebay/ … #rstats #jobs")
> match <- regmatches(tweets,gregexpr("#[[:alnum:]]+",tweets))
> match
[[1]]
[1] "#rstats" "#jobs"  

[[2]]
[1] "#rstats" "#jobs"  
> unlist(match)
[1] "#rstats" "#jobs"   "#rstats" "#jobs"  

答案 1 :(得分:0)

此代码应该可以使用,并以data.frame

的形式提供所需的输出
 Input<- data.frame(V1 = c("#fabulous","#excellent","superb","#perfection","#terrific","#magnificent"), V2 = c("7.526",  "7.247" , "7.199", "7.099",  "6.922", "6.672")) 
 extractHashtags <- Input[which(substr(Input$V1,1,1) == "#"),]
 View(extractHashtags)