getBlockMarkers function
Revises the given rawMarkers, merges overlapping or embedded words
to one big "block" and returns a list of WordMarkers which represents
such "clean" word blocks.
Sorts the given rawMarkers by their index from lowest to highest.
Implementation
List<WordMarker> getBlockMarkers(final List<WordMarker> rawMarkers) {
final List<WordMarker> revisedMarkers = <WordMarker>[];
// Sort raw markers by index, ascending.
// Combine neighboring or overlapping blocks to one large block.
rawMarkers.sort(sortByIndex);
/// Counts the number of open blocks.
/// When a start marker changes this value from 0 to 1, a new block starts.
/// When an end marker changes this value from 1 to 0, a block ends.
/// All other constellations are ignored,
/// as they mean overlapping or embedded blocks.
int openCount = 0;
for (WordMarker rawMarker in rawMarkers) {
openCount += rawMarker.type == MarkerType.start ? 1 : -1;
if (openCount < 0) {
throw Exception('There cannot be less than 0 open blocks');
}
// value changes from 0 to 1 by start marker
if (openCount == 1 && rawMarker.type == MarkerType.start) {
revisedMarkers.add(rawMarker);
}
// value changes from 1 to 0 by end marker
if (openCount == 0 && rawMarker.type == MarkerType.end) {
revisedMarkers.add(rawMarker);
}
}
return revisedMarkers;
}