خرید بک لینک

Vote count: 0

I have a program which counts the amount of times each word is mentioned in tweets and then stores them in a hash map. A fellow stackexchange member helped me store the top 5 words in a map.

Class where all work is being done:

package readtweets;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;

public class ReadTweets {

    //change this to true to enable the print lines
    static boolean DEBUG = false;

    //Creates map of words
    HashMap<String, Integer> wordMap = new HashMap();

    /**
     * @param args the command line arguments
     */
    public HashMap<String, Integer> readTweets(String twitterQuery, String[] args) throws TwitterException {//CHANGE MAIN TO READTWEETS OR SOMETHING
        // TODO code application logic here

        //Set up user authentication
        ConfigurationBuilder configBuilder = new ConfigurationBuilder();
        configBuilder.setOAuthConsumerKey("wwwwwwwwwwwwwwwwwwwwwwwww");
        configBuilder.setOAuthConsumerSecret("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        configBuilder.setOAuthAccessToken("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
        configBuilder.setOAuthAccessTokenSecret("zzzzzzzzzzzzzzzzzzzzzzzzzzzzz");

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

        //Build Query
        Query query = new Query(twitterQuery);

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

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

        //How many results do we want to retrieve
        int numResults = 400;

        //Filter out duplicate tweets and keep track of how many tweets we have downloaded
        HashMap<Long, Boolean> tweetIDMap = new HashMap();

        //Run query without max_id set
        QueryResult result = twitter.search(query);

        //Run query until we have the required number of tweets or until there are no more tweets
        while (result.getCount() > 0 && tweetIDMap.size() <= numResults) {
            //Minimum ID of the retued results. Used to get sequence of tweets
            long minID = -1;

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

            //Work through the results
            for (Status tweet : tweets) {
                String userName = tweet.getUser().getName();
                long userId = tweet.getUser().getId();
                Date creationDate = tweet.getCreatedAt();
                String tweetText = tweet.getText();

                //Print out the information
                if (DEBUG) {
                    System.out.println("Tweet created by " + userName + "(" + userId + ") on date " + creationDate);
                }
                if (DEBUG) {
                    System.out.println("Tweet text: " + tweetText + "n");
                }

                if (minID == -1 || tweet.getId() < minID) {
                    minID = tweet.getId();
                }

                tweetIDMap.put(tweet.getId(), Boolean.TRUE);

                //wordMap.put("cat", wordMap.get("cat").intValue()++);
                //String hey = new String("hey there world");
                for (String word : tweetText.toLowerCase().split("\s+")) {
                    word = word.replaceAll("^[^a-zA-Z]+", ""); //removes leading punctionation
                    word = word.replaceAll("[^a-zA-Z]+$", ""); //removes trailing punctionation
                    word = word.replaceAll("https?://\S+\s?", ""); // removes links
                    wordMap.put(word, wordMap.getOrDefault(word, 0) + 1);
                }
                wordMap.remove("");
                wordMap.remove("rt");
                wordMap.remove("the");
                wordMap.remove("to");
                wordMap.remove("a");
                wordMap.remove("of");
                wordMap.remove("and");
                wordMap.remove("i");
                wordMap.remove("in");
                wordMap.remove("have");
                wordMap.remove("are");
                wordMap.remove("for");
                wordMap.remove("is");
                wordMap.remove("the");
                wordMap.remove("you");
                wordMap.remove("my");
                wordMap.remove("not");
                wordMap.remove("that");
                wordMap.remove("me");
                wordMap.remove("be");
                wordMap.remove("with");
                wordMap.remove("like");
                wordMap.remove("as");
                wordMap.remove("https");
                wordMap.remove("they");
                wordMap.remove("who");
                wordMap.remove("on");
                wordMap.remove("but");
                wordMap.remove("this");
                wordMap.remove("by");
                wordMap.remove("so");
                wordMap.remove("seen");
                wordMap.remove("your");
                wordMap.remove("these");
                wordMap.remove("do");
                wordMap.remove("their");
                wordMap.remove("if");
                wordMap.remove("just");

                //debugging - finds the most common word in hashmap
                 Map.Entry<String, Integer> maxEntry = null;
                 for (Map.Entry<String, Integer> entry : wordMap.entrySet()) {
                 if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
                 maxEntry = entry;
                 }
                 }
                 System.out.println(maxEntry);


            }

            query.setMaxId(minID - 1);

            result = twitter.search(query);

            //Prints the hashmap of words
            if (DEBUG) {
                System.out.println(wordMap + "n");
            }
        }

        //Prints amount of tweets downloaded
        if (DEBUG) {
            System.out.println(tweetIDMap.size() + " unique tweets downloaded.");
        }

        retu wordMap;

    }

    }

JavaFX Class:

package readtweets;

import java.util.HashMap;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import twitter4j.*;

public class Gui extends Application {

    TextField tf = new TextField();

    //makes the hashmap accessible in this class
    ReadTweets tweets = new ReadTweets();
    HashMap<String, Integer> wordMap = tweets.getHashmap();

    @Override
    public void start(Stage primaryStage) {
        VBox pane = new VBox();
        pane.setSpacing(10);
        pane.setAlignment(Pos.CENTER);

        Button button = new Button("Ok");
        button.setOnAction(new okHandler());

        pane.getChildren().add(tf);
        pane.getChildren().add(button);

        Scene scene = new Scene(pane, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    class okHandler implements EventHandler<ActionEvent> {

        public void handle(ActionEvent e) {
            String text = tf.getText();

            String[] strArr = {"cats", "dogs"};
            try {
                tweets.readTweets(text, strArr);
                System.out.println(wordMap);
            } catch (TwitterException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

I can retrieve the wordMap hashmap, i tested this by printing the hashmap. However, when i try to print the top5 hashmap, it prints an empty hashmap.

How do i retrieve the top5 hashmap within the javafx class so that i can use the data to create a pie chart?

asked 30 secs ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 25 فروردين 1395 ساعت: 23:43

صفحه بندی