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

Task 3 Alexey Michael #75

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
33 changes: 24 additions & 9 deletions src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
package ru.ifmo.cet.javabasics;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.util.*;
import java.util.stream.Collectors;

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");
final Charset charset = Charset.forName("windows-1251");
List<String> string_list = Files.readAllLines(tome12Path, charset);
string_list.addAll(Files.readAllLines(tome34Path, charset));
List<String> words_list = new ArrayList<>();
string_list.stream().map(line -> line.toLowerCase()).map(lowLine -> lowLine.replaceAll("[^a-z\u0430-\u044f]", " ")).
map(lowReplaceLine -> Arrays.stream(lowReplaceLine.split(" ")).filter(word->word.length()>=4).collect(Collectors.toList())).forEach(c -> words_list.addAll(c));

// 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
Map<String,Integer> vocabulary=new LinkedHashMap<>();
words_list.forEach(key -> {
vocabulary.put(key, vocabulary.containsKey(key) ? vocabulary.get(key) + 1 : 1);
});
vocabulary.entrySet().removeIf(pair->pair.getValue()<10);

throw new UnsupportedOperationException();
}
List<Map.Entry<String, Integer>> sorted_vocabulary = new ArrayList(vocabulary.entrySet());
sorted_vocabulary.sort(Map.Entry.comparingByKey());
sorted_vocabulary.sort(Map.Entry.<String, Integer>comparingByValue().reversed());

}
String res_answer=sorted_vocabulary.stream().map(entry->entry.getKey() + " - " + entry.getValue()).collect(Collectors.joining("\n"));
return res_answer;
}
}