Skip to content
Permalink
ce0c76d38d
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.36 KB
package kb.howtokb.reader;
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 kb.howtokb.taskframe.WikiHowTask;
import kb.howtokb.taskframe.WikiHowTaskFrame;
public class TaskFrameReader {
static JSONParser parser = new JSONParser();
// Read json file and return all activity frame object
// TO DO
// ======================================================================
/**
* Return list of task frame extract from file with json format
*
* @param directory
* that saves task frame in json format
* @return a list of task frame
* @throws IOException
* @throws ClassNotFoundException
* @throws ParseException
*/
public static ArrayList<WikiHowTaskFrame> extractWikiHowTaskFrameFromJSONFile(String directory)
throws IOException, ClassNotFoundException, ParseException {
ArrayList<WikiHowTaskFrame> allframe = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(directory))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
WikiHowTaskFrame newframe = jsonToWikiHowTaskFrame(sCurrentLine);
allframe.add(newframe);
}
}
return allframe;
}
//Read a string in json format and return an activity frame
public static WikiHowTaskFrame jsonToWikiHowTaskFrame(String jsonStr) throws ParseException {
return jsonToWikiHowTaskFrame((JSONObject) parser.parse(jsonStr));
}
// Read an Json object to transfer to activity frame
@SuppressWarnings("unchecked")
public static WikiHowTaskFrame jsonToWikiHowTaskFrame(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());
}
WikiHowTask newact = new WikiHowTask(id, verb, object, oriverb, oriobject, cateid, linkid, rate, view, image,
clusterid, clustername, video, children, parent, prev, next);
return (new WikiHowTaskFrame(idframe, newact, location, temporal, partA, partO));
}
}