diff --git a/pom.xml b/pom.xml index 69e20ea..dfd0910 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,8 @@ org.sadtech.autoresponder autoresponder - 1.2.0-RELEASE + 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 553c119..05d54a2 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,22 +58,44 @@ 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()); 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 + " Необходимо: " + 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..e4df06c 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -1,21 +1,24 @@ 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) { + 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; @@ -67,12 +70,21 @@ 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; 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); @@ -80,6 +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/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/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(); -// } -// -//} - 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