Skip to content

Commit

Permalink
Trivial code cleanup. Bug fix, use equals() not == for strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
kno10 committed Aug 1, 2017
1 parent 8bf5523 commit 1073eac
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,66 @@
import java.util.regex.Pattern;

import org.apache.uima.UimaContext;
import org.apache.uima.cas.FSIterator;
import org.apache.uima.jcas.JCas;

import de.unihd.dbs.uima.types.heideltime.Timex3;

public class DecadeProcessor extends GenericProcessor {

/**
* Constructor just calls the parent constructor here.
*/
public DecadeProcessor() {
super();
}


/**
* not needed here
* not needed here
*/
public void initialize(UimaContext aContext) {
return;
}

/**
* all the functionality was put into evaluateCalculationFunctions().
*/
public void process(JCas jcas) {
evaluateFunctions(jcas);
}



/**
* This function replaces function calls from the resource files with their TIMEX value.
*
* @author Hans-Peter Pfeiffer
* @param jcas
*/
public void evaluateFunctions(JCas jcas) {

// build up a list with all found TIMEX expressions
List<Timex3> linearDates = new ArrayList<Timex3>();
FSIterator iterTimex = jcas.getAnnotationIndex(Timex3.type).iterator();
Iterable<Timex3> timexes = jcas.getAnnotationIndex(Timex3.type);

// Create List of all Timexes of types "date" and "time"
while (iterTimex.hasNext()) {
Timex3 timex = (Timex3) iterTimex.next();
if (timex.getTimexType().equals("DATE")) {
for (Timex3 timex : timexes)
if (timex.getTimexType().equals("DATE"))
linearDates.add(timex);
}
}



//////////////////////////////////////////////
// go through list of Date and Time timexes //
//////////////////////////////////////////////
//compile regex pattern for validating commands/arguments
Pattern cmd_p = Pattern.compile("(\\w\\w\\w\\w)-(\\w\\w)-(\\w\\w)\\s+decadeCalc\\((\\d+)\\)");
// compile regex pattern for validating commands/arguments
Matcher cmd_p = Pattern.compile("(\\w\\w\\w\\w)-(\\w\\w)-(\\w\\w)\\s+decadeCalc\\((\\d+)\\)").matcher("");

Matcher cmd_m;
String year;
String valueNew;
String argument;

for (int i = 0; i < linearDates.size(); i++) {
Timex3 t_i = (Timex3) linearDates.get(i);
Timex3 t_i = linearDates.get(i);
String value_i = t_i.getTimexValue();
cmd_m = cmd_p.matcher(value_i);
valueNew = value_i;

if(cmd_m.matches()) {
year = cmd_m.group(1);
argument = cmd_m.group(4);

Matcher cmd_m = cmd_p.reset(value_i);
String valueNew = value_i;

if (cmd_m.matches()) {
String year = cmd_m.group(1);
String argument = cmd_m.group(4);
valueNew = year.substring(0, Math.min(2, year.length())) + argument.substring(0, 1);
}

t_i.removeFromIndexes();
t_i.setTimexValue(valueNew);
t_i.addToIndexes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import de.unihd.dbs.uima.annotator.heideltime.HeidelTimeException;

public class ProcessorInitializationException extends HeidelTimeException {

/**
*
* Serialization version
*/
private static final long serialVersionUID = -4036889037291484936L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import de.unihd.dbs.uima.annotator.heideltime.HeidelTimeException;

public class ProcessorProcessingException extends HeidelTimeException {

/**
*
* Serialization version
*/
private static final long serialVersionUID = 6123306006146166368L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,65 @@
import de.unihd.dbs.uima.types.heideltime.Timex3Interval;

/**
* This class removes TIMEX3 annotations for temponyms and adds TIMEX3INTERVAL annotations containing (earliest|latest)(Begin|End) information.
*
* This class removes TIMEX3 annotations for temponyms and adds
* TIMEX3INTERVAL annotations containing (earliest|latest)(Begin|End) information.
* @author jannik stroetgen
*
*/
public class TemponymPostprocessing {
private static final Logger LOG = LoggerFactory.getLogger(TemponymPostprocessing.class);

private static final Pattern p = Pattern.compile("\\[(.*?), (.*?), (.*?), (.*?)\\]");

public static void handleIntervals(JCas jcas){


public static void handleIntervals(JCas jcas) {
HashSet<Timex3> timexes = new HashSet<>();


Matcher mr = p.matcher("");
// iterate over all TEMPONYMS
AnnotationIndex<Timex3> timex3s = jcas.getAnnotationIndex(Timex3.type);
for(Timex3 t : timex3s) {
if (t.getTimexType().equals("TEMPONYM")) {

// create a timex3interval for each temponym
Timex3Interval ti = new Timex3Interval(jcas);
for (Timex3 t : timex3s) {
if (!t.getTimexType().equals("TEMPONYM"))
continue;
LOG.debug("TEMPONYM: {}", t.getCoveredText());
// create a timex3interval for each temponym
Timex3Interval ti = new Timex3Interval(jcas);

LOG.debug("TEMPONYM: " + t.getCoveredText());

ti.setBegin(t.getBegin());
ti.setEnd(t.getEnd());
ti.setTimexType(t.getTimexType());
ti.setAllTokIds(t.getAllTokIds());
ti.setTimexFreq(t.getTimexFreq());
ti.setTimexMod(t.getTimexMod());
ti.setTimexQuant(t.getTimexQuant());
// set a new id
String id = t.getTimexId();
int newId = Integer.parseInt(id.replace("t", ""));
newId += 100000;
ti.setTimexId("t" + newId);
ti.setBegin(t.getBegin());
ti.setEnd(t.getEnd());
ti.setTimexType(t.getTimexType());
ti.setAllTokIds(t.getAllTokIds());
ti.setTimexFreq(t.getTimexFreq());
ti.setTimexMod(t.getTimexMod());
ti.setTimexQuant(t.getTimexQuant());
// set a new id
String id = t.getTimexId();
int newId = Integer.parseInt(id.replace("t", ""));
newId += 100000;
ti.setTimexId("t" + newId);

// get the (earliest|last)(begin|end) information
for(Matcher mr = p.matcher(t.getTimexValue()); mr.find(); ) {
ti.setTimexValueEB(mr.group(1));
ti.setTimexValueLB(mr.group(2));
ti.setTimexValueEE(mr.group(3));
ti.setTimexValueLE(mr.group(4));
}
//System.err.println("temponym: " + t.getTimexValue());
if ((ti.getTimexValueEB() == ti.getTimexValueLB()) &&
(ti.getTimexValueLB() == ti.getTimexValueEE()) &&
(ti.getTimexValueEE() == ti.getTimexValueLE())) {
ti.setTimexValue(ti.getTimexValueEB());
t.setTimexValue(ti.getTimexValueEB());
}
else { // what's the best single value for an interval!?
t.setEmptyValue(t.getTimexValue());
ti.setTimexValue(ti.getTimexValueLE());
t.setTimexValue(ti.getTimexValueLE());
}
ti.setFoundByRule(t.getFoundByRule());
ti.addToIndexes();
timexes.add(t);
// get the (earliest|last)(begin|end) information
for (mr.reset(t.getTimexValue()); mr.find();) {
ti.setTimexValueEB(mr.group(1));
ti.setTimexValueLB(mr.group(2));
ti.setTimexValueEE(mr.group(3));
ti.setTimexValueLE(mr.group(4));
}
// System.err.println("temponym: " + t.getTimexValue());
if (ti.getTimexValueEB().equals(ti.getTimexValueLB()) && //
ti.getTimexValueLB().equals(ti.getTimexValueEE()) && //
ti.getTimexValueEE().equals(ti.getTimexValueLE())) {
ti.setTimexValue(ti.getTimexValueEB());
t.setTimexValue(ti.getTimexValueEB());
} else { // what's the best single value for an interval!?
t.setEmptyValue(t.getTimexValue());
ti.setTimexValue(ti.getTimexValueLE());
t.setTimexValue(ti.getTimexValueLE());
}
ti.setFoundByRule(t.getFoundByRule());
ti.addToIndexes();
timexes.add(t);
}
// shall the standard timexes really be removed?
for (Timex3 t : timexes){
for (Timex3 t : timexes)
t.removeFromIndexes();
}
}
}

0 comments on commit 1073eac

Please sign in to comment.