Imports: { import static gate.Utils.*; } Phase: PaymentTerms Input: PaymenttermsPre Options: control=once Rule: PaymentTermsS ( {PaymenttermsPre.kind == "sentence"} ):tmp --> { AnnotationSet pts = inputAS.get("PaymenttermsPre"); AnnotationSet dates = inputAS.get("Date"); AnnotationSet lines = inputAS.get("SpaceControl"); class Match implements Comparable { long interval_start; long interval_end; long d_start; long d_end; double confidence; Match(long a, long b, long c, long d, double e) { interval_start = a; interval_end = b; d_start = c; d_end = d; confidence = e; } public int compareTo(Match o) { return Double.compare(confidence, o.confidence); } } Map> matches = new HashMap<>(); for (Annotation pt : pts) { if (!((String)pt.getFeatures().get("kind")).equals("sentence")) { continue; } long pt_end = end(pt); long pt_start = start(pt); for (Annotation d : dates) { long distance = -1; long d_end = end(d); long d_start = start(d); if (d_end - d_start < 5) { continue; } long interval_start = -1; long interval_end = -1; if (d_end < pt_start) { distance = pt_start - d_end; interval_start = d_start; interval_end = pt_end; } else if (pt_end < d_start) { distance = d_start - pt_end; interval_start = pt_start; interval_end = d_end; } long distance_limit = 200; if (distance < 0 || distance > distance_limit) { continue; } long line_distance = lines.getContained(interval_start, interval_end).size(); if (line_distance > 4) { continue; } double confidence = 0.5 + 0.4 * (1.0 - ((double)distance / (double)distance_limit)); List ms = matches.get(d); if (ms == null) { ms = new ArrayList<>(); matches.put(d, ms); } ms.add(new Match(interval_start, interval_end, d_start, d_end, confidence)); } } for (Map.Entry> entry : matches.entrySet()) { List ms = entry.getValue(); Annotation ann = entry.getKey(); Match m = Collections.max(ms); FeatureMap vdf = Factory.newFeatureMap(); vdf.putAll(ann.getFeatures()); vdf.put("rule", "PaymentTermsS"); vdf.put("confidence", m.confidence); FeatureMap ptf = Factory.newFeatureMap(); ptf.put("rule", "PaymentTermsS"); ptf.put("confidence", m.confidence); try { outputAS.add(m.d_start, m.d_end, "ValueDate", vdf); outputAS.add(m.interval_start, m.interval_end, "PaymentTerms", ptf); } catch (InvalidOffsetException e) { throw new GateRuntimeException("invalid offset in PaymentTermsS"); } } }