Рефакторинг

This commit is contained in:
upagge 2020-05-01 17:25:16 +03:00
parent f5bf79f441
commit badfe1915f
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
8 changed files with 28 additions and 39 deletions

16
pom.xml
View File

@ -42,18 +42,12 @@
</distributionManagement>
<properties>
<slf4j.ver>1.7.26</slf4j.ver>
<junit.ver>4.12</junit.ver>
<lombok.ver>1.18.6</lombok.ver>
<lombok.ver>1.18.12</lombok.ver>
<slf4j.ver>1.7.30</slf4j.ver>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.ver}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@ -65,6 +59,12 @@
<artifactId>lombok</artifactId>
<version>${lombok.ver}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.ver}</version>
</dependency>
</dependencies>
<scm>

View File

@ -9,6 +9,7 @@ import org.sadtech.autoresponder.entity.Unit;
import org.sadtech.autoresponder.entity.UnitPointer;
import org.sadtech.autoresponder.service.UnitPointerService;
import org.sadtech.autoresponder.util.Description;
import org.sadtech.autoresponder.util.Message;
import org.sadtech.autoresponder.util.Parser;
import java.util.HashSet;
@ -115,10 +116,10 @@ public class AutoResponder<U extends Unit> {
if (keyWords != null && !keyWords.isEmpty()) {
Set<String> temp = new HashSet<>(keyWords);
temp.retainAll(words);
log.info("Ключевые слова юнита: {} ({})", keyWords, keyWords.size());
log.info("Ключевые слова от пользователя: {}", words);
log.info("Пересечение: {} ({})", temp, temp.size());
log.info("Процент: {} Необходимо: {}", (double) temp.size() / (double) keyWords.size() * 100.0, unit.getMatchThreshold());
log.trace(Message.UNIT_KEYWORDS, keyWords, keyWords.size());
log.trace(Message.USER_MESSAGE_KEYWORDS, words);
log.trace(Message.INTERSECTION, temp, temp.size());
log.trace(Message.CROSSING_PERCENTAGE, (double) temp.size() / (double) keyWords.size() * 100.0, unit.getMatchThreshold());
return (double) temp.size() / (double) keyWords.size() * 100.0;
} else {
return 0.0;

View File

@ -12,13 +12,8 @@ import java.util.Comparator;
public class UnitPriorityComparator implements Comparator<Unit> {
@Override
public int compare(Unit o1, Unit o2) {
if (o1.getPriority() < o2.getPriority()) {
return -1;
} else if (o1.getPriority().equals(o2.getPriority())) {
return 0;
}
return 1;
public int compare(Unit unit1, Unit unit2) {
return Integer.compare(unit1.getPriority(), unit2.getPriority());
}
}

View File

@ -6,7 +6,6 @@ import lombok.Setter;
import lombok.ToString;
import org.sadtech.autoresponder.util.Description;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
@ -48,8 +47,8 @@ public abstract class Unit<U extends Unit> {
this.keyWords = keyWords;
this.phrase = phrase;
this.pattern = pattern;
this.matchThreshold = Optional.ofNullable(matchThreshold).orElse(10);
this.priority = Optional.ofNullable(priority).orElse(10);
this.matchThreshold = matchThreshold == null ? 10 : matchThreshold;
this.priority = priority == null ? 10 : matchThreshold;
this.nextUnits = nextUnits;
}

View File

@ -15,14 +15,6 @@ public interface UnitPointerService<U extends Unit> {
void save(@NonNull UnitPointer<U> unitPointer);
/**
* Проверка наличия {@link UnitPointer} для пользователя
*
* @param entityId Идентификатор пользователя
* @return true - если найдено
*/
boolean existsByEntityId(@NonNull Long entityId);
Optional<UnitPointer<U>> getByEntityId(@NonNull Long entityId);
void removeByEntityId(@NonNull Long entityId);

View File

@ -31,9 +31,4 @@ public class UnitPointerServiceImpl<U extends Unit> implements UnitPointerServic
log.trace("Пользователь отправлен в репозиторий");
}
@Override
public boolean existsByEntityId(@NonNull Long entityId) {
return unitPointerRepository.findByEntityId(entityId).isPresent();
}
}

View File

@ -5,9 +5,13 @@ package org.sadtech.autoresponder.util;
*
* @author upagge [22.08.2019]
*/
final class Message {
public final class Message {
static final String UTILITY_CLASS = "Клас утилита";
public static final String UNIT_KEYWORDS = "Ключевые слова юнита: {} ({})";
public static final String USER_MESSAGE_KEYWORDS = "Ключевые слова от пользователя: {}";
public static final String INTERSECTION = "Пересечение: {} ({})";
public static final String CROSSING_PERCENTAGE = "Процент: {} Необходимо: {}";
private Message() {
throw new IllegalStateException(UTILITY_CLASS);

View File

@ -16,15 +16,18 @@ import static org.sadtech.autoresponder.util.Message.UTILITY_CLASS;
public class Parser {
@Description("Множество предлогов")
private static final Set<String> pretexts = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("в", "без", "до", "из", "к", "на", "по", "о", "от", "перед", "при", "с", "у", "за", "над", "об",
"под", "про", "для")));
private static final Set<String> pretexts = Collections.unmodifiableSet(
new HashSet<>(Arrays.asList(
"в", "без", "до", "из", "к", "на", "по", "о", "от", "перед", "при", "с", "у", "за", "над", "об",
"под", "про", "для"
)));
private Parser() {
throw new IllegalStateException(UTILITY_CLASS);
}
/**
* Метод по разбиению строки на множество слов
* Метод по разбиению строки на множество слов.
*
* @param text Строка
* @return Множество слов