diff --git a/src/main/java/org/sadtech/autoresponder/AutoResponder.java b/src/main/java/org/sadtech/autoresponder/AutoResponder.java index a093ba7..792c722 100644 --- a/src/main/java/org/sadtech/autoresponder/AutoResponder.java +++ b/src/main/java/org/sadtech/autoresponder/AutoResponder.java @@ -76,6 +76,13 @@ public class AutoResponder { private Unit nextUnit(@NonNull Set nextUnits, String message) { Set searchUnit = new HashSet<>(); + nextUnits.stream() + .filter(nextUnit -> nextUnit.getPhrase() != null + && !nextUnit.getPhrase().isEmpty() + && nextUnit.getPhrase().equalsIgnoreCase(message) + ) + .forEach(searchUnit::add); + nextUnits.stream() .filter(nextUnit -> nextUnit.getPattern() != null && patternReg(nextUnit, message)) .forEach(searchUnit::add); @@ -84,9 +91,11 @@ public class AutoResponder { .filter(nextUnit -> percentageMatch(nextUnit, Parser.parse(message)) >= nextUnit.getMatchThreshold()) .forEach(searchUnit::add); - nextUnits.stream() - .filter(nextUnit -> (nextUnit.getPattern() == null && (nextUnit.getKeyWords() == null || nextUnit.getKeyWords().isEmpty()))) - .forEach(searchUnit::add); + if (searchUnit.isEmpty()) { + nextUnits.stream() + .filter(nextUnit -> (nextUnit.getPattern() == null && (nextUnit.getKeyWords() == null || nextUnit.getKeyWords().isEmpty()))) + .forEach(searchUnit::add); + } return searchUnit.stream().max(UNIT_PRIORITY_COMPARATOR).orElseThrow(NotFoundUnitException::new); } diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index 3f43dfc..cb00fb0 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -24,6 +24,9 @@ public abstract class Unit { @Description("Ключевые слова") protected Set keyWords; + @Description("Точная фраза") + protected String phrase; + @Description("Регулярное выражение") protected Pattern pattern; @@ -36,8 +39,9 @@ public abstract class Unit { @Description("Множество следующих Unit в сценарии") protected Set nextUnits; - protected Unit(Set keyWords, Pattern pattern, Integer matchThreshold, Integer priority, Set nextUnits) { + protected Unit(Set keyWords, String phrase, Pattern pattern, Integer matchThreshold, Integer priority, Set nextUnits) { this.keyWords = keyWords; + this.phrase = phrase; this.pattern = pattern; this.matchThreshold = Optional.ofNullable(matchThreshold).orElse(10); this.priority = Optional.ofNullable(priority).orElse(10); diff --git a/src/test/java/org/sadtech/autoresponder/test/TestUnit.java b/src/test/java/org/sadtech/autoresponder/test/TestUnit.java index 6922a72..79caeae 100644 --- a/src/test/java/org/sadtech/autoresponder/test/TestUnit.java +++ b/src/test/java/org/sadtech/autoresponder/test/TestUnit.java @@ -18,13 +18,15 @@ public class TestUnit extends Unit { private String message; @Builder - public TestUnit(@Singular(value = "keyWord") Set keyWords, + public TestUnit(@Singular Set keyWords, + String phrase, Pattern pattern, Integer matchThreshold, Integer priority, - @Singular(value = "nextUnit") Set nextUnits, + @Singular Set nextUnits, String message) { - super(keyWords, pattern, matchThreshold, priority, nextUnits); + super(keyWords, phrase, pattern, matchThreshold, priority, nextUnits); this.message = message; } + }