Exemplo n.º 1
0
  public static Status getStatusTweet(String JSONString) {
    if (JSONString == null) return null;

    Status tweet = null;
    try {
      // parse json to object type
      tweet = DataObjectFactory.createStatus(JSONString);
    } catch (TwitterException e) {
      System.err.println("error parsing tweet object");
      return null;
    }
    return tweet;
  }
  public void testUnparsable() throws Exception {
    String str;
    str = "";
    try {
      DataObjectFactory.createStatus(str);
      fail("should fail");
    } catch (TwitterException expected) {
    } catch (Error notExpected) {
      fail("failed" + notExpected.getMessage());
    }
    try {
      DataObjectFactory.createStatus(str);
      fail("should fail");
    } catch (TwitterException expected) {
    } catch (Error notExpected) {
      fail("failed" + notExpected.getMessage());
    }
    str =
        "{\"in_reply_to_status_id_str\":null,\"place\":null,\"in_reply_to_user_id\":null,\"text\":\"working\",\"contributors\":null,\"retweet_count\":0,\"in_reply_to_user_id_str\":null,\"retweeted\":false,\"id_str\":\"794626207\",\"source\":\"\\u003Ca href=\\\"http:\\/\\/twitterhelp.blogspot.com\\/2008\\/05\\/twitter-via-mobile-web-mtwittercom.html\\\" rel=\\\"nofollow\\\"\\u003Emobile web\\u003C\\/a\\u003E\",\"truncated\":false,\"geo\":null,\"in_reply_to_status_id\":null,\"favorited\":false,\"user\":{\"show_all_inline_media\":false,\"geo_enabled\":false,\"profile_background_tile\":false,\"time_zone\":null,\"favourites_count\":0,\"description\":null,\"friends_count\":0,\"profile_link_color\":\"0084B4\",\"location\":null,\"profile_sidebar_border_color\":\"C0DEED\",\"id_str\":\"14481043\",\"url\":null,\"follow_request_sent\":false,\"statuses_count\":1,\"profile_use_background_image\":true,\"lang\":\"en\",\"profile_background_color\":\"C0DEED\",\"profile_image_url\":\"http:\\/\\/a3.twimg.com\\/a\\/1292975674\\/images\\/default_profile_3_normal.png\",\"profile_background_image_url\":\"http:\\/\\/a3.twimg.com\\/a\\/1292975674\\/images\\/themes\\/theme1\\/bg.png\",\"followers_count\":44,\"protected\":false,\"contributors_enabled\":false,\"notifications\":false,\"screen_name\":\"Yusuke\",\"name\":\"Yusuke\",\"is_translator\":false,\"listed_count\":1,\"following\":false,\"verified\":false,\"profile_text_color\":\"333333\",\"id\":14481043,\"utc_offset\":null,\"created_at\":\"Tue Apr 22 21:49:13 +0000 2008\",\"profile_sidebar_fill_color\":\"DDEEF6\"},\"id\":794626207,\"coordinates\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Tue Apr 2200 21:49:34 +0000 2008\"";

    try {
      DataObjectFactory.createCategory(str);
      fail("should fail");
    } catch (TwitterException expected) {
      expected.printStackTrace();
    } catch (Error notExpected) {
      fail("failed" + notExpected.getMessage());
    }
    try {
      DataObjectFactory.createCategory(str);
      fail("should fail");
    } catch (TwitterException expected) {
    } catch (Error notExpected) {
      fail("failed" + notExpected.getMessage());
    }
  }
Exemplo n.º 3
0
  public static void main(String argv[]) throws IOException {
    argv[0] = "-mis";
    argv[1] = "/Users/Indri/Eclipse_workspace/GeoNames/cities1000.txt";
    argv[2] = "/Users/Indri/Eclipse_workspace/GazIndex";
    argv[3] = "SampleInput/jsonTweets.txt";
    argv[4] = "-json";
    argv[5] = "SampleOutput/jsonTweets.out.csv";
    boolean misspell = argv[0].equals("-mis") ? true : false;
    String dicPath = argv[1]; // = "GeoNames/allCountries.txt";// gazetteer from geonames
    String indexPath = argv[2]; // index path
    String input = argv[3]; // = "tweet.csv";//to be determined.// test file path
    String type = argv[4]; // -json or -text
    String output = argv[5]; // = "output2.csv"; //output file path

    CollaborativeIndex ci =
        new CollaborativeIndex()
            .config("GazIndex/StringIndex", "GazIndex/InfoIndex", "mmap", "mmap")
            .open();

    EnglishParser enparser = new EnglishParser("res/", ci, false);
    ContextDisamb c = new ContextDisamb();
    LangDetector lang = new LangDetector("res/langdetect.profile");

    BufferedReader reader = GetReader.getUTF8FileReader(argv[3]);
    CsvWriter writer = new CsvWriter(output, ',', Charset.forName("utf-8")); // write

    writer.writeRecord(new String[] {"SPANISH TWEETS", "LOCATIONS"});

    String line = null;
    while ((line = reader.readLine()) != null) {
      line = line.trim();
      if (line.length() == 0) continue;
      Tweet t = new Tweet();
      String text = null;
      if (argv[4].equals("-text")) text = line;
      else
        try {
          text = (DataObjectFactory.createStatus(line.trim()).getText());
        } catch (TwitterException e) {
          // TODO Auto-generated catch block
          System.err.println("JSON format corrupted, or no content.");
          continue;
        }
      t.setText(text);
      List<String> match = enparser.parse(t);
      // Generate Matches
      if (match == null || match.size() == 0) {
        /** write blank result and the line itself if no match found. */
        writer.writeRecord(new String[] {text, ""});
        continue;
      }
      HashSet<String> reducedmatch = new HashSet<String>();
      for (String s : match) reducedmatch.add(s.substring(3, s.length() - 3));

      // Disambiguate topo
      HashMap<String, String[]> result = c.returnBestTopo(ci, reducedmatch);

      if (result == null) {
        System.out.println("No GPS for any location is found.");
      } else {
        System.out.println("The grounded location(s) are:");
        String topoStr = "";
        for (String topo : result.keySet())
          topoStr +=
              "["
                  + (topo
                      + ": "
                      + result.get(topo)[2]
                      + " "
                      + result.get(topo)[0]
                      + " "
                      + result.get(topo)[1])
                  + "] ";
        writer.writeRecord(new String[] {text, topoStr});
      }
    }
    reader.close();
    writer.close();
  }
Exemplo n.º 4
0
  public JSONTweet readLine() {
    String line;
    try {
      line = br.readLine();
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      return null;
    }
    if (line == null) return null;

    Status tweet = null;
    try {
      // parse json to object type
      tweet = DataObjectFactory.createStatus(line);
    } catch (TwitterException e) {
      System.err.println("error parsing tweet object");
      return null;
    }

    jsontweet.JSON = line;
    jsontweet.id = tweet.getId();
    jsontweet.source = tweet.getSource();
    jsontweet.text = tweet.getText();
    jsontweet.createdat = tweet.getCreatedAt();
    jsontweet.tweetgeolocation = tweet.getGeoLocation();

    User user;
    if ((user = tweet.getUser()) != null) {

      jsontweet.userdescription = user.getDescription();
      jsontweet.userid = user.getId();
      jsontweet.userlanguage = user.getLang();
      jsontweet.userlocation = user.getLocation();
      jsontweet.username = user.getName();
      jsontweet.usertimezone = user.getTimeZone();
      jsontweet.usergeoenabled = user.isGeoEnabled();

      if (user.getURL() != null) {
        String url = user.getURL().toString();

        jsontweet.userurl = url;

        String addr = url.substring(7).split("/")[0];
        String[] countrysuffix = addr.split("[.]");
        String suffix = countrysuffix[countrysuffix.length - 1];

        jsontweet.userurlsuffix = suffix;

        try {
          InetAddress address = null; // InetAddress.getByName(user.getURL().getHost());

          String generate_URL
              // =
              // "http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress="
              = "http://www.geoplugin.net/php.gp?ip=" + address.getHostAddress();
          URL data = new URL(generate_URL);
          URLConnection yc = data.openConnection();
          BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
          String inputLine;
          String temp = "";
          while ((inputLine = in.readLine()) != null) {
            temp += inputLine + "\n";
          }
          temp = temp.split("s:2:\"")[1].split("\"")[0];

          jsontweet.userurllocation = temp;

        } catch (Exception uhe) {
          // uhe.printStackTrace();
          jsontweet.userurllocation = null;
        }
      }
    }

    return jsontweet;
  }