Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P3123 Popov Egor task-3 #100

Open
wants to merge 1 commit into
base: task-3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package ru.ifmo.cet.javabasics;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.nio.file.Path;
import java.nio.file.Paths;


public class WarAndPeaceExercise {

public static String warAndPeace() {
public static String warAndPeace() throws IOException{
final Path tome12Path = Paths.get("src", "main", "resources", "WAP12.txt");
final Path tome34Path = Paths.get("src", "main", "resources", "WAP34.txt");

Expand All @@ -15,7 +22,30 @@ public static String warAndPeace() {
// TODO Entries in final String should be also sorted by amount and then in alphabetical order iff needed.
// TODO Also omit any word with lengths less than 4 and frequency less than 10

throw new UnsupportedOperationException();
}
Stream<String> textInLines = Stream.concat(Files.lines(tome12Path.toAbsolutePath(), Charset.forName("windows-1251")), Files.lines(tome34Path.toAbsolutePath(), Charset.forName("windows-1251")));

StringBuilder end = new StringBuilder();

textInLines.map(String::toLowerCase).map(lines -> {
return lines.replaceAll("[^а-яa-z]", " ");
})
.flatMap(lines -> {
return Arrays.stream(lines.split(" "));
})
.filter(word -> {
return word.length() > 3;
})
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()))
.entrySet()
.stream()
.filter(word -> {
return word.getValue() >= 10;
})
.sorted((o1, o2) -> {
return (o1.getValue().compareTo(o2.getValue()) == 0) ? o1.getKey().compareTo(o2.getKey()) : -(o1.getValue()).compareTo(o2.getValue());
})
.forEach(word -> end.append(word.getKey()).append(" - ").append(word.getValue()).append("\n"));

return end.toString().trim();
}
}