diff --git a/pom.xml b/pom.xml
index 66eee2d..ffb9a0e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
dev.struchkov
autoresponder
- 3.3.0
+ 3.4.0
jar
Abstract Autoresponder
@@ -27,7 +27,7 @@
UTF-8
UTF-8
- 1.0.3
+ 1.2.0
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 d5d92e7..3b312a1 100644
--- a/src/main/java/dev/struchkov/autoresponder/Responder.java
+++ b/src/main/java/dev/struchkov/autoresponder/Responder.java
@@ -1,6 +1,7 @@
package dev.struchkov.autoresponder;
import dev.struchkov.autoresponder.compare.UnitPriorityComparator;
+import dev.struchkov.autoresponder.entity.DeliverableText;
import dev.struchkov.autoresponder.entity.KeyWord;
import dev.struchkov.autoresponder.entity.Unit;
import dev.struchkov.autoresponder.util.Message;
@@ -15,6 +16,8 @@ import java.util.function.Predicate;
import java.util.regex.Pattern;
import static dev.struchkov.autoresponder.util.Parser.splitWords;
+import static dev.struchkov.haiti.utils.Checker.checkNotEmpty;
+import static dev.struchkov.haiti.utils.Checker.checkNotNull;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
import static dev.struchkov.haiti.utils.Inspector.isNotNull;
@@ -28,7 +31,7 @@ public final class Responder {
private static final Logger log = LoggerFactory.getLogger(Responder.class);
/**
- * Компоратор для сортировки Unit-ов
+ * Компаратор для сортировки Unit-ов
*/
private static final UnitPriorityComparator UNIT_PRIORITY_COMPARATOR = new UnitPriorityComparator();
@@ -36,6 +39,10 @@ public final class Responder {
utilityClass();
}
+ public static > Optional nextUnit(String message, Collection nextUnits) {
+ return nextUnit(() -> message, nextUnits);
+ }
+
/**
* Выбирает, какой {@link Unit} будет отдан для обработки
*
@@ -43,31 +50,36 @@ public final class Responder {
* @param message Запрос пользователя - текстовое сообщение
* @return Юнит, который нуждается в обработке в соответствии с запросом пользователя
*/
- public static > Optional nextUnit(String message, Collection nextUnits) {
+ public static > Optional nextUnit(M message, Collection nextUnits) {
isNotNull(nextUnits);
final Set searchUnit = new HashSet<>();
- if (message != null && nextUnits != null) {
+ if (checkNotEmpty(nextUnits)) {
for (U unit : nextUnits) {
- final Set unitPhrases = unit.getPhrases();
- if (
- unitPhrases != null
- && !unitPhrases.isEmpty()
- && unitPhrases.contains(message)
- ) {
- searchUnit.add(unit);
+ final String text = message.getText();
+ if (checkNotNull(text)) {
+ final Set unitPhrases = unit.getPhrases();
+ if (checkNotEmpty(unitPhrases) && unitPhrases.contains(text)) {
+ searchUnit.add(unit);
+ }
+
+ final Set patterns = unit.getTriggerPatterns();
+ if (checkNotEmpty(patterns)) {
+ for (Pattern pattern : patterns) {
+ if (patternReg(pattern, text)) {
+ searchUnit.add(unit);
+ break;
+ }
+ }
+ }
+
+ if (percentageMatch(unit, text) >= unit.getMatchThreshold()) {
+ searchUnit.add(unit);
+ }
}
- if (unit.getPattern() != null && patternReg(unit.getPattern(), message)) {
- searchUnit.add(unit);
- }
-
- if (percentageMatch(unit, message) >= unit.getMatchThreshold()) {
- searchUnit.add(unit);
- }
-
- final Predicate triggerCheck = unit.getTriggerCheck();
- if (triggerCheck != null && triggerCheck.test(message)) {
+ final Predicate triggerCheck = unit.getTriggerCheck();
+ if (checkNotNull(triggerCheck) && triggerCheck.test(message)) {
searchUnit.add(unit);
}
}
@@ -84,25 +96,25 @@ public final class Responder {
return searchUnit.stream().max(UNIT_PRIORITY_COMPARATOR);
}
- private static > boolean isNotTrigger(U nextUnit) {
+ private static > boolean isNotTrigger(U nextUnit) {
return isNotPattern(nextUnit) && isNotKeyWords(nextUnit) && isNotPhrase(nextUnit) && isNotCheck(nextUnit);
}
- private static > boolean isNotCheck(U unit) {
+ private static > boolean isNotCheck(U unit) {
return unit.getTriggerCheck() == null;
}
- private static > boolean isNotPhrase(U unit) {
+ private static > boolean isNotPhrase(U unit) {
final Set phrases = unit.getPhrases();
return phrases == null || phrases.isEmpty();
}
- private static > boolean isNotPattern(U unit) {
- return unit.getPattern() == null;
+ private static > boolean isNotPattern(U unit) {
+ return unit.getTriggerPatterns() == null || unit.getTriggerPatterns().isEmpty();
}
- private static > boolean isNotKeyWords(U unit) {
- final Set keyWords = unit.getKeyWords();
+ private static > boolean isNotKeyWords(U unit) {
+ final Set keyWords = unit.getTriggerWords();
return keyWords == null || keyWords.isEmpty();
}
@@ -111,9 +123,9 @@ public final class Responder {
return message.matches(pattern.pattern());
}
- private static > double percentageMatch(U unit, String message) {
- final Set unitKeyWords = unit.getKeyWords();
- if (unitKeyWords != null && !unitKeyWords.isEmpty()) {
+ private static > double percentageMatch(U unit, String message) {
+ final Set unitKeyWords = unit.getTriggerWords();
+ if (checkNotEmpty(unitKeyWords)) {
final Set messageWords = splitWords(message);
final Set intersection = getIntersection(unitKeyWords, messageWords);
final double intersectionWeight = getIntersectionWeight(intersection);
diff --git a/src/main/java/dev/struchkov/autoresponder/compare/UnitPriorityComparator.java b/src/main/java/dev/struchkov/autoresponder/compare/UnitPriorityComparator.java
index 80c7aba..f07cd23 100644
--- a/src/main/java/dev/struchkov/autoresponder/compare/UnitPriorityComparator.java
+++ b/src/main/java/dev/struchkov/autoresponder/compare/UnitPriorityComparator.java
@@ -9,7 +9,7 @@ import java.util.Comparator;
*
* @author upagge [07/07/2019]
*/
-public class UnitPriorityComparator implements Comparator> {
+public class UnitPriorityComparator implements Comparator> {
@Override
public int compare(Unit unit1, Unit unit2) {
diff --git a/src/main/java/dev/struchkov/autoresponder/entity/DeliverableText.java b/src/main/java/dev/struchkov/autoresponder/entity/DeliverableText.java
new file mode 100644
index 0000000..fea1b53
--- /dev/null
+++ b/src/main/java/dev/struchkov/autoresponder/entity/DeliverableText.java
@@ -0,0 +1,8 @@
+package dev.struchkov.autoresponder.entity;
+
+@FunctionalInterface
+public interface DeliverableText {
+
+ String getText();
+
+}
diff --git a/src/main/java/dev/struchkov/autoresponder/entity/Unit.java b/src/main/java/dev/struchkov/autoresponder/entity/Unit.java
index f692ce3..242897c 100644
--- a/src/main/java/dev/struchkov/autoresponder/entity/Unit.java
+++ b/src/main/java/dev/struchkov/autoresponder/entity/Unit.java
@@ -11,12 +11,12 @@ import java.util.regex.Pattern;
*
* @author upagge [07/07/2019]
*/
-public abstract class Unit> {
+public abstract class Unit, M extends DeliverableText> {
/**
* Ключевые слова.
*/
- protected Set keyWords;
+ protected Set triggerWords;
/**
* Точная фраза.
@@ -24,11 +24,14 @@ public abstract class Unit> {
protected Set phrases;
/**
- * Регулярное выражение.
+ * Триггеры на срабатывание юнита по регулярному выражению.
*/
- protected Pattern pattern;
+ protected Set triggerPatterns;
- protected Predicate triggerCheck;
+ /**
+ * Пользовательский триггер
+ */
+ protected Predicate triggerCheck;
/**
* Значение минимального отношения количества найденных ключевых слов, к количеству ключевых слов Unit-а.
@@ -46,29 +49,29 @@ public abstract class Unit> {
protected Set nextUnits;
protected Unit(
- Set keyWords,
+ Set triggerWords,
Set phrases,
- Predicate triggerCheck,
- Pattern pattern,
+ Predicate triggerCheck,
+ Set triggerPatterns,
Integer matchThreshold,
Integer priority,
Set nextUnits
) {
- this.keyWords = keyWords;
+ this.triggerWords = triggerWords;
this.phrases = phrases;
this.triggerCheck = triggerCheck;
- this.pattern = pattern;
+ this.triggerPatterns = triggerPatterns;
this.matchThreshold = matchThreshold == null ? 10 : matchThreshold;
this.priority = priority == null ? 10 : priority;
this.nextUnits = nextUnits;
}
- public Set getKeyWords() {
- return keyWords;
+ public Set getTriggerWords() {
+ return triggerWords;
}
- public void setKeyWords(Set keyWords) {
- this.keyWords = keyWords;
+ public void setTriggerWords(Set triggerWords) {
+ this.triggerWords = triggerWords;
}
public Set getPhrases() {
@@ -83,12 +86,16 @@ public abstract class Unit> {
this.phrases.addAll(phrases);
}
- public Pattern getPattern() {
- return pattern;
+ public void setPhrases(Set phrases) {
+ this.phrases = phrases;
}
- public void setPattern(Pattern pattern) {
- this.pattern = pattern;
+ public Set getTriggerPatterns() {
+ return triggerPatterns;
+ }
+
+ public void setTriggerPatterns(Set triggerPatterns) {
+ this.triggerPatterns = triggerPatterns;
}
public Integer getMatchThreshold() {
@@ -115,11 +122,11 @@ public abstract class Unit> {
this.nextUnits = nextUnits;
}
- public Predicate getTriggerCheck() {
+ public Predicate getTriggerCheck() {
return triggerCheck;
}
- public void setTriggerCheck(Predicate triggerCheck) {
+ public void setTriggerCheck(Predicate triggerCheck) {
this.triggerCheck = triggerCheck;
}
@@ -127,21 +134,20 @@ public abstract class Unit> {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
- Unit> unit = (Unit>) o;
- return Objects.equals(keyWords, unit.keyWords) && Objects.equals(phrases, unit.phrases) && Objects.equals(pattern, unit.pattern) && Objects.equals(matchThreshold, unit.matchThreshold) && Objects.equals(priority, unit.priority);
+ Unit, ?> unit = (Unit, ?>) o;
+ return Objects.equals(triggerWords, unit.triggerWords) && Objects.equals(phrases, unit.phrases) && Objects.equals(matchThreshold, unit.matchThreshold) && Objects.equals(priority, unit.priority);
}
@Override
public int hashCode() {
- return Objects.hash(keyWords, phrases, pattern, matchThreshold, priority);
+ return Objects.hash(triggerWords, phrases, matchThreshold, priority);
}
@Override
public String toString() {
return "Unit{" +
- "keyWords=" + keyWords +
+ "keyWords=" + triggerWords +
", phrases='" + phrases + '\'' +
- ", pattern=" + pattern +
", matchThreshold=" + matchThreshold +
", priority=" + priority +
'}';