Новая фича

Теперь есть юниты, которые автоматически отвечают на любое сообщение
This commit is contained in:
Mark Struchkov 2019-01-26 14:58:34 +03:00
parent a4eaf949e0
commit 11f9858c01
2 changed files with 25 additions and 9 deletions

View File

@ -37,7 +37,7 @@ public class Autoresponder {
unit = nextUnit(person.getUnit().getNextUnits(), message); unit = nextUnit(person.getUnit().getNextUnits(), message);
} }
} }
if (unit!=null) { if (unit != null) {
person.setUnit(unit); person.setUnit(unit);
} }
return unit; return unit;
@ -59,7 +59,7 @@ public class Autoresponder {
Parser parser = new Parser(); Parser parser = new Parser();
parser.setText(message); parser.setText(message);
parser.parse(); parser.parse();
Optional<Unit> max = nextUnits.stream().filter(nextUnit -> textPercentageMatch(nextUnit, parser.getWords()) >= nextUnit.getMatchThreshold()).max(new UnitPriorityComparator()); Optional<Unit> max = nextUnits.stream().filter(nextUnit -> textPercentageMatch(nextUnit, parser.getWords()) >= nextUnit.getMatchThreshold() || nextUnit.getKeyWords() == null).max(new UnitPriorityComparator());
return max.orElse(null); return max.orElse(null);
} else { } else {
return null; return null;
@ -67,10 +67,14 @@ public class Autoresponder {
} }
private Double textPercentageMatch(Unit unit, Set<String> words) { private Double textPercentageMatch(Unit unit, Set<String> words) {
if (unit.getKeyWords() != null) {
Set<String> temp = new HashSet<>(unit.getKeyWords()); Set<String> temp = new HashSet<>(unit.getKeyWords());
temp.retainAll(words); temp.retainAll(words);
log.info((temp.size() / unit.getKeyWords().size()) * 100); log.info((temp.size() / unit.getKeyWords().size()) * 100);
return (double) (temp.size() / unit.getKeyWords().size()) * 100; return (double) (temp.size() / unit.getKeyWords().size()) * 100;
} else {
return 0.0;
}
} }
} }

View File

@ -1,8 +1,6 @@
package org.sadtech.autoresponder.entity; package org.sadtech.autoresponder.entity;
import java.util.List; import java.util.*;
import java.util.Objects;
import java.util.Set;
public abstract class Unit { public abstract class Unit {
@ -23,6 +21,20 @@ public abstract class Unit {
this.nextUnits = nextUnits; 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<String> getKeyWords() { public Set<String> getKeyWords() {
return keyWords; return keyWords;
} }