Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hw3;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- public class WC {
- public static void main(String[] args) throws FileNotFoundException {
- File f = new File(System.getProperty("user.dir") + "\\t.txt");
- Scanner sc = new Scanner(f);
- int line_ct = 0;
- int word_ct = 0;
- int char_ct = 0;
- int numb_ct = 0;
- ArrayList<String> total_words = new ArrayList<String>();
- ArrayList<String> total_nums = new ArrayList<String>();
- // while there are new tokens in the scanner
- while (sc.hasNext()) {
- String s_line = sc.nextLine(); // each line in String format
- line_ct++;
- while(s_line.isEmpty()) { // if the line is empty, move scanner next line
- s_line = sc.nextLine();
- }
- // line_ar[] is our LINE split on " ", line_ar[i] contains each word in a line
- String line_ar[] = s_line.split("\\s+");
- for(int i = 0; i < line_ar.length; i++) { //iterating through each words[]
- if(!line_ar[i].isEmpty()) {
- // put each word into a character array
- char[] chars = line_ar[i].toCharArray();
- //System.out.print(chars);
- if(Character.isLetter(chars[0])) { //if first char of chars[] is letter, increase word count
- word_ct++;
- total_words.add(line_ar[i].toLowerCase()); // append word to total_words
- }
- else if(Character.isDigit(chars[0])) { // if first char if chars[] is digit, increase num count
- numb_ct++;
- total_nums.add(line_ar[i]); // append number to total_nums
- }
- for(int b = 0; b < chars.length; b++) { // count each character in every word
- if(Character.isLetter(chars[b]) || Character.isDigit(chars[b])) { char_ct++; }
- }
- }
- }
- }
- ArrayList<String> total_char = new ArrayList<String>();
- // make total word array all lower case
- List<String> total_words_lower = total_words.stream()
- .map(String::toLowerCase)
- .collect(Collectors.toList());
- for(int i = 0; i < total_words.size(); i++) {
- char current_word[] = total_words.get(i).toCharArray();
- for(char c : current_word) {
- total_char.add(Character.toString(c)); // add each character to an array
- }
- }
- System.out.println("Line Count: " + line_ct);
- System.out.println("Word Count: " + word_ct);
- System.out.println("Char Count: " + char_ct);
- System.out.println("Num Count: " + numb_ct + "\n");
- // COUNT TOTAL OCCURANCES -> put into Map<String, Long>
- Map<String, Long> counts_words =
- total_words_lower.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));
- Map<String, Long> counts_chars =
- total_char.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));
- Map<String, Long> counts_nums =
- total_nums.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));
- //System.out.println(counts_words);
- //System.out.println(counts_chars);
- //System.out.println(counts_nums);
- // take just the keys of every Map (a unique set of every word/num/char) used to index through the Map
- String[] word_keys = counts_words.keySet().toArray(new String[counts_words.size()]);
- String[] num_keys = counts_nums.keySet().toArray(new String[counts_nums.size()]);
- String[] char_keys = counts_chars.keySet().toArray(new String[counts_chars.size()]);
- int j = 1;
- int k = 0;
- Long max = counts_words.get(word_keys[0]);
- //find 5 max occurance words
- for(int z = 0; z < 5; z++) {
- while(j < counts_words.size()) {
- // if the value of that word > max
- if(counts_words.get(word_keys[j]) > Long.valueOf(max)) {
- k = j;
- max = counts_words.get(word_keys[j]);
- }
- j++;
- }
- System.out.println(word_keys[k] + " occurs " + max + " times");
- // set frequency to 0
- counts_words.put(word_keys[k], Long.valueOf(0));
- // set max to 0
- max = Long.valueOf(0);
- // j = 1, k = 0
- j = 1; k = 0;
- }
- System.out.println();
- //find 5 max occurance numbers
- max = counts_nums.get(num_keys[0]);
- for(int z = 0; z < 5; z++) {
- while(j < counts_nums.size()) {
- // if the value of that word > max
- if(counts_nums.get(num_keys[j]) > Long.valueOf(max)) {
- k = j;
- max = counts_nums.get(num_keys[j]);
- }
- j++;
- }
- System.out.println(num_keys[k] + " occurs " + max + " times");
- counts_nums.put(num_keys[k], Long.valueOf(0));
- max = Long.valueOf(0);
- j = 1; k = 0;
- }
- System.out.println();
- // find 5 max occurance characters
- max = counts_chars.get(char_keys[0]);
- for(int z = 0; z < 5; z++) {
- while(j < counts_chars.size()) {
- // if the value of that word > max
- if(counts_chars.get(char_keys[j]) > Long.valueOf(max)) {
- k = j;
- max = counts_chars.get(char_keys[j]);
- }
- j++;
- }
- System.out.println(char_keys[k] + " occurs " + max + " times");
- counts_chars.put(char_keys[k], Long.valueOf(0));
- max = Long.valueOf(0);
- j = 1; k = 0;
- }
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement