00001
00002 import java.io.IOException;
00003 import java.io.FileReader;
00004 import java.io.BufferedReader;
00005 import java.util.ArrayList;
00006 import java.util.HashMap;
00007 import java.io.*;
00008 import java.util.regex.Pattern;
00009 import java.util.regex.Matcher;
00010
00011
00012 public class BlogbdToLDCRF {
00013
00014
00015 public static void main(String[] args) throws IOException {
00016
00017
00018
00019
00020
00021 HashMap<String, HashMap<String, String>> data = new HashMap<String, HashMap<String,String>>();
00022
00023
00024 HashMap<String, ArrayList<String[]>> constraints = new HashMap<String, ArrayList<String[]>>();
00025
00026
00027
00028 BufferedReader input = new BufferedReader(new FileReader("/home/tenorth/work/actionrec/srldb/models/set-the-table/stt-human/new-1.blogdb"));
00029
00030 String line="";
00031
00032
00033
00034 while (( line = input.readLine()) != null){
00035
00036
00037 if((line.trim().startsWith("//")) || (line.trim().equals(""))) continue;
00038
00039 else if(line.trim().startsWith("precedes")) {
00040
00041
00042 Matcher matcher = Pattern.compile(".+\\(([a-zA-Z0-9_]+)[ \t\n\f\r]*,[ \t\n\f\r]*([a-zA-Z0-9_]+)[ \t\n\f\r]*,[ \t\n\f\r]*([a-zA-Z0-9_]+)\\)=([a-zA-Z0-9_]+);").matcher(line.trim());
00043
00044 matcher.find();
00045 String action1=matcher.group(1);
00046 String action2=matcher.group(2);
00047 String activity=matcher.group(3);
00048
00049 if(!constraints.containsKey(activity)) {
00050 constraints.put(activity, new ArrayList<String[]>());
00051 }
00052 constraints.get(activity).add(new String[]{action1, action2});
00053
00054 } else {
00055
00056
00057 Matcher matcher = Pattern.compile("([a-zA-Z0-9_]+)\\(([a-zA-Z0-9_]+)\\)=([a-zA-Z0-9_]+);").matcher(line.trim());
00058
00059 matcher.find();
00060 String pred=matcher.group(1);
00061 String seqID=matcher.group(2);
00062 String val=matcher.group(3);
00063
00064 if(!data.containsKey(seqID)) {
00065 data.put(seqID, new HashMap<String, String>());
00066 }
00067 data.get(seqID).put(pred, val);
00068
00069 }
00070
00071 }
00072
00073
00074
00075 ArrayList<String> out = new ArrayList<String>();
00076 for(String act : constraints.keySet()) {
00077
00078
00079
00080 out.clear();
00081 out.add(constraints.get(act).get(0)[0]);
00082 for(String[] pair : constraints.get(act)) {
00083 out.add(pair[1]);
00084 }
00085
00086
00087 for(String seq : data.keySet()) {
00088 if(!out.contains(seq))
00089 out.add(seq);
00090 }
00091
00092
00093
00094 boolean swapped=true; int i=0;
00095 while(swapped) {
00096
00097
00098 swapped=false;
00099 for(String[] c : constraints.get(act)) {
00100 if(!constraintMet(c, out)) {
00101 out.set(out.indexOf(c[0]), c[1]);
00102 out.set(out.lastIndexOf(c[1]), c[0]);
00103 swapped=true;
00104 }
00105 }
00106 i++;
00107 if(i==10000) {
00108 return;
00109 }
00110 }
00111
00112
00113 PrintWriter outfile = new PrintWriter(new FileWriter(act+".csv"));
00114
00115
00116
00117
00118 outfile.print(data.get(out.get(0)).keySet().size()+","+out.size()+"\n");
00119
00120
00121 for(String p : data.get(out.get(0)).keySet()) {
00122
00123
00124 for(String segm : out) {
00125 if(data.get(segm)!=null && data.get(segm).get(p)!=null)
00126 outfile.print(data.get(segm).get(p)+",");
00127 else
00128 outfile.print(0+",");
00129 }
00130 outfile.print("\n");
00131 }
00132 outfile.close();
00133
00134
00135 outfile = new PrintWriter(new FileWriter(act+".labels.csv"));
00136 outfile.print("1,"+out.size()+"\n");
00137 for(String segm : out) {
00138 outfile.print(segm+",");
00139 }
00140 outfile.print("\n");
00141 outfile.close();
00142
00143 outfile = new PrintWriter(new FileWriter(act+".seqlabels.csv"));
00144 outfile.print(act);
00145 outfile.print("\n");
00146 outfile.close();
00147 }
00148
00149
00150 }
00151
00152 static boolean constraintMet(String[] c, ArrayList<String> out) {
00153
00154
00155 for(int o=out.indexOf(c[0]);o<out.size();o++) {
00156 if(out.get(o).equals(c[1]))
00157 return true;
00158 }
00159
00160 return false;
00161 }
00162 }
00163
00164