错误 inserting/retrieving 推文进入 mongolite 数据库
Error inserting/retrieving tweets into mongolite db
我正在尝试对已经获取并存储在 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)
}
我想你想使用 sapply
,它会展平 searchTwitter
returns 的状态对象列表。无论如何这都有效。请注意,您需要安装并启动 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:
#
#
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
我正在尝试对已经获取并存储在 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)
}
我想你想使用 sapply
,它会展平 searchTwitter
returns 的状态对象列表。无论如何这都有效。请注意,您需要安装并启动 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:
#
#
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