Изменил api юнита
This commit is contained in:
parent
c72d9a5298
commit
e71083fa91
4
pom.xml
4
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>dev.struchkov</groupId>
|
||||
<artifactId>autoresponder</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<version>3.4.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.3</haiti.utils.ver>
|
||||
<haiti.utils.ver>1.2.0</haiti.utils.ver>
|
||||
|
||||
<junit.ver>5.8.2</junit.ver>
|
||||
<slf4j.ver>1.7.36</slf4j.ver>
|
||||
|
@ -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 <U extends Unit<U, DeliverableText>> Optional<U> nextUnit(String message, Collection<U> nextUnits) {
|
||||
return nextUnit(() -> message, nextUnits);
|
||||
}
|
||||
|
||||
/**
|
||||
* Выбирает, какой {@link Unit} будет отдан для обработки
|
||||
*
|
||||
@ -43,31 +50,36 @@ public final class Responder {
|
||||
* @param message Запрос пользователя - текстовое сообщение
|
||||
* @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);
|
||||
final Set<U> searchUnit = new HashSet<>();
|
||||
|
||||
if (message != null && nextUnits != null) {
|
||||
if (checkNotEmpty(nextUnits)) {
|
||||
for (U unit : nextUnits) {
|
||||
final Set<String> unitPhrases = unit.getPhrases();
|
||||
if (
|
||||
unitPhrases != null
|
||||
&& !unitPhrases.isEmpty()
|
||||
&& unitPhrases.contains(message)
|
||||
) {
|
||||
searchUnit.add(unit);
|
||||
final String text = message.getText();
|
||||
if (checkNotNull(text)) {
|
||||
final Set<String> unitPhrases = unit.getPhrases();
|
||||
if (checkNotEmpty(unitPhrases) && unitPhrases.contains(text)) {
|
||||
searchUnit.add(unit);
|
||||
}
|
||||
|
||||
final Set<Pattern> 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<String> triggerCheck = unit.getTriggerCheck();
|
||||
if (triggerCheck != null && triggerCheck.test(message)) {
|
||||
final Predicate<M> 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 <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);
|
||||
}
|
||||
|
||||
private static <U extends Unit<U>> boolean isNotCheck(U unit) {
|
||||
private static <U extends Unit<U, ?>> boolean isNotCheck(U unit) {
|
||||
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();
|
||||
return phrases == null || phrases.isEmpty();
|
||||
}
|
||||
|
||||
private static <U extends Unit<U>> boolean isNotPattern(U unit) {
|
||||
return unit.getPattern() == null;
|
||||
private static <U extends Unit<U, ?>> boolean isNotPattern(U unit) {
|
||||
return unit.getTriggerPatterns() == null || unit.getTriggerPatterns().isEmpty();
|
||||
}
|
||||
|
||||
private static <U extends Unit<U>> boolean isNotKeyWords(U unit) {
|
||||
final Set<KeyWord> keyWords = unit.getKeyWords();
|
||||
private static <U extends Unit<U, ?>> boolean isNotKeyWords(U unit) {
|
||||
final Set<KeyWord> keyWords = unit.getTriggerWords();
|
||||
return keyWords == null || keyWords.isEmpty();
|
||||
}
|
||||
|
||||
@ -111,9 +123,9 @@ public final class Responder {
|
||||
return message.matches(pattern.pattern());
|
||||
}
|
||||
|
||||
private static <U extends Unit<U>> double percentageMatch(U unit, String message) {
|
||||
final Set<KeyWord> unitKeyWords = unit.getKeyWords();
|
||||
if (unitKeyWords != null && !unitKeyWords.isEmpty()) {
|
||||
private static <U extends Unit<U, ?>> double percentageMatch(U unit, String message) {
|
||||
final Set<KeyWord> unitKeyWords = unit.getTriggerWords();
|
||||
if (checkNotEmpty(unitKeyWords)) {
|
||||
final Set<String> messageWords = splitWords(message);
|
||||
final Set<KeyWord> intersection = getIntersection(unitKeyWords, messageWords);
|
||||
final double intersectionWeight = getIntersectionWeight(intersection);
|
||||
|
@ -9,7 +9,7 @@ import java.util.Comparator;
|
||||
*
|
||||
* @author upagge [07/07/2019]
|
||||
*/
|
||||
public class UnitPriorityComparator implements Comparator<Unit<?>> {
|
||||
public class UnitPriorityComparator implements Comparator<Unit<?, ?>> {
|
||||
|
||||
@Override
|
||||
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]
|
||||
*/
|
||||
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 Pattern pattern;
|
||||
protected Set<Pattern> triggerPatterns;
|
||||
|
||||
protected Predicate<String> triggerCheck;
|
||||
/**
|
||||
* Пользовательский триггер
|
||||
*/
|
||||
protected Predicate<M> triggerCheck;
|
||||
|
||||
/**
|
||||
* Значение минимального отношения количества найденных ключевых слов, к количеству ключевых слов Unit-а.
|
||||
@ -46,29 +49,29 @@ public abstract class Unit<U extends Unit<U>> {
|
||||
protected Set<U> nextUnits;
|
||||
|
||||
protected Unit(
|
||||
Set<KeyWord> keyWords,
|
||||
Set<KeyWord> triggerWords,
|
||||
Set<String> phrases,
|
||||
Predicate<String> triggerCheck,
|
||||
Pattern pattern,
|
||||
Predicate<M> triggerCheck,
|
||||
Set<Pattern> triggerPatterns,
|
||||
Integer matchThreshold,
|
||||
Integer priority,
|
||||
Set<U> 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<KeyWord> getKeyWords() {
|
||||
return keyWords;
|
||||
public Set<KeyWord> getTriggerWords() {
|
||||
return triggerWords;
|
||||
}
|
||||
|
||||
public void setKeyWords(Set<KeyWord> keyWords) {
|
||||
this.keyWords = keyWords;
|
||||
public void setTriggerWords(Set<KeyWord> triggerWords) {
|
||||
this.triggerWords = triggerWords;
|
||||
}
|
||||
|
||||
public Set<String> getPhrases() {
|
||||
@ -83,12 +86,16 @@ public abstract class Unit<U extends Unit<U>> {
|
||||
this.phrases.addAll(phrases);
|
||||
}
|
||||
|
||||
public Pattern getPattern() {
|
||||
return pattern;
|
||||
public void setPhrases(Set<String> phrases) {
|
||||
this.phrases = phrases;
|
||||
}
|
||||
|
||||
public void setPattern(Pattern pattern) {
|
||||
this.pattern = pattern;
|
||||
public Set<Pattern> getTriggerPatterns() {
|
||||
return triggerPatterns;
|
||||
}
|
||||
|
||||
public void setTriggerPatterns(Set<Pattern> triggerPatterns) {
|
||||
this.triggerPatterns = triggerPatterns;
|
||||
}
|
||||
|
||||
public Integer getMatchThreshold() {
|
||||
@ -115,11 +122,11 @@ public abstract class Unit<U extends Unit<U>> {
|
||||
this.nextUnits = nextUnits;
|
||||
}
|
||||
|
||||
public Predicate<String> getTriggerCheck() {
|
||||
public Predicate<M> getTriggerCheck() {
|
||||
return triggerCheck;
|
||||
}
|
||||
|
||||
public void setTriggerCheck(Predicate<String> triggerCheck) {
|
||||
public void setTriggerCheck(Predicate<M> triggerCheck) {
|
||||
this.triggerCheck = triggerCheck;
|
||||
}
|
||||
|
||||
@ -127,21 +134,20 @@ public abstract class Unit<U extends Unit<U>> {
|
||||
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 +
|
||||
'}';
|
||||
|
Loading…
Reference in New Issue
Block a user