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

task2 mihailov alex #77

Open
wants to merge 5 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
18 changes: 18 additions & 0 deletions .idea/gradle.xml

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

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

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

11 changes: 11 additions & 0 deletions .idea/libraries/Gradle__com_google_guava_guava_22_0.xml

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

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

11 changes: 11 additions & 0 deletions .idea/libraries/Gradle__org_apache_commons_commons_lang3_3_4.xml

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

11 changes: 11 additions & 0 deletions .idea/libraries/Gradle__org_apiguardian_apiguardian_api_1_0_0.xml

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

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

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

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

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

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

11 changes: 11 additions & 0 deletions .idea/libraries/Gradle__org_opentest4j_opentest4j_1_1_0.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/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.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.

31 changes: 31 additions & 0 deletions IFMO_JAVA_Basics_20182009.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="IFMO_JAVA_Basics_20182009" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/out/production/classes" />
<output-test url="file://$MODULE_DIR$/out/test/classes" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/out" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Gradle: org.apache.commons:commons-lang3:3.4" level="project" />
<orderEntry type="library" name="Gradle: com.google.guava:guava:22.0" level="project" />
<orderEntry type="library" name="Gradle: com.google.code.findbugs:jsr305:1.3.9" level="project" />
<orderEntry type="library" name="Gradle: com.google.errorprone:error_prone_annotations:2.0.18" level="project" />
<orderEntry type="library" name="Gradle: com.google.j2objc:j2objc-annotations:1.1" level="project" />
<orderEntry type="library" name="Gradle: org.codehaus.mojo:animal-sniffer-annotations:1.14" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: org.junit.jupiter:junit-jupiter-api:5.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: org.junit.platform:junit-platform-commons:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: org.apiguardian:apiguardian-api:1.0.0" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: org.opentest4j:opentest4j:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: org.junit.jupiter:junit-jupiter-engine:5.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: org.junit.platform:junit-platform-engine:1.2.0" level="project" />
</component>
</module>
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
67 changes: 54 additions & 13 deletions src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,62 @@
package ru.ifmo.cet.javabasics;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.*;
import java.io.File;


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

// 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 class WarAndPeaceExercise {

throw new UnsupportedOperationException();
public static String warAndPeace() throws IOException {
//считывание в мапу
Scanner in = new Scanner(new File("src/main/resources/WAP12.txt"));
HashMap<String,Integer> vocabulary= new HashMap<>();
readWords(in, vocabulary);
in = new Scanner(new File("src/main/resources/WAP34.txt"));
readWords(in, vocabulary);
//удаление слов<10
for(Iterator<HashMap.Entry<String, Integer>> it=vocabulary.entrySet().iterator();it.hasNext();) {
HashMap.Entry<String, Integer> entry = it.next();
if (entry.getValue() < 10) {
it.remove();
}
}
ArrayList<String> res_array= new ArrayList<>();
while (!vocabulary.isEmpty()){
HashMap.Entry<String, Integer> max=vocabulary.entrySet().iterator().next();
for (Map.Entry<String, Integer> pair : vocabulary.entrySet()) {
if (max.getValue() < pair.getValue()) {
max = pair;
} else if (max.getValue().equals(pair.getValue())) {
if (max.getKey().compareTo(pair.getKey()) > 0) {
max = pair;
}
}
}
String string=max.getKey()+ " - "+max.getValue();
res_array.add(string);
vocabulary.remove(max.getKey());
}
String res_string="";
for(String s:res_array) {
res_string=s.concat("\n");
}
res_string = res_string.substring(0, res_string.length() - 1);
return res_string;
}
public static void readWords(Scanner in, HashMap<String, Integer> vocabulary) {
while(in.hasNext()){
String[] words= (in.nextLine().toLowerCase().replaceAll("[^a-z\u0430-\u044f]", " ")).split(" ");
for (String word:words) {
if(word.length()<4) continue;
Integer value=vocabulary.get(word);
if (vocabulary.get(word) == null) {
vocabulary.put(word,1);
}else{
vocabulary.put(word,++value);
}
}
}
}

}