From 33253b50004bf321455a9e2f121c8d6012e5e1ee Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 2 Feb 2019 09:02:05 +0300 Subject: [PATCH] =?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); } + }