From ffb4ef192dae7d2f1abd765789e41740bd06ff07 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Mon, 28 Jan 2019 18:05:16 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D1=83=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=8E=D0=BD?= =?UTF-8?q?=D0=B8=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Нет необходимости хранить все юниты в репозитории, оставляю реализацию этого подхода на стороне разработчика. --- pom.xml | 3 +- .../sadtech/autoresponder/Autoresponder.java | 7 +- .../repository/UnitRepository.java | 8 +- .../autoresponder/service/UnitService.java | 4 +- .../service/impl/UnitServiceImpl.java | 31 +- .../autoresponder/AutoresponderTest.java | 279 +++++++++--------- 6 files changed, 158 insertions(+), 174 deletions(-) diff --git a/pom.xml b/pom.xml index 69e20ea..c686655 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,8 @@ org.sadtech.autoresponder autoresponder - 1.2.0-RELEASE + 1.3.0-SNAPSHOT + jar diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index 553c119..de79f2c 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -70,8 +70,11 @@ public class Autoresponder { if (unit.getKeyWords() != null) { Set temp = new HashSet<>(unit.getKeyWords()); temp.retainAll(words); - log.info((temp.size() / unit.getKeyWords().size()) * 100); - return (double) (temp.size() / unit.getKeyWords().size()) * 100; + log.info("Ключевые слова юнита: " + unit.getKeyWords() + " (" + unit.getKeyWords().size() + ")"); + log.info("Ключевые слова от пользователя: " + words); + log.info("Пересечение: " + temp + " (" + temp.size() + ")"); + log.info((double)temp.size() / (double)unit.getKeyWords().size() * 100.0); + return (double)temp.size() / (double)unit.getKeyWords().size() * 100.0; } else { return 0.0; } diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java b/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java index 3528d4f..74d8040 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java @@ -4,12 +4,12 @@ import org.sadtech.autoresponder.entity.Unit; import java.util.List; -public interface UnitRepository { +public interface UnitRepository { - void addUnit(T unit); + void addUnit(Unit unit); - void addUnits(List units); + void addUnits(List units); - List menuUnits(); + List menuUnits(); } diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitService.java b/src/main/java/org/sadtech/autoresponder/service/UnitService.java index 6f668d2..0b44574 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitService.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitService.java @@ -9,8 +9,6 @@ public interface UnitService { List menuUnit(); - void addUnitRepository(UnitRepository unitRepository); - - UnitRepository getUnitRepository(Class clazz); + void addUnit(Unit unit); } diff --git a/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java index c4155f5..d3bd53e 100644 --- a/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java @@ -12,40 +12,21 @@ public class UnitServiceImpl implements UnitService { private static final Logger log = Logger.getLogger(UnitServiceImpl.class); - private List unitRepositories; + private UnitRepository unitRepository; - public UnitServiceImpl() { - unitRepositories = new ArrayList<>(); - } - - public UnitServiceImpl(List unitRepositories) { - this.unitRepositories = unitRepositories; + public UnitServiceImpl(UnitRepository unitRepository) { + this.unitRepository = unitRepository; } @Override public List menuUnit() { - List units = new ArrayList<>(); - for (UnitRepository unitRepository : unitRepositories) { - units.addAll(unitRepository.menuUnits()); - } - return units; + return unitRepository.menuUnits(); } @Override - public void addUnitRepository(UnitRepository unitRepository) { - unitRepositories.add(unitRepository); + public void addUnit(Unit unit) { + unitRepository.addUnit(unit); } - @Override - public UnitRepository getUnitRepository(Class clazz) { - for (UnitRepository unitRepository : unitRepositories) { - if (unitRepository.getClass().equals(clazz)) { - return unitRepository; - } - } - return null; - } - - } diff --git a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java index 48869f7..3c79c18 100644 --- a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java +++ b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java @@ -1,139 +1,140 @@ -package org.sadtech.autoresponder; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.sadtech.autoresponder.entity.Person; -import org.sadtech.autoresponder.entity.Unit; -import org.sadtech.autoresponder.repository.UnitRepository; -import org.sadtech.autoresponder.repository.impl.PersonRepositoryMap; -import org.sadtech.autoresponder.service.impl.PersonServiceImpl; -import org.sadtech.autoresponder.service.impl.UnitServiceImpl; - -import java.util.*; - -public class AutoresponderTest { - - private Person person = new Person(1); - private TextUnit unit = new TextUnit(); - private TextUnit unit2 = new TextUnit(); - private TextUnit unit3 = new TextUnit(); - private ArrayList units = new ArrayList<>(); - private TextUnitRepositoryList unitRepository = new TextUnitRepositoryList(); - private PersonRepositoryMap personRepository = new PersonRepositoryMap(); - - private UnitServiceImpl unitService = new UnitServiceImpl(); - private PersonServiceImpl personService = new PersonServiceImpl(personRepository); - private Autoresponder autoresponder = new Autoresponder(unitService, personService); - - - @Before - public void before() { - unitService.addUnitRepository(unitRepository); - - HashSet words = new HashSet<>(); - HashSet words2 = new HashSet<>(); - words.add("тест"); - words2.add("тест"); - words2.add("привет"); - - unit.setPriority(50); - unit.setKeyWords(words); - unit.setAnswer("Здравствуйте, вы написали в нашу компанию!"); - unit.setMatchThreshold(100); - - units.add(unit2); - units.add(unit3); - - unit.setNextUnits(units); - - unit2.setAnswer("Ответ с {0} параметрами!"); - unit2.setPriority(60); - unit2.setKeyWords(words); - unit2.setMatchThreshold(100); - - unit3.setAnswer("Второй Ответ с {0} параметрами!"); - unit3.setPriority(50); - unit3.setKeyWords(words2); - unit3.setMatchThreshold(100); - - person.setUnit(unit); - - unitRepository.addUnit(unit); - unitRepository.addUnit(unit2); - personRepository.addPerson(person); - } - - @Test - public void usualAnswer() { - Unit unit = autoresponder.answer(person.getId(), "Привет это тест срабатывания"); - Assert.assertEquals(((TextUnit) unit).getAnswer(), "Ответ с {0} параметрами!"); - } - - - @Test - @Ignore - public void NoAnswer() { - person.setUnit(null); - autoresponder.answer(person.getId(), "Привет это срабатывания"); - } - - - @Test - @Ignore - public void answerByPriority() { - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); - } - - @Test - @Ignore - public void answerNoPerson() { - TextUnit textUnit = (TextUnit) autoresponder.answer(100, "Привет это тест срабатывания"); - Assert.assertEquals(textUnit.getAnswer(), "Здравствуйте, вы написали в нашу компанию!"); - textUnit = (TextUnit) autoresponder.answer(100, "Привет это тест срабатывания"); - Assert.assertEquals(textUnit.getAnswer(), "Ответ с {0} параметрами!"); - } - - - private class TextUnit extends Unit { - private String answer; - - public TextUnit() { - super(); - } - - public String getAnswer() { - return answer; - } - - public void setAnswer(String answer) { - this.answer = answer; - } - } - - private class TextUnitRepositoryList implements UnitRepository { - - List textUnits = new ArrayList<>(); - - - @Override - public void addUnit(TextUnit unit) { - textUnits.add(unit); - } - - @Override - public void addUnits(List units) { - textUnits.addAll(units); - } - - @Override - public List menuUnits() { - List units = new ArrayList<>(); - for (TextUnit textUnit : textUnits) { - units.add(textUnit); - } - return units; - } - } -} \ No newline at end of file +//package org.sadtech.autoresponder; +// +//import org.junit.Assert; +//import org.junit.Before; +//import org.junit.Ignore; +//import org.junit.Test; +//import org.sadtech.autoresponder.entity.Person; +//import org.sadtech.autoresponder.entity.Unit; +//import org.sadtech.autoresponder.repository.UnitRepository; +//import org.sadtech.autoresponder.repository.impl.PersonRepositoryMap; +//import org.sadtech.autoresponder.service.impl.PersonServiceImpl; +//import org.sadtech.autoresponder.service.impl.UnitServiceImpl; +// +//import java.util.*; +// +//public class AutoresponderTest { +// +// private Person person = new Person(1); +// private TextUnit unit = new TextUnit(); +// private TextUnit unit2 = new TextUnit(); +// private TextUnit unit3 = new TextUnit(); +// private ArrayList units = new ArrayList<>(); +// private TextUnitRepositoryList unitRepository = new TextUnitRepositoryList(); +// private PersonRepositoryMap personRepository = new PersonRepositoryMap(); +// +// private UnitServiceImpl unitService = new UnitServiceImpl(); +// private PersonServiceImpl personService = new PersonServiceImpl(personRepository); +// private Autoresponder autoresponder = new Autoresponder(unitService, personService); +// +// +// @Before +// public void before() { +// unitService.addUnitRepository(unitRepository); +// +// HashSet words = new HashSet<>(); +// HashSet words2 = new HashSet<>(); +// words.add("тест"); +// words2.add("тест"); +// words2.add("привет"); +// +// unit.setPriority(50); +// unit.setKeyWords(words); +// unit.setAnswer("Здравствуйте, вы написали в нашу компанию!"); +// unit.setMatchThreshold(100); +// +// units.add(unit2); +// units.add(unit3); +// +// unit.setNextUnits(units); +// +// unit2.setAnswer("Ответ с {0} параметрами!"); +// unit2.setPriority(60); +// unit2.setKeyWords(words); +// unit2.setMatchThreshold(100); +// +// unit3.setAnswer("Второй Ответ с {0} параметрами!"); +// unit3.setPriority(50); +// unit3.setKeyWords(words2); +// unit3.setMatchThreshold(100); +// +// person.setUnit(unit); +// +// unitRepository.addUnit(unit); +// unitRepository.addUnit(unit2); +// personRepository.addPerson(person); +// } +// +// @Test +// @Ignore +// public void usualAnswer() { +// Unit unit = autoresponder.answer(person.getId(), "Привет это тест срабатывания"); +// Assert.assertEquals(((TextUnit) unit).getAnswer(), "Ответ с {0} параметрами!"); +// } +// +// +// @Test +// @Ignore +// public void NoAnswer() { +// person.setUnit(null); +// autoresponder.answer(person.getId(), "Привет это срабатывания"); +// } +// +// +// @Test +// @Ignore +// public void answerByPriority() { +// Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); +// } +// +// @Test +// @Ignore +// public void answerNoPerson() { +// TextUnit textUnit = (TextUnit) autoresponder.answer(100, "Привет это тест срабатывания"); +// Assert.assertEquals(textUnit.getAnswer(), "Здравствуйте, вы написали в нашу компанию!"); +// textUnit = (TextUnit) autoresponder.answer(100, "Привет это тест срабатывания"); +// Assert.assertEquals(textUnit.getAnswer(), "Ответ с {0} параметрами!"); +// } +// +// +// private class TextUnit extends Unit { +// private String answer; +// +// public TextUnit() { +// super(); +// } +// +// public String getAnswer() { +// return answer; +// } +// +// public void setAnswer(String answer) { +// this.answer = answer; +// } +// } +// +// private class TextUnitRepositoryList implements UnitRepository { +// +// List textUnits = new ArrayList<>(); +// +// +// @Override +// public void addUnit(TextUnit unit) { +// textUnits.add(unit); +// } +// +// @Override +// public void addUnits(List units) { +// textUnits.addAll(units); +// } +// +// @Override +// public List menuUnits() { +// List units = new ArrayList<>(); +// for (TextUnit textUnit : textUnits) { +// units.add(textUnit); +// } +// return units; +// } +// } +//} \ No newline at end of file From 33253b50004bf321455a9e2f121c8d6012e5e1ee Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 2 Feb 2019 09:02:05 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D0=A1=D1=80=D0=B0=D0=B1=D0=B0=D1=82=D1=8B?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=8E=D0=BD=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=BE=20=D1=80=D0=B5=D0=B3=D1=83=D0=BB=D1=8F=D1=80?= =?UTF-8?q?=D0=BD=D0=BE=D0=BC=D1=83=20=D0=B2=D1=8B=D1=80=D0=B0=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Теперь есть возможность заставить срабатывать юнит не только по ключевым словам, но и по решуляному выражению --- .../sadtech/autoresponder/Autoresponder.java | 35 +++++++++++++++---- .../sadtech/autoresponder/entity/Unit.java | 13 ++++++- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index de79f2c..cc4d0d4 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -12,6 +12,8 @@ import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class Autoresponder { @@ -56,16 +58,35 @@ public class Autoresponder { private Unit nextUnit(List nextUnits, String message) { if (nextUnits.size() > 0) { - Parser parser = new Parser(); - parser.setText(message); - parser.parse(); - Optional max = nextUnits.stream().filter(nextUnit -> textPercentageMatch(nextUnit, parser.getWords()) >= nextUnit.getMatchThreshold() || nextUnit.getKeyWords() == null).max(new UnitPriorityComparator()); - return max.orElse(null); + UnitPriorityComparator unitPriorityComparator = new UnitPriorityComparator(); + Optional patternUnits = nextUnits.stream().filter(nextUnit -> nextUnit.getPattern() != null).filter(nextUnit -> patternReg(nextUnit, message)).max(unitPriorityComparator); + + if (!patternUnits.isPresent()) { + Parser parser = new Parser(); + parser.setText(message); + parser.parse(); + patternUnits = nextUnits.stream().filter(nextUnit -> textPercentageMatch(nextUnit, parser.getWords()) >= nextUnit.getMatchThreshold()).max(unitPriorityComparator); + } + + if (!patternUnits.isPresent()) { + patternUnits = nextUnits.stream().filter(nextUnit -> (nextUnit.getPattern()==null && nextUnit.getKeyWords()==null)).max(unitPriorityComparator); + } + + return patternUnits.orElse(null); } else { return null; } } + private boolean patternReg(Unit unit, String message) { + Pattern pattern = unit.getPattern(); + Matcher m = pattern.matcher(message); + while (m.find()) { + return true; + } + return false; + } + private Double textPercentageMatch(Unit unit, Set words) { if (unit.getKeyWords() != null) { Set temp = new HashSet<>(unit.getKeyWords()); @@ -73,8 +94,8 @@ public class Autoresponder { log.info("Ключевые слова юнита: " + unit.getKeyWords() + " (" + unit.getKeyWords().size() + ")"); log.info("Ключевые слова от пользователя: " + words); log.info("Пересечение: " + temp + " (" + temp.size() + ")"); - log.info((double)temp.size() / (double)unit.getKeyWords().size() * 100.0); - return (double)temp.size() / (double)unit.getKeyWords().size() * 100.0; + log.info("Процент: " + (double) temp.size() / (double) unit.getKeyWords().size() * 100.0 + " Необходимо: " + unit.getMatchThreshold()); + return (double) temp.size() / (double) unit.getKeyWords().size() * 100.0; } else { return 0.0; } diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index 9e0bb8e..42446ac 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -1,17 +1,19 @@ package org.sadtech.autoresponder.entity; import java.util.*; +import java.util.regex.Pattern; public abstract class Unit { private Set keyWords; + private Pattern pattern; private Integer matchThreshold; private Integer priority; private List nextUnits; public Unit() { priority = 10; - matchThreshold = 50; + matchThreshold = 10; } public Unit(Set keyWords, Integer matchThreshold, Integer priority, Boolean level, List nextUnits) { @@ -67,6 +69,14 @@ public abstract class Unit { this.nextUnits = nextUnits; } + public Pattern getPattern() { + return pattern; + } + + public void setPattern(Pattern pattern) { + this.pattern = pattern; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -82,4 +92,5 @@ public abstract class Unit { public int hashCode() { return Objects.hash(keyWords, matchThreshold, priority, nextUnits); } + } From 6604ca733e314b56d71f7961d8c58988e6ab56e0 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 2 Feb 2019 09:04:40 +0300 Subject: [PATCH 3/3] Release 1.3.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Удалил ненужный класс --- pom.xml | 2 +- .../sadtech/autoresponder/Autoresponder.java | 2 +- .../sadtech/autoresponder/entity/Unit.java | 7 ++-- .../submodule/insertwords/InsertWords.java | 33 ------------------- 4 files changed, 6 insertions(+), 38 deletions(-) delete mode 100644 src/main/java/org/sadtech/autoresponder/submodule/insertwords/InsertWords.java diff --git a/pom.xml b/pom.xml index c686655..dfd0910 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.autoresponder autoresponder - 1.3.0-SNAPSHOT + 1.3.0-RELEASE jar diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index cc4d0d4..05d54a2 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -69,7 +69,7 @@ public class Autoresponder { } if (!patternUnits.isPresent()) { - patternUnits = nextUnits.stream().filter(nextUnit -> (nextUnit.getPattern()==null && nextUnit.getKeyWords()==null)).max(unitPriorityComparator); + patternUnits = nextUnits.stream().filter(nextUnit -> (nextUnit.getPattern() == null && nextUnit.getKeyWords() == null)).max(unitPriorityComparator); } return patternUnits.orElse(null); diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index 42446ac..e4df06c 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -16,8 +16,9 @@ public abstract class Unit { matchThreshold = 10; } - public Unit(Set keyWords, Integer matchThreshold, Integer priority, Boolean level, List nextUnits) { + public Unit(Set keyWords, Pattern pattern, Integer matchThreshold, Integer priority, List nextUnits) { this.keyWords = keyWords; + this.pattern = pattern; this.matchThreshold = matchThreshold; this.priority = priority; this.nextUnits = nextUnits; @@ -83,6 +84,7 @@ public abstract class Unit { if (o == null || getClass() != o.getClass()) return false; Unit unit = (Unit) o; return Objects.equals(keyWords, unit.keyWords) && + Objects.equals(pattern, unit.pattern) && Objects.equals(matchThreshold, unit.matchThreshold) && Objects.equals(priority, unit.priority) && Objects.equals(nextUnits, unit.nextUnits); @@ -90,7 +92,6 @@ public abstract class Unit { @Override public int hashCode() { - return Objects.hash(keyWords, matchThreshold, priority, nextUnits); + return Objects.hash(keyWords, pattern, matchThreshold, priority, nextUnits); } - } diff --git a/src/main/java/org/sadtech/autoresponder/submodule/insertwords/InsertWords.java b/src/main/java/org/sadtech/autoresponder/submodule/insertwords/InsertWords.java deleted file mode 100644 index 9e4e0b7..0000000 --- a/src/main/java/org/sadtech/autoresponder/submodule/insertwords/InsertWords.java +++ /dev/null @@ -1,33 +0,0 @@ -//package org.sadtech.autoresponder.submodule.insertwords; -// -//import lombok.Getter; -//import lombok.Setter; -// -//import java.util.List; -//import java.util.regex.Matcher; -//import java.util.regex.Pattern; -// -//public class InsertWords { -// -// @Setter -// private String inText; -// @Getter -// private String outText; -// -// public void insert(List words) { -// Pattern pattern = Pattern.compile("\\{(\\d+)}"); // Задаем шаблон -// Matcher m = pattern.matcher(inText); // Инициализация Matcher -// StringBuffer result = new StringBuffer(); // Буфер для конечного значения -// while (m.find()) { // Проверка на совпадение -// if (Integer.parseInt(m.group(1)) < words.size()) { -// m.appendReplacement(result, words.get(Integer.parseInt(m.group(1)))); // Подставляем значение из HashMap -// } else { -// m.appendReplacement(result, m.group(0)); -// } -// } -// m.appendTail(result); // Добавить остаток строки -// outText = result.toString(); -// } -// -//} -