Skip to content
Snippets Groups Projects
Commit aff804db authored by Amaury Vanoorenberghe's avatar Amaury Vanoorenberghe :scroll:
Browse files

Q1.1 / Q1.2 / Q1.3 - Ajout du comptage des mots

parent 06ee77a2
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="src"/> <classpathentry including="**/*.java" kind="src" output="target/classes" path="src">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <attributes>
<classpathentry kind="output" path="bin"/> <attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>
/bin/ /bin/
.settings/ .settings/
/targets/ /targets/
/target/
...@@ -10,8 +10,14 @@ ...@@ -10,8 +10,14 @@
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
</natures> </natures>
</projectDescription> </projectDescription>
pom.xml 0 → 100644
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TP4</groupId>
<artifactId>TP4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package tp4.ex1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String file = Resource.readtext("file.txt");
Map<String, Integer> words = WordCounter.countAll(file);
List<Map.Entry<String, Integer>> entries = new ArrayList<>(words.entrySet());
Comparator<Map.Entry<String, Integer>> entryComparator = new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
return Integer.compare(b.getValue(), a.getValue());
};
};
Collections.sort(entries, entryComparator);
for (Map.Entry<String, Integer> entry : entries.stream().limit(5).collect(Collectors.toList())) {
printCount(words, entry.getKey());
}
}
private static void printCount(Map<String, Integer> words, String word) {
System.out.println(String.format("%s : %d", word, words.get(word)));
}
@SuppressWarnings("unused")
private static void printContains(Map<String, Integer> words, String word) {
for (Map.Entry<String, Integer> entry : words.entrySet()) {
if (entry.getKey().contains(word)) {
System.out.println(entry);
}
}
}
}
package tp4.ex1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class Resource {
public static String readtext(String resourceName) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(Resource.class.getResourceAsStream(resourceName), StandardCharsets.UTF_8))) {
StringBuilder sb = new StringBuilder();
if (reader.ready()) {
sb.append(reader.readLine());
}
while (reader.ready()) {
sb.append(String.format("\n%s", reader.readLine()));
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
package tp4.ex1;
import java.util.regex.Pattern;
import java.util.Map;
import java.util.HashMap;
public class WordCounter {
private static final String WHITESPACE = "\\p{Blank}|\\p{Space}|\s";
private static final String REGEX = WHITESPACE + "|\\p{Punct}|’";
private static final Pattern PATTERN = Pattern.compile(REGEX);
public static Map<String, Integer> countAll(String input) {
String[] words = PATTERN.split(input.toLowerCase());
Map<String, Integer> wordCount = new HashMap<String, Integer>();
for (String word : words) {
if (word.length() > 0) {
Integer count = wordCount.get(word);
wordCount.put(word, count == null ? 1 : count + 1);
}
}
return wordCount;
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment