Example usage for twitter4j JSONObject JSONObject

List of usage examples for twitter4j JSONObject JSONObject

Introduction

In this page you can find the example usage for twitter4j JSONObject JSONObject.

Prototype

public JSONObject(String json) throws JSONException 

Source Link

Document

Creates a new JSONObject with name/value mappings from the JSON string.

Usage

From source file:twitterapp.TweetsSimilarity.java

/**
 * @param args the command line arguments
 *//* w  w w.  j ava2s.com*/
public static void main(String[] args) throws JSONException, Exception {
    System.out.println(intersection("unpresidented", "unpresidented", "unpresidented"));
    System.out.println(union("unpresidented", "unpresidented", "unpresidented"));
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("myTweetdb");

    Document temp;
    JSONObject b, c;
    Double sim[] = new Double[100];
    // String one = a.getString("hashtag");
    String two = "";
    int j = 0;
    int k = 0;
    int y = 0;
    String[] collections = { "hashtagAll", "mentionedAll", "urlAll", "retweetedAll" };
    String[] entities = { "hashtag", "mentioned_users", "url", "retweeted_tweet" };
    Double[][] hashtag = new Double[1000][1000];
    for (j = 0; j < hashtag.length; j++) {
        Arrays.fill(hashtag[j], 0.0);
    }
    j = 0;
    for (int p = 0; p < collections.length; p++) {

        String file = collections[p].concat(".csv");
        System.out.println(file);
        PrintWriter writer = new PrintWriter(file);
        writer.println("Source,Target,Weight,Type");
        MongoCursor<Document> manasou;
        MongoCollection collection2 = database.getCollection(collections[p]);
        manasou = collection2.find().iterator();
        HashMap<String, Integer> count = new HashMap<>();
        String common = "";

        while (manasou.hasNext() && y < 1000) {
            b = new JSONObject(manasou.next());
            String temp1 = b.getString(entities[p]);
            String tokens[] = temp1.split(" ");
            for (int i = 0; i < tokens.length; i++) {
                if (count.containsKey(tokens[i])) {
                    Integer temp2 = count.get(tokens[i]);
                    temp2++;
                    count.replace(tokens[i], temp2);
                } else {
                    count.put(tokens[i], 1);
                }
            }
            y++;
        }
        Iterator<Entry<String, Integer>> count_it = count.entrySet().iterator();
        while (count_it.hasNext()) {
            Entry entry = count_it.next();
            if ((Integer) entry.getValue() > 500) {
                common = common.concat(entry.getKey() + " ");
            }
        }
        System.out.println(common);
        manasou = collection2.find().iterator();
        j = 0;
        while (manasou.hasNext() && j < 1000) {
            b = new JSONObject(manasou.next());
            System.out.println(j);
            MongoCursor<Document> kati2 = collection2.find().iterator();
            k = 0;
            while (kati2.hasNext() && k < 1000) {
                c = new JSONObject(kati2.next());
                if (j < k) {
                    String temp1 = b.getString(entities[p]);
                    String temp2 = c.getString(entities[p]);
                    double temp3 = intersection(temp1, temp2, common) / union(temp1, temp2, common);
                    if (Double.isNaN(temp3)) {
                        temp3 = 1;
                    }
                    String lel = "tweet" + j + "," + "tweet" + k + "," + temp3 + ",Undirected";

                    hashtag[j][k] = hashtag[j][k] + temp3;

                    writer.flush();
                    if (temp3 > 0.2) {
                        writer.println(lel);
                    }

                } else {
                    k++;
                    continue;
                }
                k++;
            }

            j++;

        }

    }
    PrintWriter writer = new PrintWriter("all.csv");
    writer.println("Source,Target,Weight,Type");
    for (j = 0; j < hashtag.length; j++) {
        for (k = 0; k < hashtag.length; k++) {
            if (j < k) {
                double temp1 = hashtag[j][k] / 4.0;
                if (temp1 > 0.2) {
                    String lel = "tweet" + j + "," + "tweet" + k + "," + temp1 + ",Undirected";
                    writer.flush();
                    writer.println(lel);
                }
            }
        }
    }

}