From ba2085807669880224a916db2b68696e0b64276c Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Sun, 19 Feb 2023 16:57:51 -0800 Subject: [PATCH 1/5] word frequency method added --- .idea/vcs.xml | 6 ++++++ src/WordFrequency.java | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/WordFrequency.java b/src/WordFrequency.java index 2f82ee4..5f4672e 100644 --- a/src/WordFrequency.java +++ b/src/WordFrequency.java @@ -1,5 +1,44 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + public class WordFrequency { + public static void wordFrequency(String fileName) { + Map wordMap = new HashMap<>(); + + + try (Scanner s = new Scanner(new File(fileName))) { + while (s.hasNext()) { + String current = s.next(); + + if(wordMap.containsKey(current)){ + int currentCount = wordMap.get(current); + currentCount++; + wordMap.put(current, currentCount); + }else{ + wordMap.put(current, 1); + } + + } + } catch (FileNotFoundException e) { + System.out.println("File not found: " + fileName); + } + + for(String key : wordMap.keySet()){ + System.out.println(key + " appears " + wordMap.get(key) + " times"); + } + + } + public static void main(String[] args) { + System.out.println("Hello world!"); + wordFrequency("tinyTale.txt"); } + + + + } \ No newline at end of file From c0b709132de95cbfa160e0a87f02931ad47ab9a0 Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Sun, 19 Feb 2023 19:12:47 -0800 Subject: [PATCH 2/5] added method to find most used word or words --- src/WordFrequency.java | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/WordFrequency.java b/src/WordFrequency.java index 5f4672e..8ad2f43 100644 --- a/src/WordFrequency.java +++ b/src/WordFrequency.java @@ -29,13 +29,50 @@ public static void wordFrequency(String fileName) { for(String key : wordMap.keySet()){ System.out.println(key + " appears " + wordMap.get(key) + " times"); } + Map maxWords = findMax(wordMap); + if(maxWords.size() > 1){ + System.out.print("The words that appear the most times are"); + int count = maxWords.size(); + for(String key : maxWords.keySet()){ + System.out.print(" \"" + key + "\""); + if(count > 2){ + System.out.print(","); + } + else if(count > 1){ + System.out.print(", and"); + } + count--; + } + System.out.println("."); + }else{ + System.out.print("The word that appears the most times is "); + for(String key : maxWords.keySet()) { + System.out.println("\"" + key + "\"."); + } + } } + public static Map findMax(Map thisMap){ + int max = 0; + Map maxMap = new HashMap<>(); + for(String key : thisMap.keySet()){ + if(thisMap.get(key) > max){ + max = thisMap.get(key); + maxMap = new HashMap<>(); + maxMap.put(key, thisMap.get(key)); + + }else if(thisMap.get(key) == max){ + maxMap.put(key, thisMap.get(key)); + } + } + return maxMap; + } + public static void main(String[] args) { System.out.println("Hello world!"); - wordFrequency("tinyTale.txt"); + wordFrequency("medTale.txt"); } From 64cdbc9e78964ef24024164e5ef99ff8c237d60e Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Mon, 20 Feb 2023 11:41:56 -0800 Subject: [PATCH 3/5] adjusted structure to make it more readable and intuitive --- src/WordFrequency.java | 87 ++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/src/WordFrequency.java b/src/WordFrequency.java index 8ad2f43..356f036 100644 --- a/src/WordFrequency.java +++ b/src/WordFrequency.java @@ -4,11 +4,23 @@ import java.util.Map; import java.util.Scanner; +/** + * @author Katherine Watkins + * SDEV 333 + * 02/20/2023 + * WordFrequency assignment + */ + public class WordFrequency { - public static void wordFrequency(String fileName) { + /** + * + * @param fileName is a String that the scanner uses to find the file + * @return returns a Map with all the words and their frequency within the specified file + */ + public static Map wordFrequency(String fileName) { + //map to store each word and it's frequency Map wordMap = new HashMap<>(); - - + //scanner in a try catch for FileNotFoundException try (Scanner s = new Scanner(new File(fileName))) { while (s.hasNext()) { String current = s.next(); @@ -25,15 +37,38 @@ public static void wordFrequency(String fileName) { } catch (FileNotFoundException e) { System.out.println("File not found: " + fileName); } + return wordMap; + } - for(String key : wordMap.keySet()){ - System.out.println(key + " appears " + wordMap.get(key) + " times"); + /** + * This method finds the most frequent word/words and prints those to the console. + * @param thisMap takes in a map as a parameter that has words as keys and their frequency as values + */ + public static void findMax(Map thisMap){ + //int to keep track of max value found + int max = 0; + //new map to store highest value/values + Map maxMap = new HashMap<>(); + for(String key : thisMap.keySet()){ + //if current word has HIGHER value than previous max, reset the maxMap and put the new values + if(thisMap.get(key) > max){ + max = thisMap.get(key); + maxMap = new HashMap<>(); + maxMap.put(key, thisMap.get(key)); + + } + //if multiple words have the same max frequency, add them to maxMap + else if(thisMap.get(key) == max){ + maxMap.put(key, thisMap.get(key)); + } } - Map maxWords = findMax(wordMap); - if(maxWords.size() > 1){ + //Print out the word/words that appear most, if more than one word is in maxMap it will print differently than if there is only one + if(maxMap.size() > 1){ + int num = 0; System.out.print("The words that appear the most times are"); - int count = maxWords.size(); - for(String key : maxWords.keySet()){ + int count = maxMap.size(); + for(String key : maxMap.keySet()){ + num = maxMap.get(key); System.out.print(" \"" + key + "\""); if(count > 2){ System.out.print(","); @@ -43,36 +78,24 @@ else if(count > 1){ } count--; } - System.out.println("."); + System.out.println(". They each appear " + num + " times."); }else{ System.out.print("The word that appears the most times is "); - for(String key : maxWords.keySet()) { - System.out.println("\"" + key + "\"."); + for(String key : maxMap.keySet()) { + System.out.println("\"" + key + "\" and it appears " + maxMap.get(key) + " times."); } } - - } - - public static Map findMax(Map thisMap){ - int max = 0; - Map maxMap = new HashMap<>(); - for(String key : thisMap.keySet()){ - if(thisMap.get(key) > max){ - max = thisMap.get(key); - maxMap = new HashMap<>(); - maxMap.put(key, thisMap.get(key)); - - }else if(thisMap.get(key) == max){ - maxMap.put(key, thisMap.get(key)); - } - } - return maxMap; } public static void main(String[] args) { - - System.out.println("Hello world!"); - wordFrequency("medTale.txt"); + //frequency Map stored in variable + Map wordMap = wordFrequency("tinyTale.txt"); + //for loop to print out each word and it's frequency + for(String key : wordMap.keySet()){ + System.out.println(key + " appears " + wordMap.get(key) + " times"); + } + //call to findMax that will print out the most frequently used word/words + findMax(wordMap); } From 91e31a93895af9f67b42ae891aff102308308fd3 Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Mon, 20 Feb 2023 11:43:04 -0800 Subject: [PATCH 4/5] adjusted structure to make it more readable and intuitive --- src/WordFrequency.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/WordFrequency.java b/src/WordFrequency.java index 356f036..17cca15 100644 --- a/src/WordFrequency.java +++ b/src/WordFrequency.java @@ -49,6 +49,7 @@ public static void findMax(Map thisMap){ int max = 0; //new map to store highest value/values Map maxMap = new HashMap<>(); + //iterate through the map to find the highest frequency word or words for(String key : thisMap.keySet()){ //if current word has HIGHER value than previous max, reset the maxMap and put the new values if(thisMap.get(key) > max){ @@ -97,8 +98,4 @@ public static void main(String[] args) { //call to findMax that will print out the most frequently used word/words findMax(wordMap); } - - - - } \ No newline at end of file From 3d6b5c4914b785aee060e4378aa642730909327d Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Mon, 20 Feb 2023 11:46:15 -0800 Subject: [PATCH 5/5] changed to treemap so words print in alphabetical order --- src/WordFrequency.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/WordFrequency.java b/src/WordFrequency.java index 17cca15..baac4f8 100644 --- a/src/WordFrequency.java +++ b/src/WordFrequency.java @@ -3,6 +3,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Scanner; +import java.util.TreeMap; /** * @author Katherine Watkins @@ -19,7 +20,7 @@ public class WordFrequency { */ public static Map wordFrequency(String fileName) { //map to store each word and it's frequency - Map wordMap = new HashMap<>(); + Map wordMap = new TreeMap<>(); //scanner in a try catch for FileNotFoundException try (Scanner s = new Scanner(new File(fileName))) { while (s.hasNext()) { @@ -90,7 +91,7 @@ else if(count > 1){ public static void main(String[] args) { //frequency Map stored in variable - Map wordMap = wordFrequency("tinyTale.txt"); + Map wordMap = wordFrequency("tale.txt"); //for loop to print out each word and it's frequency for(String key : wordMap.keySet()){ System.out.println(key + " appears " + wordMap.get(key) + " times");