remove functionality

pull/4/head
Zlatin Balevsky 2018-07-21 09:27:29 +01:00
parent 01617f876d
commit dd68d92337
2 changed files with 36 additions and 2 deletions

View File

@ -6,8 +6,7 @@ class SearchIndex {
final Map<String, Set<String>> keywords = new HashMap<>()
void add(String string) {
String name = string.replaceAll("\\."," ")
String [] split = name.split(" ")
String [] split = split(string)
split.each {
Set<String> existing = keywords.get(it)
if (existing == null) {
@ -18,6 +17,24 @@ class SearchIndex {
}
}
void remove(String string) {
String [] split = split(string)
split.each {
Set<String> existing = keywords.get it
if (existing != null) {
existing.remove(string)
if (existing.isEmpty()) {
keywords.remove(it)
}
}
}
}
private static String[] split(String source) {
source = source.replaceAll("[\\.,_-]", " ")
source.split(" ")
}
String[] search(List<String> terms) {
Set<String> rv = null;

View File

@ -55,4 +55,21 @@ class SearchIndexTest {
assert found.size() == 0
}
@Test
void testRemove() {
initIndex(["a b.c"])
index.remove("a b.c")
def found = index.search(["a"])
assert found.size() == 0
}
@Test
void testRemoveOverlap() {
initIndex(["a b.c", "b c.d"])
index.remove("a b.c")
def found = index.search(["b"])
assert found.size() == 1
assert found.contains("b c.d")
}
}