From 7f31c4477fa49c64bb817d5803350f4a9928f329 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Tue, 9 Jul 2019 11:47:55 +0100 Subject: [PATCH] matchers for keywords --- .../muwire/core/content/KeywordMatcher.groovy | 17 +++++++++++++++++ .../com/muwire/core/content/Match.groovy | 9 +++++++++ .../com/muwire/core/content/Matcher.groovy | 19 +++++++++++++++++++ .../muwire/core/content/RegexMatcher.groovy | 17 +++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 core/src/main/groovy/com/muwire/core/content/KeywordMatcher.groovy create mode 100644 core/src/main/groovy/com/muwire/core/content/Match.groovy create mode 100644 core/src/main/groovy/com/muwire/core/content/Matcher.groovy create mode 100644 core/src/main/groovy/com/muwire/core/content/RegexMatcher.groovy diff --git a/core/src/main/groovy/com/muwire/core/content/KeywordMatcher.groovy b/core/src/main/groovy/com/muwire/core/content/KeywordMatcher.groovy new file mode 100644 index 00000000..d0e30a5b --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/content/KeywordMatcher.groovy @@ -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 + } +} diff --git a/core/src/main/groovy/com/muwire/core/content/Match.groovy b/core/src/main/groovy/com/muwire/core/content/Match.groovy new file mode 100644 index 00000000..68ae9cad --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/content/Match.groovy @@ -0,0 +1,9 @@ +package com.muwire.core.content + +import com.muwire.core.Persona + +class Match { + Persona persona + String [] keywords + long timestamp +} diff --git a/core/src/main/groovy/com/muwire/core/content/Matcher.groovy b/core/src/main/groovy/com/muwire/core/content/Matcher.groovy new file mode 100644 index 00000000..9b324702 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/content/Matcher.groovy @@ -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) + } + } +} diff --git a/core/src/main/groovy/com/muwire/core/content/RegexMatcher.groovy b/core/src/main/groovy/com/muwire/core/content/RegexMatcher.groovy new file mode 100644 index 00000000..b9742f57 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/content/RegexMatcher.groovy @@ -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() + } +}