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

usacheva_p3222_task2 #97

Open
wants to merge 2 commits into
base: task-2
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
62 changes: 53 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,65 @@
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 static java.nio.file.Files.readAllLines;

public class WarAndPeaceExercise {

public static String warAndPeace() {
final Path tome12Path = Paths.get("src", "main", "resources", "WAP12.txt");
final Path tome34Path = Paths.get("src", "main", "resources", "WAP34.txt");
private static class Node implements Comparable<Node>{

private String key;
private Integer value;

// TODO map lowercased words to its amount in text and concatenate its entries.
// TODO If 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 if needed.
// TODO Also omit any word with lengths less than 4 and frequency less than 10
public Node(String key, Integer value) {
this.key = key;
this.value = value;
}

throw new UnsupportedOperationException();
@Override
public int compareTo(Node node) {
if(node.value.equals(this.value)) return this.key.compareTo(node.key);
return node.value - this.value;
}
}

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");
HashMap<String, Integer> map = new HashMap<>();
Charset set = Charset.forName("windows-1251");
List<String> lines = readAllLines(tome12Path, set);
lines.addAll(readAllLines(tome34Path, set));
for (String line : lines) {
line = line.replaceAll("[^a-zA-Zа-яА-Я]", " ");
for (String word : line.split(" ")) {
if (word.length() >= 4) {
word = word.toLowerCase();
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
} else {
map.put(word, 1);
}
}
}
}
ArrayList<Node> nodes = new ArrayList<>();
for (Map.Entry<String, Integer> entry : map.entrySet()){
nodes.add(new Node(entry.getKey(), entry.getValue()));
}
StringBuilder res = new StringBuilder();
Collections.sort(nodes);
for (Node node : nodes){
if(node.value >= 10) {
res.append(node.key).append(" - ").append(node.value).append("\n");
}
}
return res.toString().substring(0, res.length()-1);
}

}