Skip to content
Permalink
3b99e145f5
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
140 lines (122 sloc) 4.31 KB
package activity.extraction;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import activity.Activity;
import activity.ActivityFrame;
public class JsonToActivityFrame {
static JSONParser parser = new JSONParser();
// Read json file and return all activity frame object
// TO DO
// ======================================================================
/**
* Return list of activity frame extract from file with json format
*
* @param directory
* that saves activity frame in json format
* @return a list of activity frame
* @throws IOException
* @throws ClassNotFoundException
* @throws ParseException
*/
public ArrayList<ActivityFrame> extractActivityFrameFromJSONFile(String directory)
throws IOException, ClassNotFoundException, ParseException {
ArrayList<ActivityFrame> allframe = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(directory))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
ActivityFrame newframe = jsonToActivityFrame(sCurrentLine);
allframe.add(newframe);
}
}
return allframe;
}
//Read a string in json format and return an activity frame
public static ActivityFrame jsonToActivityFrame(String jsonStr) throws ParseException {
return jsonToActivityFrame((JSONObject) parser.parse(jsonStr));
}
// Read an Json object to transfer to activity frame
@SuppressWarnings("unchecked")
public static ActivityFrame jsonToActivityFrame(JSONObject obj) {
// Extract id
int idframe = (int) (long) obj.get("id");
// Extract list of location
ArrayList<String> location = new ArrayList<>();
JSONArray locationJ = (JSONArray) obj.get("location");
Iterator<String> iteratorC = locationJ.iterator();
while (iteratorC.hasNext()) {
location.add(iteratorC.next());
}
// Extract list of temporal
ArrayList<String> temporal = new ArrayList<>();
JSONArray temporalJ = (JSONArray) obj.get("temporal");
Iterator<String> iteratorT = temporalJ.iterator();
while (iteratorT.hasNext()) {
temporal.add(iteratorT.next());
}
// Extract list of part A
ArrayList<String> partA = new ArrayList<>();
JSONArray partAJ = (JSONArray) obj.get("part-agent");
Iterator<String> iteratorA = partAJ.iterator();
while (iteratorA.hasNext()) {
partA.add(iteratorA.next());
}
// Extract list of part O
ArrayList<String> partO = new ArrayList<>();
JSONArray partOJ = (JSONArray) obj.get("part-object");
Iterator<String> iteratorO = partOJ.iterator();
while (iteratorO.hasNext()) {
partO.add(iteratorO.next());
}
// Activity extraction
JSONObject actJ = (JSONObject) obj.get("activity");
// id
int id = (int) (long) actJ.get("id");
// verb
String verb = (String) actJ.get("verb");
// object
String object = (String) actJ.get("object");
// ori verb
String oriverb = (String) actJ.get("ori-verb");
// ori-object
String oriobject = (String) actJ.get("ori-object");
// image
String image = (String) actJ.get("image");
// cate id
String cateid = (String) actJ.get("categoryid");
// link id
String linkid = (String) actJ.get("linkid");
// view
int view = (int) (long) actJ.get("view");
// rate
double rate = (double) (double) actJ.get("rating");
// cluster id
int clusterid = (int) (long) actJ.get("clusterid");
// cluster name
String clustername = (String) actJ.get("clustername");
// video
String video = (String) actJ.get("video");
// parent
String parent = (String) actJ.get("parent");
// prev
String prev = (String) actJ.get("prev");
// next
String next = (String) actJ.get("next");
// list of sub-activity
ArrayList<Integer> children = new ArrayList<>();
JSONArray childrenJ = (JSONArray) actJ.get("sub-activity");
Iterator<Long> iteratorS = childrenJ.iterator();
while (iteratorS.hasNext()) {
children.add((int) (long) iteratorS.next());
}
Activity newact = new Activity(id, verb, object, oriverb, oriobject, cateid, linkid, rate, view, image,
clusterid, clustername, video, children, parent, prev, next);
return (new ActivityFrame(idframe, newact, location, temporal, partA, partO));
}
}