Revision: 16702
http://sourceforge.net/p/gate/code/16702
Author: adamfunk
Date: 2013-06-04 16:48:34 +0000 (Tue, 04 Jun 2013)
Log Message:
-----------
Handling Twitter query API output as well as streaming.
Modified Paths:
--------------
gate/trunk/plugins/Format_Twitter/src/gate/corpora/JSONTweetFormat.java
Modified:
gate/trunk/plugins/Format_Twitter/src/gate/corpora/JSONTweetFormat.java
===================================================================
--- gate/trunk/plugins/Format_Twitter/src/gate/corpora/JSONTweetFormat.java
2013-06-04 14:16:02 UTC (rev 16701)
+++ gate/trunk/plugins/Format_Twitter/src/gate/corpora/JSONTweetFormat.java
2013-06-04 16:48:34 UTC (rev 16702)
@@ -1,7 +1,7 @@
/*
* JSONTweetFormat.java
*
- * Copyright (c) 1995-2012, The University of Sheffield. See the file
+ * Copyright (c) 1995-2013, The University of Sheffield. See the file
* COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
*
* This file is part of GATE (see http://gate.ac.uk/), and is free
@@ -15,7 +15,12 @@
import java.io.*;
import java.util.*;
+
+import org.apache.commons.lang.StringUtils;
+
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+
import gate.*;
import gate.corpora.DocumentContentImpl;
import gate.corpora.MimeType;
@@ -28,6 +33,8 @@
// JSON API
// http://json-lib.sourceforge.net/apidocs/jdk15/index.html
+// Jackson API
+// http://wiki.fasterxml.com/JacksonHome
// Standard: RFC 4627
// https://tools.ietf.org/html/rfc4627
@@ -82,7 +89,7 @@
}
setNewLineProperty(doc);
- String jsonString = doc.getContent().toString();
+ String jsonString = StringUtils.trimToEmpty(doc.getContent().toString());
try {
// Parse the String
List<Tweet> tweets = readTweets(jsonString);
@@ -113,7 +120,29 @@
}
+
+ private List<Tweet> readTweetList(String string) throws IOException {
+ ObjectMapper mapper = new ObjectMapper();
+ List<Tweet> tweets = new ArrayList<Tweet>();
+ ArrayNode jarray = (ArrayNode) mapper.readTree(string);
+ for (JsonNode jnode : jarray) {
+ tweets.add(new Tweet(jnode));
+ }
+ return tweets;
+ }
+
+
private List<Tweet> readTweets(String string) throws IOException {
+ if (string.startsWith("[")) {
+ return readTweetList(string);
+ }
+
+ // implied else
+ return readTweetLines(string);
+ }
+
+
+ private List<Tweet>readTweetLines(String string) throws IOException {
ObjectMapper mapper = new ObjectMapper();
//JsonFactory jfactory = new JsonFactory();
//JsonParser parser = jfactory.createJsonParser(reader);
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. A cloud service to automate IT design, transition and operations
2. Dashboards that offer high-level views of enterprise services
3. A single system of record for all IT processes
http://p.sf.net/sfu/servicenow-d2d-j
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs