diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index 4c2ea9f..553c119 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -37,7 +37,7 @@ public class Autoresponder { unit = nextUnit(person.getUnit().getNextUnits(), message); } } - if (unit!=null) { + if (unit != null) { person.setUnit(unit); } return unit; @@ -59,7 +59,7 @@ public class Autoresponder { Parser parser = new Parser(); parser.setText(message); parser.parse(); - Optional max = nextUnits.stream().filter(nextUnit -> textPercentageMatch(nextUnit, parser.getWords()) >= nextUnit.getMatchThreshold()).max(new UnitPriorityComparator()); + Optional max = nextUnits.stream().filter(nextUnit -> textPercentageMatch(nextUnit, parser.getWords()) >= nextUnit.getMatchThreshold() || nextUnit.getKeyWords() == null).max(new UnitPriorityComparator()); return max.orElse(null); } else { return null; @@ -67,10 +67,14 @@ public class Autoresponder { } private Double textPercentageMatch(Unit unit, Set words) { - 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; + 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; + } 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 c2d3c7a..9e0bb8e 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -1,8 +1,6 @@ package org.sadtech.autoresponder.entity; -import java.util.List; -import java.util.Objects; -import java.util.Set; +import java.util.*; public abstract class Unit { @@ -23,6 +21,20 @@ public abstract class Unit { this.nextUnits = nextUnits; } + public void setKeyWord(String keyWord) { + if (keyWords == null) { + keyWords = new HashSet<>(); + } + keyWords.add(keyWord); + } + + public void setNextUnit(Unit unit) { + if (nextUnits == null) { + nextUnits = new ArrayList<>(); + } + nextUnits.add(unit); + } + public Set getKeyWords() { return keyWords; }