簡體   English   中英

字符串java中子字符串的總計數

[英]Overall count for substrings in a string java

我有一個程序,該程序從包含特定單詞的Twitter推文中獲取信息,並在每個推文中進行搜索,以計算與該主題相關的另一個單詞的出現次數(例如,在此情況下,主要單詞是cameron,它正在尋找稅收和巴拿馬。)我有它的工作,所以它計入該特定的鳴叫,但我似乎無法解決如何獲得所有出現的累計計數。 當出現單詞時,我一直在嘗試增加變量,但似乎沒有用。 下面的代碼,出於明顯的原因,我取出了我的twitter API密鑰。

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();
        }
    }
}

我也知道這絕對不是最干凈的程序,它正在開發中!

您必須在for循環之外定義計數變量。

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) );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM