From c72d9a5298715373412c538629569bf6308548ae Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Thu, 21 Jul 2022 00:46:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=82=D1=80=D0=B8=D0=B3=D0=B3?= =?UTF-8?q?=D0=B5=D1=80=20=D0=B4=D0=BB=D1=8F=20=D1=8E=D0=BD=D0=B8=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=B2=20=D0=B2=D0=B8=D0=B4=D0=B5=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8C?= =?UTF-8?q?=D1=81=D0=BA=D0=BE=D0=B9=20=D0=BB=D1=8F=D0=BC=D0=B1=D0=B4=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +-- .../struchkov/autoresponder/Responder.java | 30 ++++++++++++++----- .../struchkov/autoresponder/entity/Unit.java | 18 +++++++++-- 3 files changed, 39 insertions(+), 13 deletions(-) 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;