【问题标题】:How to change the background color in processing, according to whether a tweet has a positive or negative sentiment?如何根据推文的正面情绪或负面情绪来改变处理中的背景颜色?
【发布时间】:2015-12-02 22:34:29
【问题描述】:

我是 Processing 的新手,我正在尝试创建一个交互式信息图,其中的背景颜色会根据有关事件的最新推文是否包含正面或负面的词而变化。对于正面推文,背景将为黄色,而对于负面推文,背景将为红色。

我的项目正在运行,以便它在控制台中显示提到“温布利”(事件)的最新推文。

我不知道如何在控制台数据中打印的文本中找到正面和负面的词。

为了尝试这样做,我设置了一个字符串数组来列出我想要触发背景颜色变化的所有正面和负面词:

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";
}

然后我在 void draw() 中添加一个 if 语句

  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

 }

【问题讨论】:

  • if(console.log = positiveWords) 应该是 if(console.log == positiveWords) 因为您正在进行比较而不是分配。但是你无论如何都可以像这样比较日志,所以它是静音点。但是 if (tweetsResults = positiveWords) 应该是 if (tweetsResults == positiveWords) 出于同样的原因。
  • 另外,你在哪里定义 tweetsResults?而positiveWords 是一个数组,你不能像那样做比较。

标签: javascript twitter processing processing.js temboo


【解决方案1】:

首先,不要尝试将文本存储在控制台中。控制台主要用于调试。

相反,将您的文本存储在一个变量中。实际上,您已经在 tweetText 变量中这样做了。

接下来,将ArrayLists 用于您的positiveWords 和negativeWords。这样可以更轻松地搜索它们。

然后使用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”之类的问题要容易得多。试着把你的问题分解成更小的步骤——你能创建一个单独的草图来简单地打印出 hardcoded 单词是好是坏吗?然后你可以对硬编码的句子做同样的事情吗?从小处着手,逐步构建,而不是一次完成整个项目。

【讨论】:

  • 非常感谢。它现在似乎可以工作了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-03
  • 2018-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多