From 7bc6923ed40f582cad65c7ad8c864dde1e9d5ce9 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sun, 14 Jul 2019 14:12:45 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=81=D0=BF?= =?UTF-8?q?=D0=BE=D1=81=D0=BE=D0=B1=20=D0=B2=D1=8B=D0=B1=D0=BE=D1=80=D0=B0?= =?UTF-8?q?=20=D1=8E=D0=BD=D0=B8=D1=82=D0=B0,=20=D1=81=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BC=D0=BE=D1=89=D1=8C=D1=8E=20=D1=82=D0=BE=D1=87=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D1=81=D0=BE=D0=B2=D0=BF=D0=B0=D0=B4=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D0=BE=20=D1=84=D1=80=D0=B0=D0=B7=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/sadtech/autoresponder/AutoResponder.java | 15 ++++++++++++--- .../org/sadtech/autoresponder/entity/Unit.java | 6 +++++- .../org/sadtech/autoresponder/test/TestUnit.java | 8 +++++--- 3 files changed, 22 insertions(+), 7 deletions(-) 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; } + }