Skip to content

Commit

Permalink
Add regex for removing non letters in answers
Browse files Browse the repository at this point in the history
  • Loading branch information
XyL1GaN4eG committed Jul 28, 2024
1 parent cd212f0 commit 6f63922
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static weatherproject.tgbotservice.telegram.UserState.HAVE_SETTED_CITY;
import static weatherproject.tgbotservice.utils.Constants.ILLEGAL_CITY;
import static weatherproject.tgbotservice.utils.Constants.NEW_CITY_SETTED;
import static weatherproject.tgbotservice.utils.RegExUtil.removeNonLettersAndSpaces;

@RequiredArgsConstructor
@Component
Expand Down Expand Up @@ -41,7 +42,7 @@ public String execute(UserDTO user, WeatherDTO weatherDTO) {
}

private String[] weatherDtoToArray(WeatherDTO weatherDTO) {
var translatedWeather = translateClient.translateEngToRussian(weatherDTO.getCity() + ", " + weatherDTO.getCondition()).split(", ");
var translatedWeather = translateClient.translateEngToRussian(removeNonLettersAndSpaces(weatherDTO.getCity()) + ", " + weatherDTO.getCondition()).split(", ");
return new String[]{
translatedWeather[0],
weatherDTO.getTemperature().toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static weatherproject.tgbotservice.utils.Constants.CITY_NOT_FOUND;
import static weatherproject.tgbotservice.utils.Constants.FIRST_CITY_SET;
import static weatherproject.tgbotservice.utils.RegExUtil.removeNonLettersAndSpaces;

@RequiredArgsConstructor
@Component
Expand All @@ -33,7 +34,7 @@ public String execute(UserDTO user, WeatherDTO weatherDTO) {
}

private String[] weatherDtoToArray(WeatherDTO weatherDTO) {
var translatedWeather = translateClient.translateEngToRussian(weatherDTO.getCity() + ", " + weatherDTO.getCondition()).split(", ");
var translatedWeather = translateClient.translateEngToRussian(removeNonLettersAndSpaces(weatherDTO.getCity()) + ", " + weatherDTO.getCondition()).split(", ");
return new String[]{
translatedWeather[0],
weatherDTO.getTemperature().toString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package weatherproject.tgbotservice.utils;

public class RegExUtil {
public static String removeNonLettersAndSpaces(String input) {
// Регулярное выражение для замены всех символов, кроме букв и пробелов
String regex = "[^a-zA-Z ]";

// Замена всех ненужных символов на пустую строку
return input.replaceAll(regex, "");
}
}

0 comments on commit 6f63922

Please sign in to comment.