Permalink
Cannot retrieve contributors at this time
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?
HowToKB/src/kb/howtokb/taskframe/WikiHowTaskFrame.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
221 lines (191 sloc)
6.02 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kb.howtokb.taskframe; | |
import java.io.IOException; | |
import java.sql.SQLException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
import kb.howtokb.tools.InformationExtraction; | |
public class WikiHowTaskFrame implements BasicDataPt{ | |
private int id; | |
private WikiHowTask activity; | |
private List<String> locations; | |
private List<String> temporal; | |
private List<String> participatingAgent; | |
private List<String> participatingObject; | |
public WikiHowTaskFrame(int id, WikiHowTask act, ArrayList<String> loc, | |
ArrayList<String> tem, ArrayList<String> partA, ArrayList<String> partO){ | |
this.id = id; | |
activity = new WikiHowTask(act); | |
locations = loc; | |
temporal = tem; | |
setParticipatingObject(partO); | |
setParticipatingAgent(partA); | |
} | |
public WikiHowTaskFrame(WikiHowTask act, ArrayList<String> loc, | |
ArrayList<String> tem, ArrayList<String> partA, ArrayList<String> partO){ | |
activity = act; | |
locations = loc; | |
temporal = tem; | |
setParticipatingObject(partO); | |
setParticipatingAgent(partA); | |
} | |
public WikiHowTaskFrame(int id, WikiHowTask act){ | |
this.id = id; | |
activity = new WikiHowTask(act); | |
locations = new ArrayList<>(); | |
temporal = new ArrayList<>(); | |
participatingAgent = new ArrayList<>(); | |
participatingObject = new ArrayList<>(); | |
} | |
public WikiHowTask getActivity(){ | |
return activity; | |
} | |
public void setActivity(WikiHowTask act){ | |
activity = new WikiHowTask(act); | |
} | |
public List<String> getLocations(){ | |
return locations; | |
} | |
public void setLocations(ArrayList<String> loc){ | |
locations = loc; | |
} | |
public List<String> getTemporal(){ | |
return temporal; | |
} | |
public void setTemporal(ArrayList<String> tem){ | |
temporal = tem; | |
} | |
@Override | |
public int getID() { | |
return this.id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public List<String> getParticipatingAgent() { | |
return participatingAgent; | |
} | |
public void setParticipatingAgent(ArrayList<String> participatingAgent) { | |
this.participatingAgent = participatingAgent; | |
} | |
public List<String> getParticipatingObject() { | |
return participatingObject; | |
} | |
public void setParticipatingObject(ArrayList<String> participatingObject) { | |
this.participatingObject = participatingObject; | |
} | |
//List of location to JSON array | |
@SuppressWarnings("unchecked") | |
public JSONArray locationtoJsonArray(){ | |
JSONArray loc = new JSONArray(); | |
if (getLocations().size() > 0){ | |
for (int k=0; k<getLocations().size(); k++){ | |
loc.add(getLocations().get(k)); | |
} | |
} | |
return loc; | |
} | |
//List of temporal to JSON array | |
@SuppressWarnings("unchecked") | |
public JSONArray temporaltoJsonArray(){ | |
JSONArray temp = new JSONArray(); | |
if (getTemporal().size() > 0){ | |
for (int k=0; k<getTemporal().size(); k++){ | |
temp.add(getTemporal().get(k)); | |
} | |
} | |
return temp; | |
} | |
//List of participating agent to JSON array | |
@SuppressWarnings("unchecked") | |
public JSONArray partAtoJsonArray(){ | |
JSONArray partA = new JSONArray(); | |
if (getParticipatingAgent().size() > 0){ | |
for (int k=0; k<getParticipatingAgent().size(); k++){ | |
partA.add(getParticipatingAgent().get(k)); | |
} | |
} | |
return partA; | |
} | |
//List of participating object to JSON array | |
@SuppressWarnings("unchecked") | |
public JSONArray partOtoJsonArray(){ | |
JSONArray partO = new JSONArray(); | |
if (getParticipatingObject().size() > 0){ | |
for (int k=0; k<getParticipatingObject().size(); k++){ | |
partO.add(getParticipatingObject().get(k)); | |
} | |
} | |
return partO; | |
} | |
//Activity to JSON format | |
@SuppressWarnings("unchecked") | |
public JSONObject toJsonObject(){ | |
JSONObject obj = new JSONObject(); | |
obj.put("id", getID()); | |
obj.put("activity", getActivity().toJsonObject()); | |
obj.put("location", locationtoJsonArray()); | |
obj.put("temporal", temporaltoJsonArray()); | |
obj.put("part-agent", partAtoJsonArray()); | |
obj.put("part-object", partOtoJsonArray()); | |
return obj; | |
} | |
public String toString(){ | |
String f = "" + getID() + "\t"; | |
f += getActivity().getVerb() + " " + getActivity().getObject(); | |
f += "\t" + getActivity().getOriVerb() + " " + getActivity().getOriObject(); | |
try { | |
f += "\t" + InformationExtraction.getCategory(Integer.parseInt(getActivity().getCategoryID())); | |
} catch (NumberFormatException | IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
return f; | |
} | |
return f; | |
} | |
public String toPhrase() throws SQLException, IOException{ | |
String f = ""; | |
f += getActivity().getVerb().replaceAll(" ", "_") + "_"; | |
f += getActivity().getObject().replaceAll(" ", "_") + ""; | |
// f += getActivity().getOriVerb().replaceAll(" ", "_") + "_"; | |
// f += getActivity().getOriObject().replaceAll(" ", "_") + "_"; | |
// | |
// String loc = NormalizationText.listToPhrase(NormalizationText.normList(getLocations())); | |
// if (loc != "") | |
// f += loc + "_"; | |
// String temp = NormalizationText.listToPhrase(NormalizationText.normList(getTemporal())); | |
// if (temp != "") | |
// f += temp + "_"; | |
// String parto = NormalizationText.listToPhrase(NormalizationText.normList(getParticipatingObject())); | |
// if (parto != "") | |
// f += parto + "_"; | |
// String parta = NormalizationText.listToPhrase(NormalizationText.normList(getParticipatingAgent())); | |
// if (parta != "") | |
// f += parta; | |
return f; | |
} | |
public WikiHowTaskFrame setNormalized(){ | |
this.setActivity(this.getActivity().setNormalized()); | |
this.setLocations(normString(this.getLocations())); | |
this.setTemporal(normString(this.getTemporal())); | |
this.setParticipatingAgent(normString(this.getParticipatingAgent())); | |
this.setParticipatingObject(normString(this.getParticipatingObject())); | |
return this; | |
} | |
public static String normString(String s){ | |
return s.replaceAll("\"", "").replaceAll("}", "") | |
.replaceAll("\\{", "").replaceAll("\\[", "").replaceAll("]", "") | |
.replaceAll(":", "").replaceAll(",", " ") | |
.replaceAll("\\s+", " ").trim().toLowerCase(); | |
} | |
public static ArrayList<String> normString(List<String> list){ | |
ArrayList<String> result = new ArrayList<>(); | |
if (list.size() > 0){ | |
for (String l: list){ | |
result.add(normString(l)); | |
} | |
} | |
return result; | |
} | |
} |