diff --git a/pom.xml b/pom.xml index cd19900..66eee2d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov autoresponder - 3.2.0 + 3.3.0 jar Abstract Autoresponder @@ -27,7 +27,7 @@ UTF-8 UTF-8 - 1.0.2 + 1.0.3 5.8.2 1.7.36 diff --git a/src/main/java/dev/struchkov/autoresponder/Responder.java b/src/main/java/dev/struchkov/autoresponder/Responder.java index 591a20d..d5d92e7 100644 --- a/src/main/java/dev/struchkov/autoresponder/Responder.java +++ b/src/main/java/dev/struchkov/autoresponder/Responder.java @@ -11,6 +11,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.Optional; import java.util.Set; +import java.util.function.Predicate; import java.util.regex.Pattern; import static dev.struchkov.autoresponder.util.Parser.splitWords; @@ -64,12 +65,17 @@ public final class Responder { if (percentageMatch(unit, message) >= unit.getMatchThreshold()) { searchUnit.add(unit); } + + final Predicate triggerCheck = unit.getTriggerCheck(); + if (triggerCheck != null && triggerCheck.test(message)) { + searchUnit.add(unit); + } } } if (searchUnit.isEmpty()) { for (U nextUnit : nextUnits) { - if (isNotPattern(nextUnit) && isNotKeyWords(nextUnit) && isNotPhrase(nextUnit)) { + if (isNotTrigger(nextUnit)) { searchUnit.add(nextUnit); } } @@ -78,17 +84,25 @@ public final class Responder { return searchUnit.stream().max(UNIT_PRIORITY_COMPARATOR); } - private static > boolean isNotPhrase(U nextUnit) { - final Set phrases = nextUnit.getPhrases(); + private static > boolean isNotTrigger(U nextUnit) { + return isNotPattern(nextUnit) && isNotKeyWords(nextUnit) && isNotPhrase(nextUnit) && isNotCheck(nextUnit); + } + + private static > boolean isNotCheck(U unit) { + return unit.getTriggerCheck() == null; + } + + private static > boolean isNotPhrase(U unit) { + final Set phrases = unit.getPhrases(); return phrases == null || phrases.isEmpty(); } - private static > boolean isNotPattern(U nextUnit) { - return nextUnit.getPattern() == null; + private static > boolean isNotPattern(U unit) { + return unit.getPattern() == null; } - private static > boolean isNotKeyWords(U nextUnit) { - final Set keyWords = nextUnit.getKeyWords(); + private static > boolean isNotKeyWords(U unit) { + final Set keyWords = unit.getKeyWords(); return keyWords == null || keyWords.isEmpty(); } @@ -106,7 +120,7 @@ public final class Responder { log.debug(Message.UNIT_KEYWORDS, unitKeyWords, unitKeyWords.size()); log.debug(Message.USER_MESSAGE_KEYWORDS, messageWords); log.debug(Message.INTERSECTION, intersection, intersectionWeight); - log.debug(Message.CROSSING_PERCENTAGE, intersectionWeight / (double) unitKeyWords.size() * 100.0, unit.getMatchThreshold()); + log.debug(Message.CROSSING_PERCENTAGE, intersectionWeight / unitKeyWords.size() * 100.0, unit.getMatchThreshold()); return (double) intersection.size() / (double) unitKeyWords.size() * 100.0; } else { return 0.0; diff --git a/src/main/java/dev/struchkov/autoresponder/entity/Unit.java b/src/main/java/dev/struchkov/autoresponder/entity/Unit.java index 940ee47..f692ce3 100644 --- a/src/main/java/dev/struchkov/autoresponder/entity/Unit.java +++ b/src/main/java/dev/struchkov/autoresponder/entity/Unit.java @@ -1,9 +1,9 @@ package dev.struchkov.autoresponder.entity; import java.util.Collection; -import java.util.HashSet; import java.util.Objects; import java.util.Set; +import java.util.function.Predicate; import java.util.regex.Pattern; /** @@ -28,8 +28,10 @@ public abstract class Unit> { */ protected Pattern pattern; + protected Predicate triggerCheck; + /** - * Значение минимального отношения количества найденых ключевых слов, к количеству ключевых слов Unit-а. + * Значение минимального отношения количества найденных ключевых слов, к количеству ключевых слов Unit-а. */ protected Integer matchThreshold; @@ -46,6 +48,7 @@ public abstract class Unit> { protected Unit( Set keyWords, Set phrases, + Predicate triggerCheck, Pattern pattern, Integer matchThreshold, Integer priority, @@ -53,6 +56,7 @@ public abstract class Unit> { ) { this.keyWords = keyWords; this.phrases = phrases; + this.triggerCheck = triggerCheck; this.pattern = pattern; this.matchThreshold = matchThreshold == null ? 10 : matchThreshold; this.priority = priority == null ? 10 : priority; @@ -76,7 +80,7 @@ public abstract class Unit> { } public void addPhrases(Collection phrases) { - phrases.addAll(phrases); + this.phrases.addAll(phrases); } public Pattern getPattern() { @@ -111,6 +115,14 @@ public abstract class Unit> { this.nextUnits = nextUnits; } + public Predicate getTriggerCheck() { + return triggerCheck; + } + + public void setTriggerCheck(Predicate triggerCheck) { + this.triggerCheck = triggerCheck; + } + @Override public boolean equals(Object o) { if (this == o) return true;