0

Processing は初めてで、イベントに関する最新のツイートに肯定的な言葉が含まれているか否定的な言葉が含まれているかによって背景色が変化するインタラクティブなインフォグラフィックを作成しようとしています。肯定的なツイートの場合は背景が黄色になり、否定的なツイートの場合は背景が赤になります。

'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

 }
4

1 に答える 1

1

まず第一に、テキストをコンソールに保存しようとしないでください。コンソールは主にデバッグ用です。

代わりに、テキストを変数に格納します。実際には、tweetText変数ですでにそれを行っています。

次に、 と に使用しArrayListsます。これにより、それらを簡単に検索できます。positiveWordsnegativeWords

次に、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 を取得した」などの質問に答える方がはるかに簡単です。問題を小さなステップに分割してみてください。ハードコードされた単語が良いか悪いかを単純に出力する別のスケッチを作成できますか? ハードコーディングされた文に対して同じことができますか? 一度にプロジェクト全体を引き受けようとするのではなく、小さく始めて段階的に構築してください。

于 2015-12-03T13:47:15.917 に答える