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