mirror of https://github.com/zlatinb/muwire
matchers for keywords
parent
6bad67c1bf
commit
7f31c4477f
|
@ -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
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.muwire.core.content
|
||||
|
||||
import com.muwire.core.Persona
|
||||
|
||||
class Match {
|
||||
Persona persona
|
||||
String [] keywords
|
||||
long timestamp
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue