Hello, tell me how to count all the words from this file with a poem? That is, to get all the words without characters like #t_es1385_1 , numeric characters, as well as words containing punctuation marks.

Tried to do so:

  Files.lines(Paths.get(ReadFile.class.getResource(path).toURI()), StandardCharsets.UTF_8) .map(s -> s.split("([^а-я А-Я]|[\\d\\w\\s])+")) .flatMap(Arrays::stream) .filter(s -> !s.isEmpty()) .collect(Collectors.toList()); 

Did not work out. Below is the poem itself:

 "Вот уж вечер. Роса…" Вот уж вечер. Роса: #t_es1385_1 Блестит на крапиве. Я стою у дороги, Прислонившись к иве. От луны свет большой Прямо на нашу крышу. Где-то песнь соловья Вдалеке я слышу. Хорошо и тепло, Как зимой у печки. И березы стоят, Как большие свечки. И вдали за рекой, Видно, за опушкой, Сонный сторож стучит Мертвой колотушкой. 1910 "Там, где капустные грядки…" Там, где капустные грядки: #t_es1385_2 
  • did like this: Files.lines (Paths.get (path), StandardCharsets.UTF_8). map (s -> s.split ("([^ а-я А-Я] | [\\ d] | [\\ w] | [\\ s]) + ")) .flatMap (Arrays :: stream) .filter (s ->! s.isEmpty ()) .collect (Collectors.toList ()); it is right? - Maxim Savelyev

1 answer 1

 Files.lines(Paths.get("test.txt"), StandardCharsets.UTF_8) .map(s -> s.split(" ")) .flatMap(Arrays::stream) .filter(s -> !s.isEmpty()) .filter(s -> !s.startsWith("#")) .filter(s -> !s.matches("\\d+")) .map(s -> s.replaceAll("\\p{IsPunctuation}", "")) .collect(Collectors.toList()); 
  • Output like this "81968196819681968196819681968196Wallout" - Maxim Savelyev
  • I did this: Files.lines (Paths.get (ReadFile.class.getResource (path) .toURI ()), StandardCharsets.UTF_8) .map (s -> s.split ("([^ а-я А I] | [\\ d \\ w \\ s]) + ")) .flatMap (Arrays :: stream) .filter (s ->! S.isEmpty ()) .collect (Collectors.toList ()); It is not right? - Maxim Savelyev
  • I do not see a way to get this result on your input. - Sergey Gornostaev
  • If solves your problem, then correctly. - Sergey Gornostaev