Jestem całkiem nowy w Javie i obecnie utknąłem i nie wiem, jak postępować.
Chcę tylko sprawdzić, czy ciąg zawiera jakieś słowa z listy słów, a jeśli tak, to wypisać je.
W moim przypadku wszystkie ciągi będą miały podobny tekst, jak ten (przykład z 5 minutami):
Set timer to five minutes
Albo ten:
Timer five minutes
To jest mój obecny kod z kilkoma komentarzami, co próbuję zrobić:
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args)
{
String example = Set timer to five minutes
Stream<String> stream = Stream.of(("Timer", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten") //The stream/array would be much bigger since I want to cover every number till 200
//Now I thought I can use stream filter to check if example contains the word Timer and any of the words of the Stream and if it does I want to use the output to trigger something else
if(stream.filter(example -> example .contains(NOTSUREWHATTOPUTHERE))) {
//If detected that String contains Timer and Number, then create timer
}
}
Czy ktoś może mi udzielić porady / pomocy?
Pozdrowienia
1 odpowiedź
Możesz to zrobić w ten sposób:
String[] words = { "Timer", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
String example = "Set timer to five minutes";
String exLower = example.toLowerCase();
if (Stream.of(words).anyMatch(word -> exLower.contains(word.toLowerCase()))) {
//
}
Ten kod przynajmniej sprawdzi się poprawnie, nawet jeśli słowa mają różne duże / małe litery, ale nie powiedzie się, jeśli tekst zawiera słowo osadzone w innym słowie, np. tekst "stone"
będzie pasował, ponieważ znaleziono "one"
.
Aby to naprawić, „najłatwiej” byłoby przekonwertować listę słów na wyrażenie regularne.
String[] words = { "Timer", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
String example = "Set timer to five minutes";
String regex = Stream.of(words).map(Pattern::quote)
.collect(Collectors.joining("|", "(?i)\\b(?:", ")\\b"));
if (Pattern.compile(regex).matcher(example).find()) {
//
}
split
i nie zebrać słów toSet
, a następnie wykonać zawiera dla któregokolwiek z nich pod podanym list
?
Timer
jako jedynym słowem na liście i raz ze wszystkimi liczbami na liście słów.
Podobne pytania
Nowe pytania
java
Java to język programowania wysokiego poziomu. Użyj tego tagu, jeśli masz problemy z używaniem lub zrozumieniem samego języka. Ten tag jest rzadko używany samodzielnie i jest najczęściej używany w połączeniu z [spring], [spring-boot], [jakarta-ee], [android], [javafx], [hadoop], [gradle] i [maven].
@Builder class Action { String type; String value; }
używanego dalej wString example = "Set timer to five minutes"; Set<String> words = Arrays.stream(example.split(" ")).collect(Collectors.toSet()); Set<String> actions = Set.of("Timer", "Music"); Set<String> values = Set.of("One", "Two", "Three",...); Action.ActionBuilder actionBuilder = Action.builder(); actions.stream().filter(words::contains).findFirst().ifPresent(s -> { actionBuilder.type(s); values.stream().filter(words::contains).findFirst().ifPresent(actionBuilder::value); });//Use action further }
.