Issue #1 fixed bug in compareTo method of Interval. Problem was the compareTo only worked on start position, whereas it should also work on end position.

This commit is contained in:
robert-bor 2014-01-31 16:16:21 +01:00
parent 1785a554f3
commit 4c8ea8ba57
4 changed files with 37 additions and 2 deletions

4
.gitignore vendored
View File

@ -1,3 +1,5 @@
.idea
*.iml
src/main/java/Main.java
src/main/java/Main.java
*.txt
docs

View File

@ -52,6 +52,13 @@ public class Interval implements Intervalable {
return -1;
}
Intervalable other = (Intervalable)o;
return this.start - other.getStart();
int comparison = this.start - other.getStart();
return comparison != 0 ? comparison : this.end - other.getEnd();
}
@Override
public String toString() {
return this.start + ":" + this.end;
}
}

View File

@ -16,4 +16,9 @@ public class Emit extends Interval implements Intervalable {
return this.keyword;
}
@Override
public String toString() {
return super.toString() + "=" + this.keyword;
}
}

View File

@ -102,12 +102,33 @@ public class TrieTest {
trie.addKeyword("cba");
trie.addKeyword("ababc");
Collection<Emit> emits = trie.parseText("ababcbab");
assertEquals(2, emits.size());
Iterator<Emit> iterator = emits.iterator();
// With overlaps: ab@1, ab@3, ababc@4, cba@6, ab@7
checkEmit(iterator.next(), 0, 4, "ababc");
checkEmit(iterator.next(), 6, 7, "ab");
}
@Test
public void startOfChurchillSpeech() {
Trie trie = new Trie().removeOverlaps();
trie.addKeyword("T");
trie.addKeyword("u");
trie.addKeyword("ur");
trie.addKeyword("r");
trie.addKeyword("urn");
trie.addKeyword("ni");
trie.addKeyword("i");
trie.addKeyword("in");
trie.addKeyword("n");
trie.addKeyword("urning");
Collection<Emit> emits = trie.parseText("Turning");
for (Emit emit : emits) {
System.out.println(emit.getStart()+":"+emit.getEnd()+"="+emit.getKeyword());
}
assertEquals(2, emits.size());
}
private void checkEmit(Emit next, int expectedStart, int expectedEnd, String expectedKeyword) {
assertEquals(expectedStart, next.getStart());
assertEquals(expectedEnd, next.getEnd());