From 19f7e4bb71c9f5911eb63044807078e7a2dccf48 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 4 May 2019 14:43:31 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=D0=97=D0=B0=D0=BC=D0=B5=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 10 +++++----- .../org/sadtech/autoresponder/Autoresponder.java | 7 ++++--- .../org/sadtech/autoresponder/entity/Unit.java | 15 +++++---------- .../service/UnitPointerServiceImpl.java | 5 +++-- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index 158fbb0..92b800f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.autoresponder autoresponder - 1.5.0-RELEASE + 1.5.1-SNAPSHOT jar @@ -23,14 +23,14 @@ - 1.2.17 + 1.7.26 - log4j - log4j - ${log4j} + org.slf4j + slf4j-api + ${slf4j.ver} diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index 1dc1ace..f95d38d 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -1,11 +1,12 @@ package org.sadtech.autoresponder; -import org.apache.log4j.Logger; import org.sadtech.autoresponder.compare.UnitPriorityComparator; -import org.sadtech.autoresponder.entity.UnitPointer; import org.sadtech.autoresponder.entity.Unit; +import org.sadtech.autoresponder.entity.UnitPointer; import org.sadtech.autoresponder.service.UnitPointerService; import org.sadtech.autoresponder.util.Parser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.HashSet; import java.util.Optional; @@ -15,7 +16,7 @@ import java.util.regex.Pattern; public class Autoresponder { - private static final Logger log = Logger.getLogger(Autoresponder.class); + private static final Logger log = LoggerFactory.getLogger(Autoresponder.class); private Set menuUnits; private UnitPointerService unitPointerService; diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index 66e01b0..1a160e6 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -9,21 +9,16 @@ public abstract class Unit { private Set keyWords; private Pattern pattern; - private Integer matchThreshold; - private Integer priority; + private Integer matchThreshold = 10; + private Integer priority = 10; private Set nextUnits; public Unit() { - priority = 10; - matchThreshold = 10; + } - public Unit(Set keyWords, Pattern pattern, Integer matchThreshold, Integer priority, Set nextUnits) { - this.keyWords = keyWords; - this.pattern = pattern; - this.matchThreshold = matchThreshold; - this.priority = priority; - this.nextUnits = nextUnits; + public Unit(Unit... nextUnit) { + nextUnits = new HashSet(nextUnits); } public void setKeyWord(String keyWord) { diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java index ca6f9ad..c9e42f3 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java @@ -1,13 +1,14 @@ package org.sadtech.autoresponder.service; -import org.apache.log4j.Logger; import org.sadtech.autoresponder.entity.UnitPointer; import org.sadtech.autoresponder.repository.UnitPointerRepository; import org.sadtech.autoresponder.repository.UnitPointerRepositoryMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class UnitPointerServiceImpl implements UnitPointerService { - private static final Logger log = Logger.getLogger(UnitPointerServiceImpl.class); + private static final Logger log = LoggerFactory.getLogger(UnitPointerServiceImpl.class); private UnitPointerRepository unitPointerRepository; From f8f8a51d7936c417fae7be81ec52c39ae3067ec6 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Wed, 15 May 2019 22:27:11 +0300 Subject: [PATCH 2/7] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=B2=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5=D0=B9?= =?UTF-8?q?=D1=81=D1=8B=20service=20=D0=B8=20repository=20=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B0=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20UnitPointer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sadtech/autoresponder/Autoresponder.java | 46 ++++++++-------- .../sadtech/autoresponder/entity/Unit.java | 54 ++++++++++--------- .../autoresponder/entity/UnitPointer.java | 3 ++ .../repository/UnitPointerRepository.java | 2 + .../repository/UnitPointerRepositoryMap.java | 5 ++ .../service/UnitPointerService.java | 3 ++ .../service/UnitPointerServiceImpl.java | 8 +++ .../sadtech/autoresponder/util/Parser.java | 3 ++ 8 files changed, 77 insertions(+), 47 deletions(-) diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index f95d38d..87c07cd 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -14,43 +14,46 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +/* + Отвечает за функционирование автоответчика + */ public class Autoresponder { private static final Logger log = LoggerFactory.getLogger(Autoresponder.class); + private static final UnitPriorityComparator UNIT_PRIORITY_COMPARATOR = new UnitPriorityComparator(); - private Set menuUnits; - private UnitPointerService unitPointerService; + /* + Начальные unit, по которым происходит поиск, если пользователь только обратился или закончил сценарий. + */ + private final Set menuUnits; + private final UnitPointerService unitPointerService; - public Autoresponder(UnitPointerService unitPointerService) { - this.unitPointerService = unitPointerService; - } - - public void setMenuUnits(Set menuUnits) { + public Autoresponder(UnitPointerService unitPointerService, Set menuUnits) { this.menuUnits = menuUnits; + this.unitPointerService = unitPointerService; } public UnitPointerService getUnitPointerService() { return unitPointerService; } - public void setUnitPointerService(UnitPointerService unitPointerService) { - this.unitPointerService = unitPointerService; - } - - public Unit answer(Integer idPerson, String message) { - UnitPointer unitPointer = checkAndAddPerson(idPerson); + /* + Возвращает unit на основании сообщения пользователя + */ + public Unit answer(Integer personId, String message) { + UnitPointer unitPointer = checkAndAddPerson(personId); Unit unit; if (unitPointer.getUnit() == null) { - unit = nextUnit(menuUnits, message); + unit = nextUnit(menuUnits, message); // выбирает unit из menuUnits, если пользователь обращается впервые } else { if (unitPointer.getUnit().getNextUnits() == null) { - unit = nextUnit(menuUnits, message); + unit = nextUnit(menuUnits, message); // если пользователь закончил сценарий, то выбирает следующий юнит из menuUnits } else { unit = nextUnit(unitPointer.getUnit().getNextUnits(), message); } } if (unit != null) { - unitPointer.setUnit(unit); + unitPointerService.edit(personId, unit); } return unit; } @@ -68,19 +71,21 @@ public class Autoresponder { private Unit nextUnit(Set nextUnits, String message) { if (nextUnits.size() > 0) { - UnitPriorityComparator unitPriorityComparator = new UnitPriorityComparator(); - Optional patternUnits = nextUnits.stream().filter(nextUnit -> nextUnit.getPattern() != null).filter(nextUnit -> patternReg(nextUnit, message)).max(unitPriorityComparator); + Optional patternUnits = nextUnits.stream() + .filter(nextUnit -> nextUnit.getPattern() != null) + .filter(nextUnit -> patternReg(nextUnit, message)) + .max(UNIT_PRIORITY_COMPARATOR); if (!patternUnits.isPresent()) { patternUnits = nextUnits.stream() .filter(nextUnit -> textPercentageMatch(nextUnit, Parser.parse(message)) >= nextUnit.getMatchThreshold()) - .max(unitPriorityComparator); + .max(UNIT_PRIORITY_COMPARATOR); } if (!patternUnits.isPresent()) { patternUnits = nextUnits.stream() .filter(nextUnit -> (nextUnit.getPattern() == null && nextUnit.getKeyWords() == null)) - .max(unitPriorityComparator); + .max(UNIT_PRIORITY_COMPARATOR); } return patternUnits.orElse(null); @@ -99,7 +104,6 @@ public class Autoresponder { if (unit.getKeyWords() != null) { Set temp = new HashSet<>(unit.getKeyWords()); temp.retainAll(words); - log.info("Юнит: " + unit.getClass().getSimpleName()); log.info("Ключевые слова юнита: " + unit.getKeyWords() + " (" + unit.getKeyWords().size() + ")"); log.info("Ключевые слова от пользователя: " + words); log.info("Пересечение: " + temp + " (" + temp.size() + ")"); diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index 1a160e6..4efc004 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -1,10 +1,14 @@ package org.sadtech.autoresponder.entity; +import java.util.Arrays; import java.util.HashSet; import java.util.Objects; import java.util.Set; import java.util.regex.Pattern; +/* + Абстрактный класс, предпологающий какие-то действия + */ public abstract class Unit { private Set keyWords; @@ -13,34 +17,40 @@ public abstract class Unit { private Integer priority = 10; private Set nextUnits; - public Unit() { - - } - - public Unit(Unit... nextUnit) { - nextUnits = new HashSet(nextUnits); - } - - public void setKeyWord(String keyWord) { - if (keyWords == null) { - keyWords = new HashSet<>(); + public void setKeyWord(String... keyWords) { + if (this.keyWords == null) { + this.keyWords = new HashSet<>(); } - keyWords.add(keyWord); + this.keyWords.addAll(Arrays.asList(keyWords)); } - public void setNextUnit(Unit unit) { - if (nextUnits == null) { - nextUnits = new HashSet<>(); + public void setKeyWords(Set keyWords) { + if (this.keyWords == null) { + this.keyWords = new HashSet<>(); } - nextUnits.add(unit); + this.keyWords.addAll(keyWords); } public Set getKeyWords() { return keyWords; } - public void setKeyWords(Set keyWords) { - this.keyWords = keyWords; + public void setNextUnit(Unit... units) { + if (nextUnits == null) { + nextUnits = new HashSet<>(); + } + nextUnits.addAll(Arrays.asList(units)); + } + + public void setNextUnits(Set nextUnits) { + if (nextUnits == null) { + nextUnits = new HashSet<>(); + } + this.nextUnits.addAll(nextUnits); + } + + public Set getNextUnits() { + return nextUnits; } public Integer getMatchThreshold() { @@ -59,14 +69,6 @@ public abstract class Unit { this.priority = priority; } - public Set getNextUnits() { - return nextUnits; - } - - public void setNextUnits(Set nextUnits) { - this.nextUnits = nextUnits; - } - public Pattern getPattern() { return pattern; } diff --git a/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java index 37b40b9..84eb190 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java +++ b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java @@ -2,6 +2,9 @@ package org.sadtech.autoresponder.entity; import java.util.Objects; +/* + Сохраняет юнит, на котором остановился пользователь. + */ public class UnitPointer { private Integer entityId; diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java index dd40da0..576bf75 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java @@ -8,6 +8,8 @@ public interface UnitPointerRepository { Integer add(UnitPointer unitPointer); + void edit(UnitPointer unitPointer); + void remove(Integer entityId); void addAll(Map unitPointerMap); diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java index 0d2c3f1..5534c55 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java @@ -15,6 +15,11 @@ public class UnitPointerRepositoryMap implements UnitPointerRepository { return unitPointer.getEntityId(); } + @Override + public void edit(UnitPointer unitPointer) { + unitPointerMap.get(unitPointer.getEntityId()).setUnit(unitPointer.getUnit()); + } + @Override public void remove(Integer entityId) { unitPointerMap.remove(entityId); diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java index e819854..b174eea 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java @@ -1,5 +1,6 @@ package org.sadtech.autoresponder.service; +import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.UnitPointer; public interface UnitPointerService { @@ -10,4 +11,6 @@ public interface UnitPointerService { UnitPointer getByEntityId(Integer entityId); + void edit(Integer personId, Unit unit); + } diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java index c9e42f3..05bea40 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java @@ -1,5 +1,6 @@ package org.sadtech.autoresponder.service; +import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.UnitPointer; import org.sadtech.autoresponder.repository.UnitPointerRepository; import org.sadtech.autoresponder.repository.UnitPointerRepositoryMap; @@ -25,6 +26,13 @@ public class UnitPointerServiceImpl implements UnitPointerService { return unitPointerRepository.findByEntityId(entityId); } + @Override + public void edit(Integer personId, Unit unit) { + if (check(personId)) { + unitPointerRepository.edit(new UnitPointer(personId, unit)); + } + } + @Override public void add(UnitPointer unitPointer) { unitPointerRepository.add(unitPointer); diff --git a/src/main/java/org/sadtech/autoresponder/util/Parser.java b/src/main/java/org/sadtech/autoresponder/util/Parser.java index 91ffdc1..9fc5d54 100644 --- a/src/main/java/org/sadtech/autoresponder/util/Parser.java +++ b/src/main/java/org/sadtech/autoresponder/util/Parser.java @@ -5,6 +5,9 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +/* + Возвращает Set слов из текста + */ public class Parser { private Parser() { From 9c821ee1fb7e5fdfd2d696c4d8cbdb472ba23a2b Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Thu, 16 May 2019 00:24:08 +0300 Subject: [PATCH 3/7] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sadtech/autoresponder/Autoresponder.java | 77 ++++++++----------- .../exception/NotFoundUnitException.java | 4 + 2 files changed, 36 insertions(+), 45 deletions(-) create mode 100644 src/main/java/org/sadtech/autoresponder/exception/NotFoundUnitException.java diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index 87c07cd..69e2edb 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -3,6 +3,7 @@ package org.sadtech.autoresponder; import org.sadtech.autoresponder.compare.UnitPriorityComparator; import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.UnitPointer; +import org.sadtech.autoresponder.exception.NotFoundUnitException; import org.sadtech.autoresponder.service.UnitPointerService; import org.sadtech.autoresponder.util.Parser; import org.slf4j.Logger; @@ -22,14 +23,11 @@ public class Autoresponder { private static final Logger log = LoggerFactory.getLogger(Autoresponder.class); private static final UnitPriorityComparator UNIT_PRIORITY_COMPARATOR = new UnitPriorityComparator(); - /* - Начальные unit, по которым происходит поиск, если пользователь только обратился или закончил сценарий. - */ - private final Set menuUnits; + private final Set startUnits; private final UnitPointerService unitPointerService; - public Autoresponder(UnitPointerService unitPointerService, Set menuUnits) { - this.menuUnits = menuUnits; + public Autoresponder(UnitPointerService unitPointerService, Set startUnits) { + this.startUnits = startUnits; this.unitPointerService = unitPointerService; } @@ -43,57 +41,46 @@ public class Autoresponder { public Unit answer(Integer personId, String message) { UnitPointer unitPointer = checkAndAddPerson(personId); Unit unit; - if (unitPointer.getUnit() == null) { - unit = nextUnit(menuUnits, message); // выбирает unit из menuUnits, если пользователь обращается впервые + if (unitPointer.getUnit() == null || unitPointer.getUnit().getNextUnits() == null) { + unit = nextUnit(startUnits, message); // выбирает unit из startUnits, если пользователь обращается впервые } else { - if (unitPointer.getUnit().getNextUnits() == null) { - unit = nextUnit(menuUnits, message); // если пользователь закончил сценарий, то выбирает следующий юнит из menuUnits - } else { - unit = nextUnit(unitPointer.getUnit().getNextUnits(), message); - } - } - if (unit != null) { - unitPointerService.edit(personId, unit); + unit = nextUnit(unitPointer.getUnit().getNextUnits(), message); } + unitPointerService.edit(personId, unit); return unit; } - private UnitPointer checkAndAddPerson(Integer idPerson) { + private Unit nextUnit(Set nextUnits, String message) { + Optional unit = nextUnits.stream() + .filter(nextUnit -> nextUnit.getPattern() != null) + .filter(nextUnit -> patternReg(nextUnit, message)) + .max(UNIT_PRIORITY_COMPARATOR); + + if (!unit.isPresent()) { + unit = nextUnits.stream() + .filter(nextUnit -> textPercentageMatch(nextUnit, Parser.parse(message)) >= nextUnit.getMatchThreshold()) + .max(UNIT_PRIORITY_COMPARATOR); + } + + if (!unit.isPresent()) { + unit = nextUnits.stream() + .filter(nextUnit -> (nextUnit.getPattern() == null && nextUnit.getKeyWords() == null)) + .max(UNIT_PRIORITY_COMPARATOR); + } + return unit.orElseThrow(NotFoundUnitException::new); + } + + private UnitPointer checkAndAddPerson(Integer personId) { UnitPointer unitPointer; - if (unitPointerService.check(idPerson)) { - unitPointer = unitPointerService.getByEntityId(idPerson); + if (unitPointerService.check(personId)) { + unitPointer = unitPointerService.getByEntityId(personId); } else { - unitPointer = new UnitPointer(idPerson); + unitPointer = new UnitPointer(personId); unitPointerService.add(unitPointer); } return unitPointer; } - private Unit nextUnit(Set nextUnits, String message) { - if (nextUnits.size() > 0) { - Optional patternUnits = nextUnits.stream() - .filter(nextUnit -> nextUnit.getPattern() != null) - .filter(nextUnit -> patternReg(nextUnit, message)) - .max(UNIT_PRIORITY_COMPARATOR); - - if (!patternUnits.isPresent()) { - patternUnits = nextUnits.stream() - .filter(nextUnit -> textPercentageMatch(nextUnit, Parser.parse(message)) >= nextUnit.getMatchThreshold()) - .max(UNIT_PRIORITY_COMPARATOR); - } - - if (!patternUnits.isPresent()) { - patternUnits = nextUnits.stream() - .filter(nextUnit -> (nextUnit.getPattern() == null && nextUnit.getKeyWords() == null)) - .max(UNIT_PRIORITY_COMPARATOR); - } - - return patternUnits.orElse(null); - } else { - return null; - } - } - private boolean patternReg(Unit unit, String message) { Pattern pattern = unit.getPattern(); Matcher m = pattern.matcher(message); diff --git a/src/main/java/org/sadtech/autoresponder/exception/NotFoundUnitException.java b/src/main/java/org/sadtech/autoresponder/exception/NotFoundUnitException.java new file mode 100644 index 0000000..bb8be59 --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/exception/NotFoundUnitException.java @@ -0,0 +1,4 @@ +package org.sadtech.autoresponder.exception; + +public class NotFoundUnitException extends RuntimeException { +} From 666e9223eafb62515a5c4d80ab66e5b7a1511866 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Thu, 16 May 2019 00:34:47 +0300 Subject: [PATCH 4/7] =?UTF-8?q?=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D1=8C=20=D0=B4=D0=B5=D1=84=D0=BE=D0=BB=D1=82?= =?UTF-8?q?=D0=BD=D1=8B=D0=B9=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82,=20=D0=B5?= =?UTF-8?q?=D1=81=D0=BB=D0=B8=20=D0=BD=D0=B8=20=D0=BE=D0=B4=D0=B8=D0=BD=20?= =?UTF-8?q?=D0=B8=D0=B7=20=D1=8E=D0=BD=D0=B8=D1=82=D0=BE=D0=B2=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=BE=D0=B4=D0=BE=D1=88=D0=B5=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sadtech/autoresponder/Autoresponder.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index 69e2edb..6c081cf 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -24,6 +24,7 @@ public class Autoresponder { private static final UnitPriorityComparator UNIT_PRIORITY_COMPARATOR = new UnitPriorityComparator(); private final Set startUnits; + private Unit defaultUnit; private final UnitPointerService unitPointerService; public Autoresponder(UnitPointerService unitPointerService, Set startUnits) { @@ -35,18 +36,26 @@ public class Autoresponder { return unitPointerService; } + public void setDefaultUnit(Unit defaultUnit) { + this.defaultUnit = defaultUnit; + } + /* - Возвращает unit на основании сообщения пользователя - */ + Возвращает unit на основании сообщения пользователя + */ public Unit answer(Integer personId, String message) { UnitPointer unitPointer = checkAndAddPerson(personId); Unit unit; - if (unitPointer.getUnit() == null || unitPointer.getUnit().getNextUnits() == null) { - unit = nextUnit(startUnits, message); // выбирает unit из startUnits, если пользователь обращается впервые - } else { - unit = nextUnit(unitPointer.getUnit().getNextUnits(), message); + try { + if (unitPointer.getUnit() == null || unitPointer.getUnit().getNextUnits() == null) { + unit = nextUnit(startUnits, message); // выбирает unit из startUnits, если пользователь обращается впервые + } else { + unit = nextUnit(unitPointer.getUnit().getNextUnits(), message); + } + unitPointerService.edit(personId, unit); + } catch (NotFoundUnitException e) { + unit = defaultUnit; } - unitPointerService.edit(personId, unit); return unit; } From f017148ef7965ead9008e0824b0c1055a9cbd918 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Thu, 16 May 2019 00:47:55 +0300 Subject: [PATCH 5/7] =?UTF-8?q?=D0=9F=D0=B0=D1=80=D1=81=D0=B5=D1=80=20?= =?UTF-8?q?=D0=BD=D0=B0=D1=83=D1=87=D0=B8=D0=BB=D1=81=D1=8F=20=D1=83=D0=B4?= =?UTF-8?q?=D0=B0=D0=BB=D1=8F=D1=82=D1=8C=20=D0=BF=D1=80=D0=B5=D0=B4=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=B8=20=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/sadtech/autoresponder/Autoresponder.java | 3 --- .../java/org/sadtech/autoresponder/entity/Unit.java | 10 +++++----- .../org/sadtech/autoresponder/entity/UnitPointer.java | 8 ++++++++ .../java/org/sadtech/autoresponder/util/Parser.java | 8 ++++++++ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index 6c081cf..80b104f 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -40,9 +40,6 @@ public class Autoresponder { this.defaultUnit = defaultUnit; } - /* - Возвращает unit на основании сообщения пользователя - */ public Unit answer(Integer personId, String message) { UnitPointer unitPointer = checkAndAddPerson(personId); Unit unit; diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index 4efc004..3880082 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -17,11 +17,11 @@ public abstract class Unit { private Integer priority = 10; private Set nextUnits; - public void setKeyWord(String... keyWords) { + public void setKeyWord(String... keyWord) { if (this.keyWords == null) { this.keyWords = new HashSet<>(); } - this.keyWords.addAll(Arrays.asList(keyWords)); + this.keyWords.addAll(Arrays.asList(keyWord)); } public void setKeyWords(Set keyWords) { @@ -35,11 +35,11 @@ public abstract class Unit { return keyWords; } - public void setNextUnit(Unit... units) { + public void setNextUnit(Unit... unit) { if (nextUnits == null) { nextUnits = new HashSet<>(); } - nextUnits.addAll(Arrays.asList(units)); + nextUnits.addAll(Arrays.asList(unit)); } public void setNextUnits(Set nextUnits) { @@ -80,7 +80,7 @@ public abstract class Unit { @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (!(o instanceof Unit)) return false; Unit unit = (Unit) o; return Objects.equals(keyWords, unit.keyWords) && Objects.equals(pattern, unit.pattern) && diff --git a/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java index 84eb190..271c5c4 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java +++ b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java @@ -48,4 +48,12 @@ public class UnitPointer { public int hashCode() { return Objects.hash(entityId, unit); } + + @Override + public String toString() { + return "UnitPointer{" + + "entityId=" + entityId + + ", unit=" + unit + + '}'; + } } diff --git a/src/main/java/org/sadtech/autoresponder/util/Parser.java b/src/main/java/org/sadtech/autoresponder/util/Parser.java index 9fc5d54..4466194 100644 --- a/src/main/java/org/sadtech/autoresponder/util/Parser.java +++ b/src/main/java/org/sadtech/autoresponder/util/Parser.java @@ -4,12 +4,19 @@ import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; /* Возвращает Set слов из текста */ public class Parser { + private static final Set pretexts = Stream + .of("в", "без", "до", "из", "к", "на", "по", "о", "от", "перед", "при", "с", "у", "за", "над", "об", + "под", "про", "для") + .collect(Collectors.toSet()); + private Parser() { throw new IllegalStateException("Utility Class"); } @@ -21,6 +28,7 @@ public class Parser { while (m.find()) { words.add(m.group().toLowerCase()); } + words.removeAll(pretexts); return words; } From 659c9cf2ad43e42e46a1805033d2322cf929bef1 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Fri, 17 May 2019 00:32:37 +0300 Subject: [PATCH 6/7] =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D0=BA=D0=B8=D0=B9=20?= =?UTF-8?q?=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8=D0=BD?= =?UTF-8?q?=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/sadtech/autoresponder/Autoresponder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index 80b104f..a269541 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -53,7 +53,7 @@ public class Autoresponder { } catch (NotFoundUnitException e) { unit = defaultUnit; } - return unit; + return Optional.ofNullable(unit).orElseThrow(NotFoundUnitException::new); } private Unit nextUnit(Set nextUnits, String message) { From 5b942ab2d6128f20f495409b3de0fdfa6ebe1159 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 18 May 2019 13:09:09 +0300 Subject: [PATCH 7/7] release-1.6.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 92b800f..9909887 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.autoresponder autoresponder - 1.5.1-SNAPSHOT + 1.6.0-RELEASE jar