Advertisement
Guest User

WordStatWords

a guest
Oct 17th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.*;
  3. import java.io.*;
  4.  
  5. public class WordStatWords {
  6. public static void main(String[] args) throws Exception {
  7. Map <String, Integer> wordsAndCnt = new TreeMap<>();
  8. ArrayList <String> lines = new ArrayList<>();
  9. ArrayList <String> words = new ArrayList<>();
  10. try {
  11. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"));
  12. String line;
  13. while ((line=reader.readLine()) != null) {
  14. lines.add(line);
  15. }
  16. reader.close();
  17. } catch (FileNotFoundException e){
  18. System.out.print("File does not exsist");
  19. }
  20. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1]), "UTF-8"));
  21. for (int i = 0; i < lines.size(); i++) {
  22. String line = lines.get(i);
  23. line = line.toLowerCase();
  24. line = line.replaceAll("[^\\p{L}\\p{Pd} ']", " ");
  25. String[] arrWords = line.split("\\s+");
  26. for (int j = 0; j < arrWords.length; j++) {
  27. if (arrWords[j]!= null && !arrWords[j].isEmpty()){
  28. words.add(arrWords[j]);
  29. }
  30. }
  31. }
  32. for (int i = 0; i < words.size(); i++) {
  33. if (wordsAndCnt.containsKey(words.get(i))) {
  34. int x = wordsAndCnt.get(words.get(i));
  35. wordsAndCnt.put(words.get(i), x+1);
  36. } else {
  37. wordsAndCnt.put(words.get(i), 1);
  38. }
  39. }
  40. try {
  41. for (Map.Entry e : wordsAndCnt.entrySet()) {
  42. writer.write(e.getKey() + " " + e.getValue() + "\r\n");
  43. }
  44. writer.close();
  45. } catch (FileNotFoundException e) {
  46. System.out.print("File does not exsist");
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement