| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.nio.file.Files;
- import java.nio.file.DirectoryStream;
- import java.util.HashMap;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- public class WordCount {
- public static void main(String[] args) throws Exception {
- HashMap<String, Integer> record = new HashMap<String, Integer>(); // store count in map
- String path = "../testdata";
- Path dataDir = Paths.get(path); // get directory info
- DirectoryStream<Path> files = Files.newDirectoryStream(dataDir);
- for (Path file : files) {
- if (Files.isDirectory(file)) {
- continue;
- }
- String text = new String(Files.readAllBytes(dataDir.resolve(file.getFileName()))); // read file content
- String word = new String("");
- for (int i = 0; i < text.length(); i++) {
- if (Character.isLetter(text.charAt(i)) || word.length() > 0 && text.charAt(i) == '\'') {
- word += Character.toLowerCase(text.charAt(i)); // extend word
- } else {
- for (; word.length() > 0 && word.charAt(word.length() - 1) == '\'';) {
- word = word.substring(0, word.length() - 1); // remove trailing quotation mark
- }
- if (word.length() > 0) {
- record.put(word, record.getOrDefault(word, 0) + 1); // update map
- word = "";
- }
- }
- }
- for (; word.length() > 0 && word.charAt(word.length() - 1) == '\'';) {
- word = word.substring(0, word.length() - 1); // remove trailing quotation mark
- }
- if (word.length() > 0) {
- record.put(word, record.getOrDefault(word, 0) + 1); // update map
- }
- }
- ArrayList<HashMap.Entry<String, Integer>> list = new ArrayList<HashMap.Entry<String, Integer>>(record.entrySet()); // get answer from map
- Collections.sort(list, new Comparator<HashMap.Entry<String, Integer>>() {
- public int compare(HashMap.Entry<String, Integer> o1, HashMap.Entry<String, Integer> o2) { // compare function
- if (o1.getValue().equals(o2.getValue())) {
- return o1.getKey().compareTo(o2.getKey());
- }
- return o1.getValue() < o2.getValue() ? 1 : -1;
- }
- });
- for (HashMap.Entry<String, Integer> itr : list) {
- System.out.printf("%s %d\n", itr.getKey(), itr.getValue());
- }
- }
- }
|