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

P3121 Miloradov Anton task-3 #84

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
43 changes: 40 additions & 3 deletions src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,57 @@

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

public class WarAndPeaceExercise
{

public class WarAndPeaceExercise {
public static String warAndPeace() throws IOException
{

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



// TODO map lowercased words to its amount in text and concatenate its entries.
// TODO Iff word "котик" occurred in text 23 times then its entry would be "котик - 23\n".

// 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> strngTf = Files.lines(tome34Path.toAbsolutePath(), Charset.forName("windows-1251"));

Stream<String> strngTwlv = Files.lines(tome12Path.toAbsolutePath(), Charset.forName("windows-1251"));

Stream<String> strng = Stream.concat(strngTwlv,strngTf);


StringBuilder Result = new StringBuilder();

strng.map(String::toLowerCase).map(lines->lines.replaceAll("[^a-zа-я]"," "))

.flatMap(lines -> Arrays.stream(lines.split(" ")))
.filter(word -> word.length() > 3)

.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()))
.entrySet()

.stream()
.sorted((o1, o2) ->o1.getValue().equals(o2.getValue()) ? o1.getKey().compareTo(o2.getKey()) : -(o1.getValue()).compareTo(o2.getValue()))
.filter(word-> word.getValue()>=10)

.forEach(word->Result.append(word.getKey()).append(" - ").append(word.getValue()).append("\n"));


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

}