我正在尝试使用node、express、twit和情绪构建一个基本的twitter情绪分析应用程序。它将允许用户输入搜索词,只要流是打开的,下面的代码就会返回情绪分析。然而,当我输入搜索词时,例如"音乐",我会得到错误:
无法获取/监视?短语=音乐我不确定代码哪里出了问题,但我认为monitoringPhrase没有正确分配,或者没有存储查询。忽略"senmentImage",我没有包含代码,因为它不相关。
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;
};
您似乎没有定义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)
}
});