阅读Json文件并拆分推文消息并进行分析

时间:2014-11-27 11:38:32

标签: java split readfile tweets sentiment-analysis

public class Tweets {

    private String Name;
    private String Username;
    private String Time;
    private long Followers;
    private long Following;
    private int Location;
    private String Tweet; 
    private double longitude; 
    private double latitude; 


    public Tweets(String name, String username, String time,
            long followers, long following, int location, String tweet,
            double longitude, double latitude) {

        Name = name;
        Username = username;
        Time = time;
        Followers = followers;
        Following = following;
        Location = location;
        Tweet = tweet;
        this.longitude = longitude;
        this.latitude = latitude;
    }
    public double getSentimentValue() {
        return sentimentValue;
    }
    public void setSentimentValue(double sentimentValue) {
        this.sentimentValue = sentimentValue;
    }
    public boolean isHaveisettheentimentvaluecalculateornot() {
        return haveisettheentimentvaluecalculateornot;
    }
    public void setHaveisettheentimentvaluecalculateornot(
            boolean haveisettheentimentvaluecalculateornot) {
        this.haveisettheentimentvaluecalculateornot = haveisettheentimentvaluecalculateornot;
    }
    public String getName() {
        return Name;
    }
    public String getUsername() {
        return Username;
    }
    public String getTime() {
        return Time;
    }
    public long getFollowers() {
        return Followers;
    }
    public long getFollowing() {
        return Following;
    }
    public int getLocation() {
        return Location;
    }
    public String getTweet() {
        return Tweet;
    }
    public double getLongitude() {
        return longitude;
    }
    public double getLatitude() {
        return latitude;
    }


    private double sentimentValue;
    private boolean haveisettheentimentvaluecalculateornot;





    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("***** Employee Details *****\n");
        sb.append("Name="+getName()+"\n");
        sb.append("UserName="+getUsername()+"\n");
        sb.append("Time="+getTime()+"\n");
        sb.append("Followers="+getFollowers()+"\n");
        sb.append("Following="+getFollowing()+"\n");
        sb.append("Location="+getLocation());
        sb.append("\n*****************************");

        return sb.toString();
}

在json文件下面,我附上了用于打开文件的代码,它会打开文件,但我仍然坚持将推文分成单个单词。

private static final String filePath = "H:\\My Documents\\OO2\\Ass\\Tweets.Json";

public static void main(String[] args) {

        try {
            // read the json file
            FileReader reader = new FileReader(filePath);

            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

            // get a String from the JSON object
            String Username = (String) jsonObject.get("Username");
            System.out.println("The first name is: " + Username);

            // get a number from the JSON object
            long Name =  (long) jsonObject.get("Name");
            System.out.println("The accouunt name is: " + Name);

            // get an array from the JSON object
            JSONArray Tweet= (JSONArray) jsonObject.get("Tweet");

            // take the elements of the json array
            for(int i=0; i<Tweet.size(); i++){
                System.out.println("The " + i + " element of the array: "+Tweet.get(i));
            }
            Iterator i = Tweet.iterator();

            // take each value from the json array separately
            while (i.hasNext()) {
                JSONObject innerObj = (JSONObject) i.next();
                System.out.println("Tweet "+ innerObj.get("Tweet") + 
                        " with level " + innerObj.get("knowledge"));
            }
            // handle a structure into the json object
            JSONObject structure = (JSONObject) jsonObject.get("job");
            System.out.println("Into job structure, name: " + structure.get("name"));

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ParseException ex) {
            ex.printStackTrace();
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }

    }
}

我正在尝试阅读这个Json文件并将推文消息拆分为单个单词并分析带有情绪的推文。

{
    "User":"BarackObama",
    "UserName":"Barack Obama",
    "Tweet":"Happening now: President Obama is holding a press conference. Tune in. ofa.bo/s1QX",
    "Time":"Wed Nov 05 19:58:06 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  },
  {
    "User":"BarackObama",
    "User Name":"Barack Obama",
    "Tweet":"\"I'm going to squeeze every last bit of opportunity to make the world a better place over these last two years.\" ?President Obama",
    "Time":"Wed Nov 05 21:33:27 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  },
  {
    "User":"BarackObama",
    "User Name":"Barack Obama",
    "Tweet":"This is undeniable progress on the minimum wage. Congrats to the @OFA volunteers who are fighting to #RaiseTheWage. pic.twitter.com/m4cLUzYZQO",
    "Time":"Thu Nov 06 18:14:22 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  },
  {
    "User":"BarackObama",
    "User Name":"Barack Obama",
    "Tweet":"Jackie Robinson West All Stars: America's champions. pic.twitter.com/JAmZBdgNuB",
    "Time":"Thu Nov 06 23:19:11 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  },
  {
    "User":"BarackObama",
    "User Name":"Barack Obama",
    "Tweet":"The unemployment rate is falling as fast as any point in the last 30 years. Let's keep building that momentum. pic.twitter.com/IiTCcVJM7t",
    "Time":"Fri Nov 07 17:17:17 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  },
  {
    "User":"BarackObama",
    "User Name":"Barack Obama",
    "Tweet":"For the first time since the 1990s, the private sector has added at least 200K jobs for nine consecutive months. #ActOnJobs",
    "Time":"Fri Nov 07 19:28:31 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  }

0 个答案:

没有答案