Добавил новый триггер для юнитов в виде пользовательской лямбды
This commit is contained in:
parent
1963b5ba27
commit
c72d9a5298
4
pom.xml
4
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>dev.struchkov</groupId>
|
||||
<artifactId>autoresponder</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<version>3.3.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Abstract Autoresponder</name>
|
||||
@ -27,7 +27,7 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<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>
|
||||
<slf4j.ver>1.7.36</slf4j.ver>
|
||||
|
@ -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<String> 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 <U extends Unit<U>> boolean isNotPhrase(U nextUnit) {
|
||||
final Set<String> phrases = nextUnit.getPhrases();
|
||||
private static <U extends Unit<U>> boolean isNotTrigger(U nextUnit) {
|
||||
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();
|
||||
}
|
||||
|
||||
private static <U extends Unit<U>> boolean isNotPattern(U nextUnit) {
|
||||
return nextUnit.getPattern() == null;
|
||||
private static <U extends Unit<U>> boolean isNotPattern(U unit) {
|
||||
return unit.getPattern() == null;
|
||||
}
|
||||
|
||||
private static <U extends Unit<U>> boolean isNotKeyWords(U nextUnit) {
|
||||
final Set<KeyWord> keyWords = nextUnit.getKeyWords();
|
||||
private static <U extends Unit<U>> boolean isNotKeyWords(U unit) {
|
||||
final Set<KeyWord> 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;
|
||||
|
@ -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<U extends Unit<U>> {
|
||||
*/
|
||||
protected Pattern pattern;
|
||||
|
||||
protected Predicate<String> triggerCheck;
|
||||
|
||||
/**
|
||||
* Значение минимального отношения количества найденых ключевых слов, к количеству ключевых слов Unit-а.
|
||||
* Значение минимального отношения количества найденных ключевых слов, к количеству ключевых слов Unit-а.
|
||||
*/
|
||||
protected Integer matchThreshold;
|
||||
|
||||
@ -46,6 +48,7 @@ public abstract class Unit<U extends Unit<U>> {
|
||||
protected Unit(
|
||||
Set<KeyWord> keyWords,
|
||||
Set<String> phrases,
|
||||
Predicate<String> triggerCheck,
|
||||
Pattern pattern,
|
||||
Integer matchThreshold,
|
||||
Integer priority,
|
||||
@ -53,6 +56,7 @@ public abstract class Unit<U extends Unit<U>> {
|
||||
) {
|
||||
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<U extends Unit<U>> {
|
||||
}
|
||||
|
||||
public void addPhrases(Collection<String> phrases) {
|
||||
phrases.addAll(phrases);
|
||||
this.phrases.addAll(phrases);
|
||||
}
|
||||
|
||||
public Pattern getPattern() {
|
||||
@ -111,6 +115,14 @@ public abstract class Unit<U extends Unit<U>> {
|
||||
this.nextUnits = nextUnits;
|
||||
}
|
||||
|
||||
public Predicate<String> getTriggerCheck() {
|
||||
return triggerCheck;
|
||||
}
|
||||
|
||||
public void setTriggerCheck(Predicate<String> triggerCheck) {
|
||||
this.triggerCheck = triggerCheck;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
Loading…
Reference in New Issue
Block a user