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

Никита Кулин P3221 Task3 #88

Open
wants to merge 1 commit into
base: task-3
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/main/java/ru/ifmo/cet/javabasics/WAPResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class WAPResult {
public final String result;

public WAPResult() throws IOException {

result = readAllLines(Paths.get("src", "main","resources", "WAPResult.txt")).stream().collect(Collectors.joining("\n"));


}
}
27 changes: 20 additions & 7 deletions src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
package ru.ifmo.cet.javabasics;

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

import static java.nio.file.Files.readAllLines;


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");

// 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
String txt = readAllLines(tome12Path, Charset.forName("windows-1251")).stream().collect(Collectors.joining(" "));
txt += readAllLines(tome34Path, Charset.forName("windows-1251")).stream().collect(Collectors.joining(" "));
txt = txt.replaceAll("[^a-zA-Zа-яА-Я]", " ").toLowerCase();

throw new UnsupportedOperationException();
}
Map<String, Integer> dict = new HashMap<>();
Stream<String> textStr = Stream.of(txt.split(" "));
textStr.forEach((String key) -> dict.put(key, dict.containsKey(key) ? dict.get(key) + 1 : 1));

List<Map.Entry<String, Integer>> sortedDictionary = new ArrayList<>(dict.entrySet());
Collections.sort(sortedDictionary, (o1, o2) -> (o1.getValue().equals(o2.getValue())) ? o1.getKey().compareTo(o2.getKey()) : o2.getValue().compareTo(o1.getValue()));
return sortedDictionary.stream().filter(m -> (m.getKey().length() >= 4) && (m.getValue() >= 10))
.map(m -> m.getKey() + " - " + m.getValue()).collect(Collectors.joining("\n"));
}
}