Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. package hw3;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Map;
  9. public class WC {
  10. public static void main(String[] args) throws FileNotFoundException {
  11.  
  12. File f = new File(System.getProperty("user.dir") + "\\t.txt");
  13. Scanner sc = new Scanner(f);
  14.  
  15. int line_ct = 0;
  16. int word_ct = 0;
  17. int char_ct = 0;
  18. int numb_ct = 0;
  19.  
  20. ArrayList<String> total_words = new ArrayList<String>();
  21. ArrayList<String> total_nums = new ArrayList<String>();
  22.  
  23. // while there are new tokens in the scanner
  24. while (sc.hasNext()) {
  25. String s_line = sc.nextLine(); // each line in String format
  26. line_ct++;
  27. while(s_line.isEmpty()) { // if the line is empty, move scanner next line
  28. s_line = sc.nextLine();
  29. }
  30. // line_ar[] is our LINE split on " ", line_ar[i] contains each word in a line
  31. String line_ar[] = s_line.split("\\s+");
  32.  
  33. for(int i = 0; i < line_ar.length; i++) { //iterating through each words[]
  34.  
  35. if(!line_ar[i].isEmpty()) {
  36. // put each word into a character array
  37. char[] chars = line_ar[i].toCharArray();
  38.  
  39. //System.out.print(chars);
  40.  
  41. if(Character.isLetter(chars[0])) { //if first char of chars[] is letter, increase word count
  42. word_ct++;
  43. total_words.add(line_ar[i].toLowerCase()); // append word to total_words
  44. }
  45. else if(Character.isDigit(chars[0])) { // if first char if chars[] is digit, increase num count
  46. numb_ct++;
  47. total_nums.add(line_ar[i]); // append number to total_nums
  48. }
  49.  
  50. for(int b = 0; b < chars.length; b++) { // count each character in every word
  51. if(Character.isLetter(chars[b]) || Character.isDigit(chars[b])) { char_ct++; }
  52. }
  53.  
  54. }
  55.  
  56. }
  57.  
  58.  
  59. }
  60. ArrayList<String> total_char = new ArrayList<String>();
  61. // make total word array all lower case
  62. List<String> total_words_lower = total_words.stream()
  63. .map(String::toLowerCase)
  64. .collect(Collectors.toList());
  65.  
  66.  
  67. for(int i = 0; i < total_words.size(); i++) {
  68. char current_word[] = total_words.get(i).toCharArray();
  69. for(char c : current_word) {
  70. total_char.add(Character.toString(c)); // add each character to an array
  71. }
  72. }
  73.  
  74.  
  75. System.out.println("Line Count: " + line_ct);
  76. System.out.println("Word Count: " + word_ct);
  77. System.out.println("Char Count: " + char_ct);
  78. System.out.println("Num Count: " + numb_ct + "\n");
  79.  
  80. // COUNT TOTAL OCCURANCES -> put into Map<String, Long>
  81. Map<String, Long> counts_words =
  82. total_words_lower.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));
  83.  
  84.  
  85. Map<String, Long> counts_chars =
  86. total_char.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));
  87.  
  88. Map<String, Long> counts_nums =
  89. total_nums.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));
  90.  
  91.  
  92. //System.out.println(counts_words);
  93. //System.out.println(counts_chars);
  94. //System.out.println(counts_nums);
  95.  
  96. // take just the keys of every Map (a unique set of every word/num/char) used to index through the Map
  97. String[] word_keys = counts_words.keySet().toArray(new String[counts_words.size()]);
  98. String[] num_keys = counts_nums.keySet().toArray(new String[counts_nums.size()]);
  99. String[] char_keys = counts_chars.keySet().toArray(new String[counts_chars.size()]);
  100. int j = 1;
  101. int k = 0;
  102.  
  103. Long max = counts_words.get(word_keys[0]);
  104.  
  105. //find 5 max occurance words
  106. for(int z = 0; z < 5; z++) {
  107.  
  108. while(j < counts_words.size()) {
  109. // if the value of that word > max
  110. if(counts_words.get(word_keys[j]) > Long.valueOf(max)) {
  111. k = j;
  112. max = counts_words.get(word_keys[j]);
  113. }
  114. j++;
  115. }
  116. System.out.println(word_keys[k] + " occurs " + max + " times");
  117. // set frequency to 0
  118. counts_words.put(word_keys[k], Long.valueOf(0));
  119. // set max to 0
  120. max = Long.valueOf(0);
  121. // j = 1, k = 0
  122. j = 1; k = 0;
  123. }
  124. System.out.println();
  125.  
  126. //find 5 max occurance numbers
  127. max = counts_nums.get(num_keys[0]);
  128. for(int z = 0; z < 5; z++) {
  129.  
  130. while(j < counts_nums.size()) {
  131. // if the value of that word > max
  132. if(counts_nums.get(num_keys[j]) > Long.valueOf(max)) {
  133. k = j;
  134. max = counts_nums.get(num_keys[j]);
  135. }
  136. j++;
  137. }
  138. System.out.println(num_keys[k] + " occurs " + max + " times");
  139.  
  140. counts_nums.put(num_keys[k], Long.valueOf(0));
  141.  
  142. max = Long.valueOf(0);
  143.  
  144. j = 1; k = 0;
  145. }
  146. System.out.println();
  147. // find 5 max occurance characters
  148. max = counts_chars.get(char_keys[0]);
  149. for(int z = 0; z < 5; z++) {
  150.  
  151. while(j < counts_chars.size()) {
  152. // if the value of that word > max
  153. if(counts_chars.get(char_keys[j]) > Long.valueOf(max)) {
  154. k = j;
  155. max = counts_chars.get(char_keys[j]);
  156. }
  157. j++;
  158. }
  159. System.out.println(char_keys[k] + " occurs " + max + " times");
  160.  
  161. counts_chars.put(char_keys[k], Long.valueOf(0));
  162.  
  163. max = Long.valueOf(0);
  164.  
  165. j = 1; k = 0;
  166. }
  167.  
  168.  
  169. sc.close();
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement