From 7f1803af9954a3a31506faa59b4052607b65bbaf Mon Sep 17 00:00:00 2001 From: Ron Nguyen Date: Tue, 21 Feb 2023 21:28:29 -0500 Subject: [PATCH 1/3] Finish --- src/WordFrequency.java | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/WordFrequency.java b/src/WordFrequency.java index 2f82ee4..810af4e 100644 --- a/src/WordFrequency.java +++ b/src/WordFrequency.java @@ -1,5 +1,26 @@ +import java.util.*; +import java.io.*; + public class WordFrequency { - public static void main(String[] args) { - System.out.println("Hello world!"); + public static void main(String[] args) throws FileNotFoundException { + Map wordCounts = new TreeMap<>(); + + Scanner infile = new Scanner(new File("tinyTale.txt").getAbsolutePath()); + + while(infile.hasNext()){ + String word = infile.next(); + + if (wordCounts.containsKey(word)){ + int value = wordCounts.get(word); + value++; + wordCounts.put(word, value); + } + else { + wordCounts.put(word, 1); + } + } + + System.out.println(wordCounts); } + } \ No newline at end of file From 68e2069fe73db8429c0b23f32e0361b46f0711bc Mon Sep 17 00:00:00 2001 From: Ron Nguyen Date: Tue, 21 Feb 2023 21:43:50 -0500 Subject: [PATCH 2/3] change path and final version --- src/WordFrequency.java | 54 +++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/src/WordFrequency.java b/src/WordFrequency.java index 810af4e..472f68a 100644 --- a/src/WordFrequency.java +++ b/src/WordFrequency.java @@ -2,25 +2,51 @@ import java.io.*; public class WordFrequency { - public static void main(String[] args) throws FileNotFoundException { - Map wordCounts = new TreeMap<>(); + public static void main(String[] args){ + String file1 = "tinyTale.txt"; + String file2 = "tale.txt"; - Scanner infile = new Scanner(new File("tinyTale.txt").getAbsolutePath()); + //Map of all words appeared in files + System.out.println("Map of " + file1 + " file: " + countWords("333/WordFrequency/" + file1)); + System.out.println("Map of " + file2 + " file: " + countWords("333/WordFrequency/" + file2)); - while(infile.hasNext()){ - String word = infile.next(); + //This will print out the most frequent word in the files + System.out.println(mostAppearWord(file1)); + System.out.println(mostAppearWord(file2)); + } + public static Map countWords(String fileName){ - if (wordCounts.containsKey(word)){ - int value = wordCounts.get(word); - value++; - wordCounts.put(word, value); + Map wordCounts = new TreeMap<>(); + try { + Scanner scanner = new Scanner(new File(fileName)); + while (scanner.hasNext()) { + String line = scanner.nextLine(); + String[] words = line.toLowerCase().trim().split("[\\W\\d]+"); + for (String word : words) { + if (!word.isEmpty()) { // ignore empty strings + if (!wordCounts.containsKey(word)) { + wordCounts.put(word, 1); + } else { + wordCounts.put(word, wordCounts.get(word) + 1); + } + } + } } - else { - wordCounts.put(word, 1); + } catch (FileNotFoundException e) { + System.out.println("File not found: " + fileName); + } + return wordCounts; + } + public static String mostAppearWord(String fileName){ + Map wordCounts = countWords("333/WordFrequency/" + fileName); + int mostAppeared = 0; + String wordMostAppear = ""; + for (Map.Entry entry : wordCounts.entrySet()) { + if (entry.getValue() > mostAppeared) { + mostAppeared = entry.getValue(); + wordMostAppear = entry.getKey(); } } - - System.out.println(wordCounts); + return "The word \"" + wordMostAppear + "\" is the most appeared in " + fileName + " file with " + mostAppeared + " times."; } - } \ No newline at end of file From 3015a0235538a448f97861b773e9a6db9cf70eeb Mon Sep 17 00:00:00 2001 From: Ron Nguyen Date: Tue, 21 Feb 2023 22:34:44 -0500 Subject: [PATCH 3/3] final version --- src/WordFrequency.java | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/WordFrequency.java b/src/WordFrequency.java index 472f68a..70285c2 100644 --- a/src/WordFrequency.java +++ b/src/WordFrequency.java @@ -1,3 +1,4 @@ +import java.lang.constant.Constable; import java.util.*; import java.io.*; @@ -6,10 +7,9 @@ public static void main(String[] args){ String file1 = "tinyTale.txt"; String file2 = "tale.txt"; - //Map of all words appeared in files - System.out.println("Map of " + file1 + " file: " + countWords("333/WordFrequency/" + file1)); - System.out.println("Map of " + file2 + " file: " + countWords("333/WordFrequency/" + file2)); - + //This method will print out all word and appeared time in the file + eachWordCount(file1); + eachWordCount(file2); //This will print out the most frequent word in the files System.out.println(mostAppearWord(file1)); System.out.println(mostAppearWord(file2)); @@ -49,4 +49,19 @@ public static String mostAppearWord(String fileName){ } return "The word \"" + wordMostAppear + "\" is the most appeared in " + fileName + " file with " + mostAppeared + " times."; } + + public static void eachWordCount(String fileName){ + Map wordsCount = countWords("333/WordFrequency/" + fileName); + System.out.println(fileName + " file has: "); + for (Map.Entry entry : wordsCount.entrySet()) { + String time = ""; + if (entry.getValue() == 1) { + time = " time."; + } else { + time = " times."; + } + System.out.println("The word \"" + entry.getKey() + "\" appeared " + entry.getValue() + time); + } + System.out.println("-----------------------------------------------------------------------------------"); + } } \ No newline at end of file