use of com.google.startupos.common.Protos.WordChange in project startup-os by google.
the class TextDifferencer method addWordChanges.
// Adds WordChanges, from segmentStart until segmentEnd (inclusive).
private void addWordChanges(List<DiffLine> leftLines, List<DiffLine> rightLines, int segmentStart, int segmentEnd) {
DiffMatchPatch diffMatchPatch = new DiffMatchPatch();
LinkedList<DiffMatchPatch.Diff> diffs = diffMatchPatch.diff_main(getMultilineText(leftLines, segmentStart, segmentEnd), getMultilineText(rightLines, segmentStart, segmentEnd));
diffMatchPatch.diff_cleanupSemantic(diffs);
// Split multi-lines
int leftLineCounter = 0;
int rightLineCounter = 0;
int leftCharCounter = 0;
int rightCharCounter = 0;
for (DiffMatchPatch.Diff diff : diffs) {
String[] diffLines = diff.text.split("\n");
ChangeType type = getChangeType(diff.operation);
for (int i = 0; i < diffLines.length; i++) {
if (i > 0) {
// This means we had a newline
leftCharCounter = 0;
rightCharCounter = 0;
if (type == ChangeType.DELETE || type == ChangeType.NO_CHANGE) {
leftLineCounter++;
}
if (type == ChangeType.ADD || type == ChangeType.NO_CHANGE) {
rightLineCounter++;
}
}
String line = diffLines[i];
int charCounter = type == ChangeType.DELETE ? leftCharCounter : rightCharCounter;
WordChange wordChange = WordChange.newBuilder().setText(line).setStartIndex(charCounter).setEndIndex(charCounter + line.length()).setType(type).build();
if (type == ChangeType.DELETE) {
int index = segmentStart + leftLineCounter;
if (!wordChangeIsWholeLine(leftLines.get(index), wordChange)) {
leftLines.set(index, leftLines.get(index).toBuilder().addWordChange(wordChange).build());
}
} else if (type == ChangeType.ADD) {
int index = segmentStart + rightLineCounter;
if (!wordChangeIsWholeLine(rightLines.get(index), wordChange)) {
rightLines.set(index, rightLines.get(index).toBuilder().addWordChange(wordChange).build());
}
}
if (type == ChangeType.DELETE || type == ChangeType.NO_CHANGE) {
leftCharCounter += line.length();
}
if (type == ChangeType.ADD || type == ChangeType.NO_CHANGE) {
rightCharCounter += line.length();
}
}
}
}
Aggregations