我是一个新的信息处理新手,我正在尝试创建一个交互式的信息图,其中的背景颜色会根据最近关于某个事件的推文是否包含积极或消极的单词而变化。对于积极的推文,背景将是黄色的,而对于负面的推文,背景将是红色的。
我已经让这个项目工作了,所以它在控制台中显示了提到‘wembley’(事件)的最新推文。
我被困在如何在控制台数据中打印的文本中找到积极和消极的单词。
为了尝试这样做,我设置了一个字符串数组来列出我想要触发背景颜色变化的所有正面和负面单词:
String [] positiveWords = new String[6];
{
positiveWords [0] = "great";
positiveWords [1] = "love";
positiveWords [2] = "amazing";
positiveWords [3] = "fun";
positiveWords [4] = "brilliant";
positiveWords [5] = "good";
}
String [] negativeWords = new String[4];
{
negativeWords [0] = "bad";
negativeWords [1] = "hate";
negativeWords [2] = "terrible";
negativeWords [3] = "boring";
}然后,我将if语句放在void draw()中。
if (console.log = positiveWords) {
background (0, 100, 100);
}
if (console.log = negativeWords) {
background (255, 0, 0);
}这将返回错误‘期望LPAREN,找到’控制台‘
几天来,我一直在到处寻找答案,但我不知所措!任何帮助都将非常非常感谢!非常感谢。
完整的源代码在这里:
import com.temboo.core.*;
import com.temboo.Library.Twitter.Search.*;
//string array to identify positive words
String [] positiveWords = new String[6];
{
positiveWords [0] = "great";
positiveWords [1] = "love";
positiveWords [2] = "amazing";
positiveWords [3] = "fun";
positiveWords [4] = "brilliant";
positiveWords [5] = "good";
}
//string array to identify negative words
String [] negativeWords = new String[4];
{
negativeWords [0] = "bad";
negativeWords [1] = "hate";
negativeWords [2] = "terrible";
negativeWords [3] = "boring";
}
// Create a session using your Temboo account application details
TembooSession session = new TembooSession("userName", "appName", "******");
// The name of your Temboo Twitter Profile
String twitterProfile = "Twittersearch1";
// Declare font and text strings
PFont fontTweet, fontInstructions;
String searchText, tweetText, instructionText;
// Create a JSON object to store the search results
JSONObject searchResults;
void setup() {
size(700, 350);
// Set a search term and instructions
searchText = "wembley";
instructionText = "Press any key to load a new tweet about '"+searchText+"'";
// Display initial tweet
runTweetsChoreo(); // Run the Tweets Choreo function
getTweetFromJSON(); // Parse the JSON response
displayText(); // Display the response
}
void draw() {
if (keyPressed) {
runTweetsChoreo(); // Run the Tweets Choreo function
getTweetFromJSON(); // Parse the JSON response
displayText(); // Display the response
}
//if statements to change the background color
if (tweetsResults = positiveWords) {
background (0, 100, 100);
}
if (tweetsResults = negativeWords) {
background (255, 0, 0);
}
}
void runTweetsChoreo() {
// Create the Choreo object using your Temboo session
Tweets tweetsChoreo = new Tweets(session);
// Set Profile
tweetsChoreo.setCredential(twitterProfile);
// Set inputs
tweetsChoreo.setQuery(searchText);
// Run the Choreo and store the results
TweetsResultSet tweetsResults = tweetsChoreo.run();
// Store results in a JSON object
searchResults = parseJSONObject(tweetsResults.getResponse());
}
void getTweetFromJSON() {
JSONArray statuses = searchResults.getJSONArray("statuses"); // Create a JSON array of the Twitter statuses in the object
JSONObject tweet = statuses.getJSONObject(0); // Grab the first tweet and put it in a JSON object
tweetText = tweet.getString("text"); // Pull the tweet text from tweet JSON object
}
void displayText() {
println(tweetText); // Print tweet to console
}发布于 2015-12-03 21:47:16
首先,不要试图将文本存储在控制台中。控制台主要用于调试。
相反,将文本存储在变量中。实际上,您已经在tweetText变量中这样做了。
接下来,对您的positiveWords和negativeWords使用ArrayLists。这将使搜索它们变得更容易。
然后使用split()函数将您的tweetText分解为单独的单词。检查这些单词是否都在您的某个ArrayLists中。
把它们放在一起,它可能看起来像这样:
void checkTweetText(){
boolean containsPositive = false;
boolean containsNegative = false;
String[] words = split(tweetText, " ");
for(String word : words){
if(positiveWords.contains(word)){
containsPositive = true;
}
if(negativeWords.contains(word)){
containsNegative = true;
}
}
if(containsPositive){
//do something
}
if(containsNegative){
//do something
}
}请注意,您可能不得不在拆分文本时涉及更多的逻辑--您必须考虑标点符号等问题。
另外,请注意,这是一个相当广泛的问题。很难回答一般的“我该怎么做”类型的问题。回答像这样的问题要容易得多,比如“我尝试了X,期望的是Y,但得到的是Z”。尝试将你的问题分解成更小的步骤--你能创建一个单独的草图,简单地打印出一个硬编码的单词是好是坏吗?那么你能对一个硬编码的句子做同样的事情吗?从小规模开始,逐步构建,而不是试图一次承担整个项目。
https://stackoverflow.com/questions/34054424
复制相似问题