Парсер научился удалять предлоги

Рефакторинг
This commit is contained in:
Mark Struchkov 2019-05-16 00:47:55 +03:00
parent 666e9223ea
commit f017148ef7
4 changed files with 21 additions and 8 deletions

View File

@ -40,9 +40,6 @@ public class Autoresponder {
this.defaultUnit = defaultUnit; this.defaultUnit = defaultUnit;
} }
/*
Возвращает unit на основании сообщения пользователя
*/
public Unit answer(Integer personId, String message) { public Unit answer(Integer personId, String message) {
UnitPointer unitPointer = checkAndAddPerson(personId); UnitPointer unitPointer = checkAndAddPerson(personId);
Unit unit; Unit unit;

View File

@ -17,11 +17,11 @@ public abstract class Unit {
private Integer priority = 10; private Integer priority = 10;
private Set<Unit> nextUnits; private Set<Unit> nextUnits;
public void setKeyWord(String... keyWords) { public void setKeyWord(String... keyWord) {
if (this.keyWords == null) { if (this.keyWords == null) {
this.keyWords = new HashSet<>(); this.keyWords = new HashSet<>();
} }
this.keyWords.addAll(Arrays.asList(keyWords)); this.keyWords.addAll(Arrays.asList(keyWord));
} }
public void setKeyWords(Set<String> keyWords) { public void setKeyWords(Set<String> keyWords) {
@ -35,11 +35,11 @@ public abstract class Unit {
return keyWords; return keyWords;
} }
public void setNextUnit(Unit... units) { public void setNextUnit(Unit... unit) {
if (nextUnits == null) { if (nextUnits == null) {
nextUnits = new HashSet<>(); nextUnits = new HashSet<>();
} }
nextUnits.addAll(Arrays.asList(units)); nextUnits.addAll(Arrays.asList(unit));
} }
public void setNextUnits(Set<Unit> nextUnits) { public void setNextUnits(Set<Unit> nextUnits) {
@ -80,7 +80,7 @@ public abstract class Unit {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (!(o instanceof Unit)) return false;
Unit unit = (Unit) o; Unit unit = (Unit) o;
return Objects.equals(keyWords, unit.keyWords) && return Objects.equals(keyWords, unit.keyWords) &&
Objects.equals(pattern, unit.pattern) && Objects.equals(pattern, unit.pattern) &&

View File

@ -48,4 +48,12 @@ public class UnitPointer {
public int hashCode() { public int hashCode() {
return Objects.hash(entityId, unit); return Objects.hash(entityId, unit);
} }
@Override
public String toString() {
return "UnitPointer{" +
"entityId=" + entityId +
", unit=" + unit +
'}';
}
} }

View File

@ -4,12 +4,19 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/* /*
Возвращает Set слов из текста Возвращает Set слов из текста
*/ */
public class Parser { public class Parser {
private static final Set<String> pretexts = Stream
.of("в", "без", "до", "из", "к", "на", "по", "о", "от", "перед", "при", "с", "у", "за", "над", "об",
"под", "про", "для")
.collect(Collectors.toSet());
private Parser() { private Parser() {
throw new IllegalStateException("Utility Class"); throw new IllegalStateException("Utility Class");
} }
@ -21,6 +28,7 @@ public class Parser {
while (m.find()) { while (m.find()) {
words.add(m.group().toLowerCase()); words.add(m.group().toLowerCase());
} }
words.removeAll(pretexts);
return words; return words;
} }