Advertisement
Guest User

1

a guest
Oct 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5.  
  6. public class WordStatWords {
  7. public static void main(String[] args) throws Exception {
  8. Map<String, Integer> wordsAndCnt = new TreeMap<>();
  9. String regexp = "[\\p{L}\\p{Pd}']+";
  10. Pattern pattern = Pattern.compile(regexp);
  11. try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"))) {
  12. String line;
  13. while ((line = reader.readLine()) != null) {
  14. line = line.toLowerCase();
  15. Matcher matcher = pattern.matcher(line);
  16. while (matcher.find()) {
  17. String word = matcher.group();
  18. wordsAndCnt.putIfAbsent(word, 0);
  19. wordsAndCnt.put(word, wordsAndCnt.get(word) + 1);
  20. }
  21. }
  22. } catch (FileNotFoundException e) {
  23. System.err.println("File does not exsist");
  24. } catch (ArrayIndexOutOfBoundsException e) {
  25. System.err.println("Enter the name of the file");
  26. }
  27. try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1]), "UTF-8"))) {
  28. for (Map.Entry e : wordsAndCnt.entrySet()) {
  29. writer.write(e.getKey() + " " + e.getValue() + "\r\n");
  30. }
  31. } catch (FileNotFoundException e) {
  32. System.err.println("File does not exsist");
  33. }
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement