我正在尝试使用node,express,twit和sentiment构建一个基本的twitter情绪分析应用程序。它将允许用户输入搜索词,只要流打开,下面的代码将返回情绪分析。但是,当我输入搜索词,例如“音乐”时,我收到错误:
无法GET /监听?词组=音乐
我不确定代码出错的地方,但我认为没有正确分配 monitoringPhrase 或者没有存储查询。忽略'sentimentImage',我没有包含代码,因为它不相关。
app.get('/',
function (req, res) {
var welcomeResponse = "<HEAD>" +
"<title>Twitter Sentiment Analysis</title>\n" +
"</HEAD>\n" +
"<BODY>\n" +
"<P>\n" +
"Welcome to the Twitter Sentiment Analysis app.<br>\n" +
"What would you like to monitor?\n" +
"</P>\n" +
"<FORM action=\"/monitor\" method=\"get\">\n" +
"<P>\n" +
"<INPUT type=\"text\" name=\"phrase\"><br><br>\n" +
"<INPUT type=\"submit\" value=\"Go\">\n" +
"</P>\n" + "</FORM>\n" + "</BODY>";
if (!monitoringPhrase) {
res.send(welcomeResponse);
} else {
var monitoringResponse = "<HEAD>" +
"<META http-equiv=\"refresh\" content=\"5; URL=http://" +
req.headers.host +
"/\">\n" +
"<title>Twitter Sentiment Analysis</title>\n" +
"</HEAD>\n" +
"<BODY>\n" +
"<P>\n" +
"The Twittersphere is feeling<br>\n" +
"<IMG align=\"middle\" src=\"" + sentimentImage() + "\"/><br>\n" +
"about " + monitoringPhrase + ".<br><br>" +
"Analyzed " + tweetCount + " tweets...<br>" +
"</P>\n" +
"<A href=\"/reset\">Monitor another phrase</A>\n" +
"</BODY>";
res.send(monitoringResponse);
}
});
var tweetCount = 0;
var tweetTotalSentiment = 0;
var monitoringPhrase;
function resetMonitoring() {
monitoringPhrase = "";
}
function beginMonitoring(phrase) {
var stream;
// cleanup if we're re-setting the monitoring
if (monitoringPhrase) {
resetMonitoring();
}
monitoringPhrase = phrase;
tweetCount = 0;
tweetTotalSentiment = 0;
stream = tweeter.stream('statuses/filter', {
'track': monitoringPhrase
}, function (stream) {
console.log("Monitoring Twitter for " + monitoringPhrase);
stream.on('data', function (data) {
// only evaluate the sentiment of English-language tweets
if (data.lang === 'en') {
sentiment(data.text, function (err, result) {
tweetCount++;
tweetTotalSentiment += result.score;
});
}
});
});
return stream;
};
答案 0 :(得分:0)
您似乎没有定义app.get('/monitor', function(){})
路线。
另外,我无法在任何地方看到beginMonitoring
。
这是一个很长的镜头,但我想你想要更像这样的东西:
var welcomeResponse = "<HEAD>" +
"<title>Twitter Sentiment Analysis</title>\n" +
"</HEAD>\n" +
"<BODY>\n" +
"<P>\n" +
"Welcome to the Twitter Sentiment Analysis app.<br>\n" +
"What would you like to monitor?\n" +
"</P>\n" +
"<FORM action=\"/monitor\" method=\"get\">\n" +
"<P>\n" +
"<INPUT type=\"text\" name=\"phrase\"><br><br>\n" +
"<INPUT type=\"submit\" value=\"Go\">\n" +
"</P>\n" + "</FORM>\n" + "</BODY>";
var tweetCount = 0;
var tweetTotalSentiment = 0;
var monitoringPhrase = "";
app.get('/', function (req, res) {
res.send(welcomeResponse);
});
app.get('/monitor', function (req, res) {
if ('phrase' in req.query && req.query.phrase != "" && req.query.phrase != monitoringPhrase) {
// Picked new phrase to monitor
beginMonitoring(req.query.phrase);
}
if (monitoringPhrase) {
// Keep monitoring current phrase
var monitoringResponse = "<HEAD>" +
"<META http-equiv=\"refresh\" content=\"5; URL=http://" +
req.headers.host +
"/\">\n" +
"<title>Twitter Sentiment Analysis</title>\n" +
"</HEAD>\n" +
"<BODY>\n" +
"<P>\n" +
"The Twittersphere is feeling<br>\n" +
"<IMG align=\"middle\" src=\"" + sentimentImage() + "\"/><br>\n" +
"about " + monitoringPhrase + ".<br><br>" +
"Analyzed " + tweetCount + " tweets...<br>" +
"</P>\n" +
"<A href=\"/reset\">Monitor another phrase</A>\n" +
"</BODY>";
res.send(monitoringResponse);
} else {
// No phrase chosen, so just show the default page.
res.send(welcomeResponse)
}
});