use of edu.cmu.lti.javelin.util.RangeMap in project lucida by claritylab.
the class StanfordParser method mapOffsets.
/**
* Maps Tree node offsets using provided mapping.
* @param tree the Tree whose begin and end extents should be mapped.
* @param mapping the list of RangeMap objects which defines the mapping.
*/
protected static void mapOffsets(Tree tree, List<RangeMap> mapping) {
// if mapping is empty, then assume 1-to-1 mapping.
if (mapping == null || mapping.size() == 0)
return;
int begin_map_index = 0;
RangeMap begin_rmap = mapping.get(begin_map_index);
TREE: for (Tree t : tree) {
if (t.isLeaf())
continue;
MapLabel label = (MapLabel) t.label();
int begin = (Integer) label.get(BEGIN_KEY);
// "end" must be index of last char in range
int end = (Integer) label.get(END_KEY) - 1;
// annotation.begin");
while (begin_rmap.end <= begin) {
begin_map_index++;
if (begin_map_index >= mapping.size())
break TREE;
begin_rmap = mapping.get(begin_map_index);
}
// mapping is 1-to-1).
if (begin_rmap.begin > end) {
// mapping)");
continue;
}
// if beginning of current annotation falls within current range
// map, then map it back to source space.
int new_begin = begin;
if (begin_rmap.begin <= new_begin) {
// log.debug("Applying RangeMap to begin offset");
new_begin = begin_rmap.map(new_begin);
}
// find the first rangemap whose end is greater than the end of
// current annotation.
// log.debug("Finding RangeMap whose extents include
// annotation.end");
int end_map_index = begin_map_index;
RangeMap end_rmap = begin_rmap;
END_OFFSET: while (end_rmap.end <= end) {
end_map_index++;
if (end_map_index >= mapping.size())
break END_OFFSET;
end_rmap = mapping.get(end_map_index);
}
// if end of current annotation falls within "end" range map,
// then map it back to source space.
int new_end = end;
if (end_rmap.begin <= end) {
// log.debug("Applying RangeMap to end offset");
new_end = end_rmap.map(end);
}
label.put(BEGIN_KEY, new_begin);
label.put(END_KEY, new_end + 1);
}
}
use of edu.cmu.lti.javelin.util.RangeMap in project lucida by claritylab.
the class StanfordParser method createMapping.
/**
* @param sentence
* @return a list of RangeMap objects which define a mapping of character
* offsets in a white-space depleted version of the input string back into
* offsets in the input string.
*/
protected static List<RangeMap> createMapping(String sentence) {
List<RangeMap> mapping = new LinkedList<RangeMap>();
Matcher whitespace_matcher = whitespace_pattern.matcher(sentence);
DeltaRangeMap delta_rmap = null;
// find all sequences of whitespace chars
while (whitespace_matcher.find()) {
int start = whitespace_matcher.start();
int end = whitespace_matcher.end();
int length = end - start;
if (delta_rmap == null) {
// create a new RangeMap object whose start begins at current
// match start, and whose end is at the moment undefined. The
// delta here is taken to be the length of the whitespace
// sequence.
delta_rmap = new DeltaRangeMap(start, 0, length);
} else {
// we've found the next sequence of whitespace chars, so we
// finalize the end extent of the previous RangeMap, and make a
// new RangeMap to describe the mapping from this point forward.
delta_rmap.end = start - delta_rmap.delta;
mapping.add(delta_rmap);
delta_rmap = new DeltaRangeMap(delta_rmap.end, 0, delta_rmap.delta + length);
}
}
// process trailing DeltaRangeMap if it exists
if (delta_rmap != null) {
delta_rmap.end = sentence.length() - delta_rmap.delta;
mapping.add(delta_rmap);
}
return mapping;
}
Aggregations