2016-04-12 19 views
0

Belirli bir sözcük içeren ve her bir tweet boyunca konuyla ilgili başka bir sözcüğün yinelemelerini saymak için tweet'leri kullanan bir programım var (örneğin bu durumda ana sözcük cameron ve vergi ve panama arıyor.) Çalışıyorum, bu yüzden bu belirli tweet için sayar ama tüm oluşumlar için birikimli bir sayımı nasıl elde edeceğimi göremiyorum. Sözcük oluştuğunda bir değişkeni arttırmakla oynamıştım ama işe yaramıyor. Kod aşağıda, açık nedenlerden dolayı twitter API anahtarlarımı çıkardım.Bir dize içinde alt dizgiler için toplam sayım java

public class TwitterWordCount { 

    public static void main(String[] args) { 
     ConfigurationBuilder configBuilder = new ConfigurationBuilder(); 
     configBuilder.setOAuthConsumerKey(XXXXXXXXXXXXXXXXXX); 
     configBuilder.setOAuthConsumerSecret(XXXXXXXXXXXXXXXXXX); 
     configBuilder.setOAuthAccessToken(XXXXXXXXXXXXXXXXXX); 
     configBuilder.setOAuthAccessTokenSecret(XXXXXXXXXXXXXXXXXX); 

     //create instance of twitter for searching etc. 
     TwitterFactory tf = new TwitterFactory(configBuilder.build()); 
     Twitter twitter = tf.getInstance(); 

     //build query 
     Query query = new Query("cameron"); 

     //number of results pulled each time 
     query.setCount(100); 

     //set the language of the tweets that we want 
     query.setLang("en"); 

     //Execute the query 
     QueryResult result; 
     try { 
      result = twitter.search(query); 

      //Get the results 
      List<Status> tweets = result.getTweets(); 

      //Print out the information 
      for (Status tweet : tweets) { 
       //get information about the tweet 
       String userName = tweet.getUser().getName(); 
       long userId = tweet.getUser().getId(); 
       Date creationDate = tweet.getCreatedAt(); 
       String tweetText = tweet.getText(); 

       //print out the information 
       System.out.println(); 
       System.out.println("Tweeted by " + userName + "(" + userId + ") on date " + creationDate); 
       System.out.println("Tweet: " + tweetText); 
       // System.out.println(); 
       String s = tweetText; 
       Pattern pattern = Pattern.compile("\\w+"); 
       Matcher matcher = pattern.matcher(s); 
       while (matcher.find()) { 
        System.out.print(matcher.group() + " "); 

       } 

       String str = s; 
       String findStr = "tax"; 
       int lastIndex = 0; 
       int count = 0; 
       //int countall = 0; 

       while (lastIndex != -1) { 
        lastIndex = str.indexOf(findStr, lastIndex); 

        if (lastIndex != -1) { 
         count++; 
         lastIndex += findStr.length(); 
         //countall++; 
        } 
       } 

       System.out.println(); 
       System.out.println(findStr + " = " + count); 

       String two = tweetText; 

       String str2 = two; 
       String findStr2 = "panama"; 
       int lastIndex2 = 0; 
       int count2 = 0; 

       while (lastIndex2 != -1) { 
        lastIndex2 = str2.indexOf(findStr2, lastIndex2); 

        if (lastIndex2 != -1) { 
         count++; 
         lastIndex2 += findStr.length(); 
        } 

        System.out.println(findStr2 + " = " + count2); 
       } 
      } 
     } 
     catch (TwitterException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

Bunun kesinlikle programların en temiz olmadığını da biliyorum, devam ediyor!

+0

Peki, karşılaştığınız sorun nedir? – Maljam

+0

@Maljam problemi, –

+0

çalışmak için toplam kelime toplamını toplayan bir sayaç alamıyorum, fakat int sayısı gösteriliyor? – Maljam

cevap

1

Sayma değişkenlerinizi döngü dışı dışında tanımlamanız gerekir.

int countKeyword1 = 0; 
int countKeyword2 = 0; 

for (Status tweet : tweets) { 

    //increase count variables in you while loops 

} 

System.out.Println("Keyword1 occurrences : " + countKeyword1); 
System.out.Println("Keyword2 occurrences : " + countKeyword2); 
System.out.Println("All occurrences : " + (countKeyword1 + countKeyword2)); 
İlgili konular