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 Kalenik Igor task-2 #111

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
43 changes: 36 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,50 @@
package ru.ifmo.cet.javabasics;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.*;


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
Map<String, Integer> txtMap = new TreeMap();
List<String> linetome12 = Files.readAllLines(tome12Path, Charset.forName("windows-1251"));
List<String> linetome34 = Files.readAllLines(tome34Path, Charset.forName("windows-1251"));
ArrayList<String> txtLines = new ArrayList<>();
txtLines.addAll(linetome12);
txtLines.addAll(linetome34);

throw new UnsupportedOperationException();
for(String i : txtLines){
String lines = i.toLowerCase().replaceAll("[^а-яa-z]", " ");
for(String txtWord : lines.split(" ")){
if(txtWord.length()>=4){
if(txtMap.containsKey(txtWord)) txtMap.put(txtWord,txtMap.get(txtWord)+1);
else txtMap.put(txtWord,1);
}
}
}
return wordsSort(txtMap);
}
public static String wordsSort(Map<String,Integer> txtMap){
List<Map.Entry<String,Integer>> words = new ArrayList<Map.Entry<String, Integer>>(txtMap.entrySet());
Collections.sort(words,new Comparator<Map.Entry<String,Integer>>(){
public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2){
if(o1.getValue().equals(o2.getValue())) return o1.getKey().compareTo(o2.getKey());
else return -(o1.getValue()).compareTo(o2.getValue());
}
});
String result = "";
for(Map.Entry<String,Integer> k : words){
if(k.getValue()>=10) result += k.getKey()+" - "+k.getValue()+"\n";
}
return result.trim();
}

}