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

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>
<artifactId>autoresponder</artifactId>
<version>3.1.0</version>
<version>3.2.0</version>
<packaging>jar</packaging>
<name>Abstract Autoresponder</name>

View File

@ -48,11 +48,11 @@ public final class Responder {
if (message != null && nextUnits != null) {
for (U unit : nextUnits) {
final String unitPhrase = unit.getPhrase();
final Set<String> unitPhrases = unit.getPhrases();
if (
unitPhrase != null
&& !unitPhrase.isEmpty()
&& unitPhrase.equalsIgnoreCase(message)
unitPhrases != null
&& !unitPhrases.isEmpty()
&& unitPhrases.contains(message)
) {
searchUnit.add(unit);
}
@ -79,7 +79,8 @@ public final class Responder {
}
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) {
@ -87,7 +88,8 @@ public final class Responder {
}
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) {

View File

@ -1,5 +1,6 @@
package dev.struchkov.autoresponder.entity;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
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 в сценарии.
*/
protected Set<U> nextUnits = new HashSet<>();
protected Set<U> nextUnits;
protected Unit(
Set<KeyWord> keyWords,
String phrase,
Set<String> phrases,
Pattern pattern,
Integer matchThreshold,
Integer priority,
Set<U> nextUnits
) {
this.keyWords = keyWords;
this.phrase = phrase;
this.phrases = phrases;
this.pattern = pattern;
this.matchThreshold = matchThreshold == null ? 10 : matchThreshold;
this.priority = priority == null ? 10 : priority;
@ -66,12 +67,16 @@ public abstract class Unit<U extends Unit<U>> {
this.keyWords = keyWords;
}
public String getPhrase() {
return phrase;
public Set<String> getPhrases() {
return phrases;
}
public void setPhrase(String phrase) {
this.phrase = phrase;
public void addPhrase(String phrase) {
phrases.add(phrase);
}
public void addPhrases(Collection<String> phrases) {
phrases.addAll(phrases);
}
public Pattern getPattern() {
@ -111,19 +116,19 @@ public abstract class Unit<U extends Unit<U>> {
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(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
public int hashCode() {
return Objects.hash(keyWords, phrase, pattern, matchThreshold, priority);
return Objects.hash(keyWords, phrases, pattern, matchThreshold, priority);
}
@Override
public String toString() {
return "Unit{" +
"keyWords=" + keyWords +
", phrase='" + phrase + '\'' +
", phrases='" + phrases + '\'' +
", pattern=" + pattern +
", matchThreshold=" + matchThreshold +
", priority=" + priority +