Добавил новый триггер для юнитов в виде пользовательской лямбды

This commit is contained in:
Struchkov Mark 2022-07-21 00:46:03 +03:00
parent 1963b5ba27
commit c72d9a5298
3 changed files with 39 additions and 13 deletions

View File

@ -6,7 +6,7 @@
<groupId>dev.struchkov</groupId> <groupId>dev.struchkov</groupId>
<artifactId>autoresponder</artifactId> <artifactId>autoresponder</artifactId>
<version>3.2.0</version> <version>3.3.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Abstract Autoresponder</name> <name>Abstract Autoresponder</name>
@ -27,7 +27,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<haiti.utils.ver>1.0.2</haiti.utils.ver> <haiti.utils.ver>1.0.3</haiti.utils.ver>
<junit.ver>5.8.2</junit.ver> <junit.ver>5.8.2</junit.ver>
<slf4j.ver>1.7.36</slf4j.ver> <slf4j.ver>1.7.36</slf4j.ver>

View File

@ -11,6 +11,7 @@ import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static dev.struchkov.autoresponder.util.Parser.splitWords; import static dev.struchkov.autoresponder.util.Parser.splitWords;
@ -64,12 +65,17 @@ public final class Responder {
if (percentageMatch(unit, message) >= unit.getMatchThreshold()) { if (percentageMatch(unit, message) >= unit.getMatchThreshold()) {
searchUnit.add(unit); searchUnit.add(unit);
} }
final Predicate<String> triggerCheck = unit.getTriggerCheck();
if (triggerCheck != null && triggerCheck.test(message)) {
searchUnit.add(unit);
}
} }
} }
if (searchUnit.isEmpty()) { if (searchUnit.isEmpty()) {
for (U nextUnit : nextUnits) { for (U nextUnit : nextUnits) {
if (isNotPattern(nextUnit) && isNotKeyWords(nextUnit) && isNotPhrase(nextUnit)) { if (isNotTrigger(nextUnit)) {
searchUnit.add(nextUnit); searchUnit.add(nextUnit);
} }
} }
@ -78,17 +84,25 @@ public final class Responder {
return searchUnit.stream().max(UNIT_PRIORITY_COMPARATOR); return searchUnit.stream().max(UNIT_PRIORITY_COMPARATOR);
} }
private static <U extends Unit<U>> boolean isNotPhrase(U nextUnit) { private static <U extends Unit<U>> boolean isNotTrigger(U nextUnit) {
final Set<String> phrases = nextUnit.getPhrases(); return isNotPattern(nextUnit) && isNotKeyWords(nextUnit) && isNotPhrase(nextUnit) && isNotCheck(nextUnit);
}
private static <U extends Unit<U>> boolean isNotCheck(U unit) {
return unit.getTriggerCheck() == null;
}
private static <U extends Unit<U>> boolean isNotPhrase(U unit) {
final Set<String> phrases = unit.getPhrases();
return phrases == null || phrases.isEmpty(); return phrases == null || phrases.isEmpty();
} }
private static <U extends Unit<U>> boolean isNotPattern(U nextUnit) { private static <U extends Unit<U>> boolean isNotPattern(U unit) {
return nextUnit.getPattern() == null; return unit.getPattern() == null;
} }
private static <U extends Unit<U>> boolean isNotKeyWords(U nextUnit) { private static <U extends Unit<U>> boolean isNotKeyWords(U unit) {
final Set<KeyWord> keyWords = nextUnit.getKeyWords(); final Set<KeyWord> keyWords = unit.getKeyWords();
return keyWords == null || keyWords.isEmpty(); return keyWords == null || keyWords.isEmpty();
} }
@ -106,7 +120,7 @@ public final class Responder {
log.debug(Message.UNIT_KEYWORDS, unitKeyWords, unitKeyWords.size()); log.debug(Message.UNIT_KEYWORDS, unitKeyWords, unitKeyWords.size());
log.debug(Message.USER_MESSAGE_KEYWORDS, messageWords); log.debug(Message.USER_MESSAGE_KEYWORDS, messageWords);
log.debug(Message.INTERSECTION, intersection, intersectionWeight); 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; return (double) intersection.size() / (double) unitKeyWords.size() * 100.0;
} else { } else {
return 0.0; return 0.0;

View File

@ -1,9 +1,9 @@
package dev.struchkov.autoresponder.entity; package dev.struchkov.autoresponder.entity;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -28,8 +28,10 @@ public abstract class Unit<U extends Unit<U>> {
*/ */
protected Pattern pattern; protected Pattern pattern;
protected Predicate<String> triggerCheck;
/** /**
* Значение минимального отношения количества найденых ключевых слов, к количеству ключевых слов Unit-а. * Значение минимального отношения количества найденных ключевых слов, к количеству ключевых слов Unit-а.
*/ */
protected Integer matchThreshold; protected Integer matchThreshold;
@ -46,6 +48,7 @@ public abstract class Unit<U extends Unit<U>> {
protected Unit( protected Unit(
Set<KeyWord> keyWords, Set<KeyWord> keyWords,
Set<String> phrases, Set<String> phrases,
Predicate<String> triggerCheck,
Pattern pattern, Pattern pattern,
Integer matchThreshold, Integer matchThreshold,
Integer priority, Integer priority,
@ -53,6 +56,7 @@ public abstract class Unit<U extends Unit<U>> {
) { ) {
this.keyWords = keyWords; this.keyWords = keyWords;
this.phrases = phrases; this.phrases = phrases;
this.triggerCheck = triggerCheck;
this.pattern = pattern; this.pattern = pattern;
this.matchThreshold = matchThreshold == null ? 10 : matchThreshold; this.matchThreshold = matchThreshold == null ? 10 : matchThreshold;
this.priority = priority == null ? 10 : priority; this.priority = priority == null ? 10 : priority;
@ -76,7 +80,7 @@ public abstract class Unit<U extends Unit<U>> {
} }
public void addPhrases(Collection<String> phrases) { public void addPhrases(Collection<String> phrases) {
phrases.addAll(phrases); this.phrases.addAll(phrases);
} }
public Pattern getPattern() { public Pattern getPattern() {
@ -111,6 +115,14 @@ public abstract class Unit<U extends Unit<U>> {
this.nextUnits = nextUnits; this.nextUnits = nextUnits;
} }
public Predicate<String> getTriggerCheck() {
return triggerCheck;
}
public void setTriggerCheck(Predicate<String> triggerCheck) {
this.triggerCheck = triggerCheck;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;