getEvents() {
21 | return events;
22 | }
23 |
24 | public String getBodyText() {
25 | return bodyText;
26 | }
27 |
28 | public String getDocId() {
29 | return docId;
30 | }
31 | public RedEvent getEventFromId(int eventid){
32 | for(RedEvent e:events){
33 | if(e.getId()==eventid)
34 | return e;
35 | }
36 | return null;
37 | }
38 | public String Render4Crowdflower(String color){
39 | StringBuilder sb = new StringBuilder();
40 | for(RedEvent e:events){
41 | sb.append(String.format("\"%s\",\"%d\",\"%s\",\"%s\"\n",docId,e.getId(),e.getText(),e.Render4Crowdflower("red")));
42 | }
43 | return sb.toString();
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/readers/RED/RedEvent.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.readers.RED;
2 |
3 | public class RedEvent{
4 | protected class IntPair{
5 | private int first, second;
6 |
7 | public IntPair(int first, int second) {
8 | this.first = first;
9 | this.second = second;
10 | }
11 |
12 | public int getFirst() {
13 | return first;
14 | }
15 |
16 | public int getSecond() {
17 | return second;
18 | }
19 | }
20 | private String polarity,contextualModality;
21 | private IntPair charSpan;//right exclusive
22 | private String text;
23 | private int id;
24 | private RedDoc doc;
25 |
26 | private static String nodeType = "RedEvent";
27 |
28 | // Constructors
29 | public RedEvent(RedDoc doc, int id, int charStart, int charEnd, String polarity, String contextualModality) {
30 | this.doc = doc;
31 | this.id = id;
32 | charSpan = new IntPair(charStart,charEnd);
33 | if(charStart<0||charEnd>=doc.getBodyText().length()){
34 | System.out.println("Invalid char spans.");
35 | }
36 | this.text = doc.getBodyText().substring(charStart,charEnd);
37 | this.polarity = polarity;
38 | this.contextualModality = contextualModality;
39 | }
40 |
41 |
42 |
43 | // other functions
44 | public String toString() {
45 | return String.format("Doc=%s, id=%d ([%d,%d)): %s, POLARITY=%s, MODALITY=%s\n",
46 | doc.getDocId(),id,charSpan.getFirst(),charSpan.getSecond(),text,polarity,contextualModality);
47 | }
48 |
49 | public String getPolarity() {
50 | return polarity;
51 | }
52 |
53 | public String getContextualModality() {
54 | return contextualModality;
55 | }
56 |
57 | public IntPair getCharSpan() {
58 | return charSpan;
59 | }
60 |
61 | public String getText() {
62 | return text;
63 | }
64 |
65 | public int getId() {
66 | return id;
67 | }
68 |
69 | public RedDoc getDoc() {
70 | return doc;
71 | }
72 |
73 | public static String getNodeType() {
74 | return nodeType;
75 | }
76 | public String Render4Crowdflower(String color){
77 | int charStart = charSpan.getFirst(), charEnd = charSpan.getSecond();
78 | StringBuilder sb = new StringBuilder();
79 | String left = doc.getBodyText().substring(0,charStart).replaceAll("\n"," ").replaceAll("\""," ");
80 | left = left.substring(left.lastIndexOf(">")+1,left.length());
81 | String right = doc.getBodyText().substring(charEnd).replaceAll("\n"," ").replaceAll("\""," ");
82 | int tmp = right.indexOf("<");
83 | right = right.substring(0,tmp>0?tmp:right.length());
84 | sb.append("");
85 | sb.append(left);
86 | sb.append(String.format("%s",color,text));
87 | sb.append(right);
88 | sb.append("
");
89 | return sb.toString();
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/readers/axisAnnotationReader.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.readers;
2 |
3 | import edu.illinois.cs.cogcomp.temporal.utils.IO.myIOUtils;
4 |
5 | import java.util.HashMap;
6 |
7 | public class axisAnnotationReader {
8 | public static String LABEL_NOT_ON_ANY_AXIS = "null";
9 | public static String LABEL_ON_MAIN_AXIS = "main";
10 | public static String LABEL_ON_ORTHOGONAL_AXIS = "orthogonal";
11 | public static String LABEL_ON_NEGATION_AXIS = "negation";
12 | public static String LABEL_ON_HYPO_AXIS = "hypothesis";
13 | public static String LABEL_ON_OTHER_AXIS = "others";
14 | public static String axis_label_conversion(String label){
15 | switch (label){
16 | case "yes"://herited from older version of anchorability
17 | case "yes_its_anchorable":
18 | return LABEL_ON_MAIN_AXIS;
19 | case "no_its_intentionwishopinion":
20 | //return LABEL_ON_ORTHOGONAL_AXIS;
21 | case "no_its_negation":
22 | //return LABEL_ON_NEGATION_AXIS;
23 | case "no_its_hypotheticalcondition":
24 | //return LABEL_ON_HYPO_AXIS;
25 | case "no_its_recurrent":
26 | case "no_its_static":
27 | case "no_its_abstractnonspecific":
28 | return LABEL_ON_OTHER_AXIS;
29 | default:
30 | return LABEL_NOT_ON_ANY_AXIS;
31 | }
32 | }
33 | public static HashMap> readAxisMapFromCrowdFlower(String fileList){
34 | // docid-->eventid (index in doc)-->axis_label
35 | HashMap> axisMap = new HashMap<>();
36 | String[] files = fileList.split(",");
37 | for(String file:files){
38 | String tmpDir = myIOUtils.getParentDir(file);
39 | String tmpFile = myIOUtils.getFileOrDirName(file);
40 | myCSVReader cf_reader = new myCSVReader(tmpDir,tmpFile);
41 | for(int i=0;i());
50 | axisMap.get(docid).put(eventid,anchorability);
51 | }
52 | catch (Exception e){
53 | e.printStackTrace();
54 | }
55 | }
56 | }
57 | return axisMap;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/readers/myCSVReader.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.readers;
2 |
3 | import com.opencsv.CSVReader;
4 | import org.jetbrains.annotations.NotNull;
5 |
6 | import java.io.File;
7 | import java.io.FileReader;
8 | import java.util.ArrayList;
9 | import java.util.HashMap;
10 | import java.util.List;
11 |
12 | public class myCSVReader {
13 | private String dir = "";
14 | private String fname = "";
15 | private String[] header;
16 | private List content = new ArrayList<>();
17 | private HashMap col2idx = new HashMap<>();
18 |
19 | public myCSVReader(String dir, String fname) {
20 | this.dir = dir;
21 | this.fname = fname;
22 | try {
23 | CSVReader csvReader = new CSVReader(new FileReader(dir+ File.separator+fname));
24 | content = csvReader.readAll();
25 | if(content==null||content.isEmpty()){
26 | System.out.println("File "+dir+" is empty.");
27 | return;
28 | }
29 | extractHeader();
30 | }
31 | catch (Exception e){
32 | System.out.println("Failed loading file "+dir);
33 | e.printStackTrace();
34 | }
35 | }
36 | private void extractHeader(){
37 | header = content.get(0);
38 | for(int i=0;i=getContentLines()){
69 | System.out.println("Line "+line+" doesn't exist.");
70 | return "";
71 | }
72 | return content.get(line)[idx];
73 | }
74 |
75 | public String getDir() {
76 | return dir;
77 | }
78 |
79 | public String getFname() {
80 | return fname;
81 | }
82 |
83 | public String[] getHeader() {
84 | return header;
85 | }
86 |
87 | public List getContent() {
88 | return content;
89 | }
90 |
91 | public HashMap getCol2idx() {
92 | return col2idx;
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/CoDL/ScoringFunc.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils.CoDL;
2 |
3 | import edu.illinois.cs.cogcomp.lbjava.classify.ScoreSet;
4 |
5 | public interface ScoringFunc {
6 | ScoreSet scores(LearningObj obj);
7 | String discreteValue(LearningObj obj);
8 | }
9 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/CoDL/TempRelLabelerMultiLBJ.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils.CoDL;
2 |
3 | import edu.illinois.cs.cogcomp.lbjava.learn.Learner;
4 | import edu.illinois.cs.cogcomp.temporal.TemporalRelationExtractor.TempRelLabeler;
5 | import edu.illinois.cs.cogcomp.temporal.datastruct.Temporal.TemporalRelType;
6 | import edu.illinois.cs.cogcomp.temporal.datastruct.Temporal.TemporalRelation;
7 | import edu.illinois.cs.cogcomp.temporal.datastruct.Temporal.TemporalRelation_EE;
8 | import edu.illinois.cs.cogcomp.temporal.utils.CoDL.MultiClassifiers;
9 | import org.jetbrains.annotations.NotNull;
10 |
11 | import static edu.illinois.cs.cogcomp.temporal.datastruct.Temporal.TemporalRelType.getNullTempRel;
12 |
13 | public class TempRelLabelerMultiLBJ extends TempRelLabeler{
14 | private MultiClassifiers multiClassifiers;
15 |
16 | public TempRelLabelerMultiLBJ(MultiClassifiers multiClassifiers) {
17 | this.multiClassifiers = multiClassifiers;
18 | }
19 |
20 | public TempRelLabelerMultiLBJ(Learner cls, double lambda, boolean norm_before_merge) {
21 | super();
22 | multiClassifiers = new MultiClassifiers<>(cls, lambda, norm_before_merge);
23 | }
24 |
25 | public TempRelLabelerMultiLBJ(double lambda, boolean norm_before_merge) {
26 | super();
27 | multiClassifiers = new MultiClassifiers<>(lambda, norm_before_merge);
28 | }
29 |
30 | public void addClassifier(Learner classifier){
31 | multiClassifiers.addClassifier(classifier);
32 | }
33 |
34 | public void dropClassifier(){
35 | multiClassifiers.dropClassifier();
36 | }
37 |
38 | public MultiClassifiers getMultiClassifiers() {
39 | return multiClassifiers;
40 | }
41 |
42 | @Override
43 | public boolean isIgnore(TemporalRelation ee) {
44 | return ! (ee instanceof TemporalRelation_EE)
45 | ||Math.abs(ee.getSentDiff()) > 1;
46 | }
47 |
48 | @Override
49 | @NotNull
50 | public TemporalRelType tempRelLabel(TemporalRelation ee) {
51 | TemporalRelType ret = getNullTempRel();
52 | if(isIgnore(ee))
53 | return ret;
54 | if (ee.getSentDiff() == 0 || ee.getSentDiff() == 1) {
55 | ret = new TemporalRelType(multiClassifiers.discreteValue((TemporalRelation_EE)ee));
56 | ret.setScores(temporalScores2doubles(multiClassifiers.scores((TemporalRelation_EE)ee), TemporalRelType.relTypes.getAllNames(), true));
57 | }
58 | return ret;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/CrossValidation/CVWrapper_LBJ.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils.CrossValidation;
2 |
3 | import edu.illinois.cs.cogcomp.core.io.IOUtils;
4 | import edu.illinois.cs.cogcomp.lbjava.learn.Learner;
5 | import edu.illinois.cs.cogcomp.lbjava.learn.Lexicon;
6 | import edu.illinois.cs.cogcomp.temporal.utils.ExecutionTimeUtil;
7 | import edu.illinois.cs.cogcomp.temporal.utils.PrecisionRecallManager;
8 |
9 | import java.io.File;
10 | import java.util.List;
11 |
12 | public abstract class CVWrapper_LBJ extends CrossValidationWrapper {
13 | protected Learner classifier;
14 | protected List testStructs;
15 | protected int evalMetric = 2;//0:prec. 1: recall. 2: f1
16 | protected String modelPath, lexiconPath;
17 | protected static String[] LABEL_TO_IGNORE = new String[]{};
18 |
19 | public CVWrapper_LBJ(int seed, int totalFold, int evalMetric) {
20 | super(seed, totalFold);
21 | this.evalMetric = evalMetric;
22 | }
23 | public void setModelPath(String dir, String name) {
24 | IOUtils.mkdir(dir);
25 | modelPath = dir+ File.separator+name+".lc";
26 | lexiconPath = dir+File.separator+name+".lex";
27 | }
28 |
29 | public abstract String getLabel(LearningStruct learningStruct);
30 | @Override
31 | public double evaluate(List slist, int verbose) {
32 | ExecutionTimeUtil timer = new ExecutionTimeUtil();
33 | PrecisionRecallManager evaluator = new PrecisionRecallManager();
34 | timer.start();
35 | for(LearningStruct learningStruct:slist){
36 | String p = classifier.discreteValue(learningStruct);
37 | String l = getLabel(learningStruct);
38 | evaluator.addPredGoldLabels(p,l);
39 | }
40 | timer.end();
41 | if(verbose>0) {
42 | evaluator.printPrecisionRecall(LABEL_TO_IGNORE);
43 | }
44 | double res;
45 | switch(evalMetric){
46 | case 0:
47 | res = evaluator.getResultStruct(LABEL_TO_IGNORE).prec;
48 | break;
49 | case 1:
50 | res = evaluator.getResultStruct(LABEL_TO_IGNORE).rec;
51 | break;
52 | case 2:
53 | res = evaluator.getResultStruct(LABEL_TO_IGNORE).f;
54 | break;
55 | default:
56 | res = evaluator.getResultStruct(LABEL_TO_IGNORE).f;
57 | }
58 | return res;
59 | }
60 | public double evaluateTest(){
61 | System.out.println("-------------------");
62 | System.out.println("Evaluating TestSet...");
63 | return evaluate(testStructs,1);
64 | }
65 | public void saveClassifier(){
66 | classifier.write(modelPath,lexiconPath);
67 | }
68 |
69 | public static void StandardExperiment(CVWrapper_LBJ exp){
70 | // remember to setup model names before calling this
71 | exp.load();
72 | exp.myParamTuner();
73 | exp.retrainUsingBest();
74 | Lexicon lex = exp.classifier.getLexicon();
75 | System.out.printf("Lexicon size: %d\n",lex.size());
76 | exp.evaluateTest();
77 | exp.saveClassifier();
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/CrossValidation/CVWrapper_LBJ_Perceptron.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils.CrossValidation;
2 |
3 | import edu.illinois.cs.cogcomp.temporal.utils.myLogFormatter;
4 |
5 | import java.util.Arrays;
6 | import java.util.Collections;
7 | import java.util.List;
8 | import java.util.Random;
9 |
10 | public abstract class CVWrapper_LBJ_Perceptron extends CVWrapper_LBJ {
11 | protected static double[] LEARNRATE,THICKNESS,SAMRATE,ROUND;
12 |
13 | public CVWrapper_LBJ_Perceptron(int seed, int totalFold, int evalMetric) {
14 | super(seed, totalFold, evalMetric);
15 | }
16 |
17 | public abstract List SetLrThSrCls(double lr, double th, double sr,List slist);
18 | @Override
19 | public void learn(List slist, double[] param, int seed) {
20 | double lr = param[0];
21 | double th = param[1];
22 | double sr = param[2];
23 | int round = (int) Math.round(param[3]);
24 | List slist_sample = SetLrThSrCls(lr,th,sr,slist);
25 | classifier.forget();
26 | classifier.beginTraining();
27 | for(int iter=0;iter allIdx = new ArrayList<>();
15 | public CrossValidationSplit(int TotalFold, int SampleSize, int seed){
16 | this.TotalFold = TotalFold;
17 | this.SampleSize = SampleSize;
18 | this.seed = seed;
19 | for(int i=0;i getFold(int fold){
25 | if(fold < 1 || fold > TotalFold)
26 | return null;
27 | int left = (int)(1.0*(fold-1)*SampleSize/TotalFold);
28 | int right = (int)(1.0*fold*SampleSize/TotalFold);
29 | left = left<0? 0 : left;
30 | right = right>SampleSize? SampleSize:right;
31 | return allIdx.subList(left,right);
32 | }
33 | public Pair,List> TrainTestSplit(int fold){
34 | if(fold < 1 || fold > TotalFold)
35 | return null;
36 | List test = getFold(fold);
37 | List train = new ArrayList<>();
38 | for(int i = 1; i <= TotalFold; i++){
39 | if(i == fold)
40 | continue;
41 | train.addAll(getFold(i));
42 | }
43 | return new Pair<>(train,test);
44 |
45 | }
46 | public static void main(String[] args) {
47 | int TotalFold = 3;
48 | int SampleSize = 10;
49 | CrossValidationSplit cv = new CrossValidationSplit(TotalFold,SampleSize,1);
50 | System.out.println(cv.allIdx);
51 | for(int i = 1;i<=TotalFold;i++) {
52 | System.out.println("******Fold: "+i+"******\nTrain:");
53 | System.out.println(cv.TrainTestSplit(i).getFirst()+"\nTest:");
54 | System.out.println(cv.TrainTestSplit(i).getSecond());
55 | }
56 | }
57 | }
58 |
59 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/ExecutionTimeUtil.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils;
2 |
3 | import edu.illinois.cs.cogcomp.core.utilities.ExecutionTimer;
4 |
5 | /**
6 | * Created by qning2 on 11/28/16.
7 | */
8 | public class ExecutionTimeUtil extends ExecutionTimer {
9 |
10 | public void print(){
11 | System.out.println("Total Time: "+getTimeSeconds());
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/GraphVisualizer/edge.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils.GraphVisualizer;
2 |
3 | public class edge {
4 | private int source;
5 | private int target;
6 | private String label;
7 | private int length;
8 | private int colorId;
9 | private String markerEnd;
10 |
11 | public int getSource() {
12 | return source;
13 | }
14 |
15 | public void setSource(int source) {
16 | this.source = source;
17 | }
18 |
19 | public int getTarget() {
20 | return target;
21 | }
22 |
23 | public void setTarget(int target) {
24 | this.target = target;
25 | }
26 |
27 | public String getLabel() {
28 | return label;
29 | }
30 |
31 | public void setLabel(String label) {
32 | this.label = label;
33 | }
34 |
35 | public edge(int source, int target, String label, int length, int colorId, String markerEnd) {
36 | this.source = source;
37 | this.target = target;
38 | this.label = label;
39 | this.length = length;
40 | this.colorId = colorId;
41 | this.markerEnd = markerEnd;
42 | }
43 |
44 | public String toString4d3(){
45 | return String.format("source: %d, target: %d, label: \"%s\", len: %d, color: %d, markerend: \"%s\"",source,target,label,length, colorId,markerEnd);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/GraphVisualizer/vertex.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils.GraphVisualizer;
2 |
3 | public class vertex {
4 | private String uniqueid;
5 | private String text;
6 | private int colorId;
7 |
8 | public vertex(String uniqueid, String text, int colorId) {
9 | this.uniqueid = uniqueid;
10 | this.text = text;
11 | this.colorId = colorId;
12 | }
13 |
14 | public boolean equals(vertex v2){
15 | return uniqueid.equals(v2.uniqueid);
16 | }
17 |
18 | public String getUniqueid() {
19 | return uniqueid;
20 | }
21 |
22 | public String getText() {
23 | return text;
24 | }
25 |
26 | public String toString4d3(){
27 | return String.format("name:\"%s\", color: %d", toString(),colorId);
28 | }
29 |
30 | public String toString(){
31 | return uniqueid+":"+text;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/IO/myIOUtils.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils.IO;
2 |
3 | import edu.illinois.cs.cogcomp.core.io.IOUtils;
4 | import org.jetbrains.annotations.NotNull;
5 |
6 | import java.io.File;
7 |
8 | public class myIOUtils extends IOUtils {
9 | @NotNull
10 | public static String removeLastSeparatorIfExists(String path){
11 | while(path.endsWith(File.separator))// hopefully we don't have cases like "/foo///.../"
12 | path = path.substring(0,path.length()-1);
13 | return path;
14 | }
15 | public static String getParentDir(String path){
16 | path = removeLastSeparatorIfExists(path);
17 | int slashIndex = path.lastIndexOf(File.separator);
18 | return path.substring(0,slashIndex);
19 | }
20 | public static String getFileOrDirName(String path) {
21 | path = removeLastSeparatorIfExists(path);
22 | int slashIndex = path.lastIndexOf(File.separator);
23 | return path.substring(slashIndex + 1);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/IO/mySerialization.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils.IO;
2 |
3 | import java.io.*;
4 |
5 | public class mySerialization {
6 | private boolean verbose;
7 |
8 | public mySerialization() {
9 | verbose = true;
10 | }
11 |
12 | public mySerialization(boolean verbose) {
13 | this.verbose = verbose;
14 | }
15 |
16 | public void serialize(Object obj, String path) throws Exception{
17 | File serializedFile = new File(path);
18 | FileOutputStream fileOut = new FileOutputStream(serializedFile.getPath());
19 | ObjectOutputStream out = new ObjectOutputStream(fileOut);
20 | out.writeObject(obj);
21 | out.close();
22 | fileOut.close();
23 | if(verbose)
24 | System.out.println("Serialization of object has been saved to "+serializedFile.getPath());
25 | }
26 | public Object deserialize(String path) throws Exception{
27 | File serializedFile = new File(path);
28 | Object obj = null;
29 | if(serializedFile.exists()){
30 | if(verbose)
31 | System.out.println("Serialization exists. Loading from "+serializedFile.getPath());
32 | FileInputStream fileIn = new FileInputStream(serializedFile.getPath());
33 | ObjectInputStream in = new ObjectInputStream(fileIn);
34 | obj = in.readObject();
35 | in.close();
36 | fileIn.close();
37 | }
38 | else{
39 | if(verbose)
40 | System.out.println("Serialization doesn't exist. Return null. ");
41 | }
42 | return obj;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/ListSampler.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Random;
6 |
7 | public class ListSampler {
8 | public interface ListElementLabeler{
9 | boolean labeler(T element);// those elements with "True" labels won't be affected by samplingRate
10 | }
11 |
12 | ListElementLabeler listElementLabeler;
13 |
14 | public ListSampler(ListElementLabeler listElementLabeler) {
15 | this.listElementLabeler = listElementLabeler;
16 | }
17 |
18 | public double autoSelectSamplingRate(List list){
19 | int cnt1 = 0, cnt2 = 0;
20 | for(T element:list){
21 | if(listElementLabeler.labeler(element))
22 | cnt1++;
23 | else
24 | cnt2++;
25 | }
26 | return 1.0*cnt1/cnt2;
27 | }
28 | public List ListSampling(List list, Random rng){
29 | double samplingRate = autoSelectSamplingRate(list);
30 | return ListSampling(list,samplingRate,rng);
31 | }
32 | public List ListSampling(List list, double samplingRate, Random rng){
33 | List newlist = new ArrayList<>();
34 | for(T element:list){
35 | if(listElementLabeler.labeler(element))
36 | newlist.add(element);
37 | else{
38 | if(samplingRate==1)
39 | newlist.add(element);
40 | else if(samplingRate<1){
41 | if(rng.nextDouble() <= samplingRate)
42 | newlist.add(element);
43 | }
44 | else{
45 | double tmp = samplingRate;
46 | for (; tmp > 1; tmp--) {
47 | newlist.add(element);
48 | }
49 | if (rng.nextDouble() <= tmp)
50 | newlist.add(element);
51 | }
52 | }
53 | }
54 | return newlist;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/Triplet.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils;
2 |
3 | /**
4 | * Created by qning2 on 12/21/16.
5 | */
6 | public class Triplet {
7 | private T first;
8 | private F second;
9 | private K third;
10 | public Triplet(T first, F second, K third){
11 | this.first=first;
12 | this.second=second;
13 | this.third=third;
14 | }
15 |
16 | public T getFirst() {
17 | return first;
18 | }
19 |
20 | public void setFirst(T first) {
21 | this.first = first;
22 | }
23 |
24 | public F getSecond() {
25 | return second;
26 | }
27 |
28 | public void setSecond(F second) {
29 | this.second = second;
30 | }
31 |
32 | public K getThird() {
33 | return third;
34 | }
35 |
36 | public void setThird(K third) {
37 | this.third = third;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/WordNet/SynsetNode.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils.WordNet;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * @author qning2 Apr 24, 2018
8 | */
9 | public class SynsetNode {
10 |
11 | public String nodeId;
12 | public List words = null;
13 | public List hyperIds = null;
14 | public List entailIds = null;
15 | public List antonIds = null;
16 | public List derivationIds = null;
17 |
18 | public SynsetNode() {
19 | nodeId = "NULL";
20 | words = new ArrayList();
21 | hyperIds = new ArrayList();
22 | entailIds = new ArrayList();
23 | antonIds = new ArrayList();
24 | derivationIds = new ArrayList();
25 | }
26 |
27 | }
28 |
29 |
--------------------------------------------------------------------------------
/src/main/java/edu/illinois/cs/cogcomp/temporal/utils/myLogFormatter.java:
--------------------------------------------------------------------------------
1 | package edu.illinois.cs.cogcomp.temporal.utils;
2 |
3 | import java.util.Arrays;
4 |
5 | public class myLogFormatter {
6 | public static int gap = 5;
7 | public static int level = 0;
8 | private static char[] tagsLevel = new char[]{'#','*','+','-','.'};
9 | private static char tagAtLevel(){
10 | if(level<0)
11 | return tagsLevel[0];
12 | if(level>=tagsLevel.length)
13 | return tagsLevel[tagsLevel.length-1];
14 | return tagsLevel[level];
15 | }
16 | public static String fullBlockLog(String label){
17 | char tag = tagAtLevel();
18 | label = label.replaceAll("\n"," ");//make sure it's oneline
19 | int len = label.length();
20 | StringBuilder sb = new StringBuilder();
21 | sb.append(lineOfTag(tag,len+gap*2));
22 | sb.append("\n");
23 | sb.append(middleBlockLog(label));
24 | sb.append(lineOfTag(tag,len+gap*2));
25 | sb.append("\n");
26 | return sb.toString();
27 | }
28 | public static String startBlockLog(String label){
29 | char tag = tagAtLevel();
30 | label = label.replaceAll("\n"," ");//make sure it's oneline
31 | int len = label.length();
32 | StringBuilder sb = new StringBuilder();
33 | sb.append(lineOfTag(tag,len+gap*2));
34 | sb.append("\n");
35 | sb.append(middleBlockLog(label));
36 | level++;
37 | if(level>=tagsLevel.length)
38 | level = tagsLevel.length-1;
39 | return sb.toString();
40 | }
41 | public static String endBlockLog(String label){
42 | level--;
43 | if(level<0)
44 | level = 0;
45 | char tag = tagAtLevel();
46 | label = label.replaceAll("\n"," ");//make sure it's oneline
47 | int len = label.length();
48 | StringBuilder sb = new StringBuilder();
49 | sb.append(middleBlockLog(label));
50 | sb.append(lineOfTag(tag,len+gap*2));
51 | sb.append("\n");
52 | return sb.toString();
53 | }
54 | public static String middleBlockLog(String label){
55 | char tag = tagAtLevel();
56 | label = label.replaceAll("\n"," ");//make sure it's oneline
57 | int len = label.length();
58 | StringBuilder sb = new StringBuilder();
59 | sb.append(tag);
60 | sb.append(lineOfTag(' ',len+gap*2-2));
61 | sb.append(tag+"\n"+tag);
62 | sb.append(lineOfTag(' ',gap-1));
63 | sb.append(label);
64 | sb.append(lineOfTag(' ',gap-1));
65 | sb.append(tag+"\n"+tag);
66 | sb.append(lineOfTag(' ',len+gap*2-2));
67 | sb.append(tag+"\n");
68 | return sb.toString();
69 | }
70 | public static String lineOfTag(char tag, int len){
71 | char[] line = new char[len];
72 | Arrays.fill(line,tag);
73 | return String.valueOf(line);
74 | }
75 |
76 | public static void main(String[] args) {
77 | System.out.println(startBlockLog("testtesttest"));
78 | System.out.println(startBlockLog("testtesttest"));
79 | System.out.println(endBlockLog("testtesttest"));
80 | System.out.println(endBlockLog("testtesttest"));
81 | }
82 | }
83 |
--------------------------------------------------------------------------------