/** * Compare the target segments to the 'otherSegments', and update the classifier by sum_x * [delta*x], for each example x corresponding to a target segment that's not in otherSegments. */ private int compareSegmentsAndIncrement( ExampleSchema schema, Segmentation segments, Segmentation otherSegments, Hyperplane[] accum, double delta, CandidateSegmentGroup g) { int errors = 0; // first, work out the name of the previous class for each segment Map<Segment, String> map = previousClassMap(segments, schema); Map<Segment, String> otherMap = previousClassMap(otherSegments, schema); String[] history = new String[1]; for (Iterator<Segment> j = segments.iterator(); j.hasNext(); ) { Segmentation.Segment seg = j.next(); String previousClass = map.get(seg); if (seg.lo >= 0 && (!otherSegments.contains(seg) || !otherMap.get(seg).equals(previousClass))) { errors++; history[0] = previousClass; Instance instance = new InstanceFromSequence(g.getSubsequenceExample(seg.lo, seg.hi), history); if (DEBUG) log.debug( "class " + schema.getClassName(seg.y) + " update " + delta + " for: " + instance.getSource()); accum[seg.y].increment(instance, delta); } } return errors; }
/** * Build a mapping from segment to string name of previous segment. This should let you look up * segments which are logically equivalent, as well as ones which are pointer-equivalent (==) */ private Map<Segment, String> previousClassMap(Segmentation segments, ExampleSchema schema) { // use a treemap so that logically equivalent segments be mapped to same previousClass Map<Segment, String> map = new TreeMap<Segment, String>(); Segmentation.Segment previousSeg = null; for (Iterator<Segment> j = segments.iterator(); j.hasNext(); ) { Segmentation.Segment seg = j.next(); String previousClassName = previousSeg == null ? NULL_CLASS_NAME : schema.getClassName(previousSeg.y); map.put(seg, previousClassName); previousSeg = seg; } return map; }