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 Lomonosov Alexander Task-2 #105

Open
wants to merge 3 commits into
base: task-2
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
83 changes: 69 additions & 14 deletions src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,76 @@
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() {
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 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

throw new UnsupportedOperationException();

List<String> linesList = new ArrayList<>();
linesList.addAll(readAllLines(tome12Path, Charset.forName("windows-1251")));
linesList.addAll(readAllLines(tome34Path, Charset.forName("windows-1251")));
List<String> words = new ArrayList<>();
for(String In : linesList) {
String[] tmp = In.split("[\\s,.[0-9]«»()?!\\]\\[;:'\\“\"…„]+");
words.addAll(Arrays.asList(tmp));
}
Map<String, Integer> map = new HashMap<>();
for (String word : words) {
word = word.toLowerCase();
if (word.length() >= 4) {
if (map.keySet().contains(word)) { map.put(word, map.get(word) + 1); }
else { map.put(word, 1); }
}
}
SortedSet<KeyValuePair> set = new TreeSet<>();
for(Map.Entry<String, Integer> item : new ArrayList<>(map.entrySet())) {
set.add(new KeyValuePair(item.getValue(),item.getKey()));
}

StringBuilder builder = new StringBuilder();
for(KeyValuePair p : set) {
if(p.getValue() >= 10) builder.append(p.toString());
}
return builder.substring(0,builder.length()-1);
}

}
}
class KeyValuePair implements Comparable<KeyValuePair>{
private String key;
private int value;

KeyValuePair(int value, String key) {
super();
this.key = key;
this.value = value;
}

public int getValue() {
return value;
}

@Override
public String toString()
{
return key + " - " + value + "\n";
}

@Override
public int compareTo(KeyValuePair o) {
if(value != o.value)
{
return o.value - value;
}
else
{
return key.compareTo(o.key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ void testNoLambdas() throws IOException {
assertFalse(source.contains("stream"));

}
}
}