matchers for keywords

pull/11/head
Zlatin Balevsky 2019-07-09 11:47:55 +01:00
parent 6bad67c1bf
commit 7f31c4477f
4 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package com.muwire.core.content
class KeywordMatcher extends Matcher {
private final String keyword
KeywordMatcher(String keyword) {
this.keyword = keyword
}
@Override
protected boolean match(String[] searchTerms) {
searchTerms.each {
if (keyword == it)
return true
}
false
}
}

View File

@ -0,0 +1,9 @@
package com.muwire.core.content
import com.muwire.core.Persona
class Match {
Persona persona
String [] keywords
long timestamp
}

View File

@ -0,0 +1,19 @@
package com.muwire.core.content
import com.muwire.core.search.QueryEvent
abstract class Matcher {
final Match [] matches = Collections.synchronizedList(new ArrayList<>())
protected abstract boolean match(String []searchTerms);
public void process(QueryEvent qe) {
def terms = qe.searchEvent.searchTerms
if (terms == null)
return
if (match(terms)) {
long now = System.currentTimeMillis()
matches << new Match(persona : qe.originator, keywords : terms, timestamp : now)
}
}
}

View File

@ -0,0 +1,17 @@
package com.muwire.core.content
import java.util.regex.Pattern
import java.util.stream.Collectors
class RegexMatcher extends Matcher {
private final Pattern pattern
RegexMatcher(String pattern) {
this.pattern = Pattern.compile(pattern)
}
@Override
protected boolean match(String[] keywords) {
String combined = keywords.join(" ")
return pattern.matcher(combined).find()
}
}