【问题标题】:Error inserting/retrieving tweets into mongolite db将推文插入/检索到 mongolite db 时出错
【发布时间】:2016-03-31 20:51:06
【问题描述】:

我正在尝试对已经获取并存储在 MongoDb 中的推文执行情感分析。获取数据框格式的推文后,我收到以下错误:

ip.txt=laply(ip.lst,function(t) t$getText())
Error in t$getText : $ operator is invalid for atomic vectors

整个代码如下:

iphone.tweets <- searchTwitter('#iphone', n=15, lang="en")
iphone.text=laply(iphone.tweets,function(t) t$getText())
df_ip <- as.data.frame(iphone.text)

m <- mongo("iphonecollection",db="project")
m$insert(df_ip)
df_ip<-m$find()
ip.lst<-as.list(t(df_ip))
ip.txt=laply(ip.lst,function(t) t$getText())

我想做的是按如下方式计算情绪分数:

iphone.scores <- score.sentiment(ip.txt, pos.words,neg.words, .progress='text')

score.sentiment 套路如下:

  score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
  require(plyr)
  require(stringr)
   # we got a vector of sentences. plyr will handle a list or a vector as an "l" for us
   # we want a simple array of scores back, so we use "l" + "a" + "ply" = laply:
  scores = laply(sentences, function(sentence, pos.words, neg.words) {
    # clean up sentences with R's regex-driven global substitute, gsub():
    sentence = gsub('[[:punct:]]', '', sentence)
    sentence = gsub('[[:cntrl:]]', '', sentence)
    sentence = gsub('\\d+', '', sentence)
    # and convert to lower case:
    sentence = tolower(sentence)
    # split into words. str_split is in the stringr package
    word.list = str_split(sentence, '\\s+')
    # sometimes a list() is one level of hierarchy too much
    words = unlist(word.list)
    # compare our words to the dictionaries of positive & negative terms
    pos.matches = match(words, pos.words)
    neg.matches = match(words, neg.words)
    # match() returns the position of the matched term or NA
    # we just want a TRUE/FALSE:
    pos.matches = !is.na(pos.matches)
    neg.matches = !is.na(neg.matches)
    # and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum():
    score = sum(pos.matches) - sum(neg.matches)
    return(score)
   }, pos.words, neg.words, .progress=.progress )
   scores.df = data.frame(score=scores, text=sentences)
   return(scores.df)
 } 

【问题讨论】:

  • 一些事情。你的score.sentiment 例程来自哪里? mongo db 的意义何在?为什么不能直接将ip.lst 放入score.sentiment 例程中?
  • 我打算将它们一次存储到 Mongodb 中,然后从那里获取和处理推文,而不是一直获取推文。

标签: r mongodb twitter sentiment-analysis mongolite


【解决方案1】:

我认为您想使用sapply,它会展平searchTwitter 返回的状态对象列表。在任何情况下,这都有效。请注意,您需要安装并启动 MongoDB 才能使其工作:

library(twitteR)
library(plyr)
library(stringr)
library(mongolite)

# you have to set up a Twitter Application at https://dev.twitter.com/ to get these 
#
ntoget <- 600 # get 600 tweets

iphone.tweets <- searchTwitter('#iphone', n=ntoget, lang="en")
iphone.text <- sapply(iphone.tweets,function(t) t$getText())
df_ip <- as.data.frame(iphone.text)

# MongoDB must be installed and the service started (mongod.exe in Windows)
#
m <- mongo("iphonecollection",db="project")
m$insert(df_ip)
df_ip_out<-m$find()

# Following routine (score.sentiment) was copied from:
# http://stackoverflow.com/questions/32395098/r-sentiment-analysis-with-phrases-in-dictionaries
#
score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
  require(plyr)  
  require(stringr)  
  # we got a vector of sentences. plyr will handle a list  
  # or a vector as an "l" for us  
  # we want a simple array ("a") of scores back, so we use  
  # "l" + "a" + "ply" = "laply":  
  scores = laply(sentences, function(sentence, pos.words, neg.words) {
    # clean up sentences with R's regex-driven global substitute, gsub():
    sentence = gsub('[[:punct:]]', '', sentence)
    sentence = gsub('[[:cntrl:]]', '', sentence)
    sentence = gsub('\\d+', '', sentence)    
    # and convert to lower case:    
    sentence = tolower(sentence)    
    # split into words. str_split is in the stringr package    
    word.list = str_split(sentence, '\\s+')    
    # sometimes a list() is one level of hierarchy too much    
    words = unlist(word.list)    
    # compare our words to the dictionaries of positive & negative terms
    pos.matches = match(words, pos)
    neg.matches = match(words, neg)   
    # match() returns the position of the matched term or NA    
    # we just want a TRUE/FALSE:    
    pos.matches = !is.na(pos.matches)   
    neg.matches = !is.na(neg.matches)   
    # and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum():
    score = sum(pos.matches) - sum(neg.matches)    
    return(score)    
  }, pos.words, neg.words, .progress=.progress )  
  scores.df = data.frame(score=scores, text=sentences)  
  return(scores.df)  
}

tweets <- as.character(df_ip_out$iphone.text)
neg = c("bad","prank","inferior","evil","poor","minor")
pos = c("good","great","superior","excellent","positive","super","better")
analysis <- score.sentiment(tweets,pos,neg)
table(analysis$score)

产生以下结果(4 分差,592 分中立,4 分好):

 -1   0   1 
  4 592   4 

【讨论】:

  • 谢谢。您能否告诉我您的代码中的以下行实际上做了什么: tweets
  • 它将df_ip_out$phone.text 向量从因子向量转换为字符向量。您可以使用class() 函数查看向量的类型。
  • 如果您认为是正确的,请将此标记为正确。
  • 为什么在 as.character(df_ip_out$iphone.text) 中使用 iphone.text?我的目标是处理仅从 mongoDB 获取的推文。 iphone.txt 是从 searchTwitter 函数返回的推文中获取的。我希望变量推文独立于最初获取的推文。它应该只依赖于 mongoDB 中的数据。
  • 我认为您混淆了 df_ip 数据帧,它是由 searchTwitter 检索的数据构建的,以及 df_ip_out 数据帧,它是从 m$find mongo 检索中检索的数据构建的功能。
猜你喜欢
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 2019-05-11
  • 2017-11-30
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多