Теперь можно указывать несколько фраз для юнита

This commit is contained in:
Struchkov Mark 2022-07-15 10:26:12 +03:00
parent 252d987857
commit 1963b5ba27
3 changed files with 26 additions and 19 deletions

View File

@ -6,7 +6,7 @@
<groupId>dev.struchkov</groupId> <groupId>dev.struchkov</groupId>
<artifactId>autoresponder</artifactId> <artifactId>autoresponder</artifactId>
<version>3.1.0</version> <version>3.2.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Abstract Autoresponder</name> <name>Abstract Autoresponder</name>

View File

@ -48,11 +48,11 @@ public final class Responder {
if (message != null && nextUnits != null) { if (message != null && nextUnits != null) {
for (U unit : nextUnits) { for (U unit : nextUnits) {
final String unitPhrase = unit.getPhrase(); final Set<String> unitPhrases = unit.getPhrases();
if ( if (
unitPhrase != null unitPhrases != null
&& !unitPhrase.isEmpty() && !unitPhrases.isEmpty()
&& unitPhrase.equalsIgnoreCase(message) && unitPhrases.contains(message)
) { ) {
searchUnit.add(unit); searchUnit.add(unit);
} }
@ -79,7 +79,8 @@ public final class Responder {
} }
private static <U extends Unit<U>> boolean isNotPhrase(U nextUnit) { private static <U extends Unit<U>> boolean isNotPhrase(U nextUnit) {
return nextUnit.getPhrase() == null; final Set<String> phrases = nextUnit.getPhrases();
return phrases == null || phrases.isEmpty();
} }
private static <U extends Unit<U>> boolean isNotPattern(U nextUnit) { private static <U extends Unit<U>> boolean isNotPattern(U nextUnit) {
@ -87,7 +88,8 @@ public final class Responder {
} }
private static <U extends Unit<U>> boolean isNotKeyWords(U nextUnit) { private static <U extends Unit<U>> boolean isNotKeyWords(U nextUnit) {
return nextUnit.getKeyWords() == null || nextUnit.getKeyWords().isEmpty(); final Set<KeyWord> keyWords = nextUnit.getKeyWords();
return keyWords == null || keyWords.isEmpty();
} }
private static boolean patternReg(Pattern pattern, String message) { private static boolean patternReg(Pattern pattern, String message) {

View File

@ -1,5 +1,6 @@
package dev.struchkov.autoresponder.entity; package dev.struchkov.autoresponder.entity;
import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
@ -15,12 +16,12 @@ public abstract class Unit<U extends Unit<U>> {
/** /**
* Ключевые слова. * Ключевые слова.
*/ */
protected Set<KeyWord> keyWords = new HashSet<>(); protected Set<KeyWord> keyWords;
/** /**
* Точная фраза. * Точная фраза.
*/ */
protected String phrase; protected Set<String> phrases;
/** /**
* Регулярное выражение. * Регулярное выражение.
@ -40,18 +41,18 @@ public abstract class Unit<U extends Unit<U>> {
/** /**
* Множество следующих Unit в сценарии. * Множество следующих Unit в сценарии.
*/ */
protected Set<U> nextUnits = new HashSet<>(); protected Set<U> nextUnits;
protected Unit( protected Unit(
Set<KeyWord> keyWords, Set<KeyWord> keyWords,
String phrase, Set<String> phrases,
Pattern pattern, Pattern pattern,
Integer matchThreshold, Integer matchThreshold,
Integer priority, Integer priority,
Set<U> nextUnits Set<U> nextUnits
) { ) {
this.keyWords = keyWords; this.keyWords = keyWords;
this.phrase = phrase; this.phrases = phrases;
this.pattern = pattern; this.pattern = pattern;
this.matchThreshold = matchThreshold == null ? 10 : matchThreshold; this.matchThreshold = matchThreshold == null ? 10 : matchThreshold;
this.priority = priority == null ? 10 : priority; this.priority = priority == null ? 10 : priority;
@ -66,12 +67,16 @@ public abstract class Unit<U extends Unit<U>> {
this.keyWords = keyWords; this.keyWords = keyWords;
} }
public String getPhrase() { public Set<String> getPhrases() {
return phrase; return phrases;
} }
public void setPhrase(String phrase) { public void addPhrase(String phrase) {
this.phrase = phrase; phrases.add(phrase);
}
public void addPhrases(Collection<String> phrases) {
phrases.addAll(phrases);
} }
public Pattern getPattern() { public Pattern getPattern() {
@ -111,19 +116,19 @@ public abstract class Unit<U extends Unit<U>> {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
Unit<?> unit = (Unit<?>) o; Unit<?> unit = (Unit<?>) o;
return Objects.equals(keyWords, unit.keyWords) && Objects.equals(phrase, unit.phrase) && Objects.equals(pattern, unit.pattern) && Objects.equals(matchThreshold, unit.matchThreshold) && Objects.equals(priority, unit.priority); return Objects.equals(keyWords, unit.keyWords) && Objects.equals(phrases, unit.phrases) && Objects.equals(pattern, unit.pattern) && Objects.equals(matchThreshold, unit.matchThreshold) && Objects.equals(priority, unit.priority);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(keyWords, phrase, pattern, matchThreshold, priority); return Objects.hash(keyWords, phrases, pattern, matchThreshold, priority);
} }
@Override @Override
public String toString() { public String toString() {
return "Unit{" + return "Unit{" +
"keyWords=" + keyWords + "keyWords=" + keyWords +
", phrase='" + phrase + '\'' + ", phrases='" + phrases + '\'' +
", pattern=" + pattern + ", pattern=" + pattern +
", matchThreshold=" + matchThreshold + ", matchThreshold=" + matchThreshold +
", priority=" + priority + ", priority=" + priority +