Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.nio.file.*;
- import java.io.*;
- public class WordStatWords {
- public static void main(String[] args) {
- Map<String, Integer> words = new LinkedHashMap<String, Integer>();
- String word;
- int index;
- try {
- for (String line : Files.readAllLines(Paths.get(args[0]))) {
- index = 0;
- for (int i = 0; i < line.length(); i++) {
- char c = line.charAt(i);
- if (!(Character.isLetter(c) ||
- Character.getType(c) == Character.DASH_PUNCTUATION ||
- c == '\'')) {
- if (index == i) {
- index++;
- } else {
- word = line.substring(index, i).toLowerCase();
- if (words.containsKey(word)) {
- words.put(word, words.get(word) + 1);
- } else {
- words.put(word, 1);
- }
- index = i + 1;
- }
- }
- }
- if (index != line.length()) {
- word = line.substring(index).toLowerCase();
- if (words.containsKey(word)) {
- words.put(word, words.get(word) + 1);
- } else {
- words.put(word, 1);
- }
- }
- }
- PrintWriter writer = new PrintWriter(args[1], "utf8");
- try {
- words.entrySet().stream()
- .sorted(Map.Entry.comparingByKey())
- .forEach(entry -> writer.println(entry.getKey() + " " + entry.getValue()));
- } finally {
- writer.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment