From 51be5a12a6fe83836de3b730a1193cf618da30d2 Mon Sep 17 00:00:00 2001 From: Keqi Huang Date: Wed, 4 Apr 2018 16:07:19 -0500 Subject: [PATCH 1/4] Update TreeTraversal.java --- Data Structures/Trees/TreeTraversal.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data Structures/Trees/TreeTraversal.java b/Data Structures/Trees/TreeTraversal.java index bd0f2f7f5..60a2cd491 100644 --- a/Data Structures/Trees/TreeTraversal.java +++ b/Data Structures/Trees/TreeTraversal.java @@ -39,7 +39,7 @@ public class TreeTraversal { /** * The Node class which initializes a Node of a tree -* Consists of all 3 traversal methods: printInOrder, printPostOrder & printPreOrder +* Consists of all 4 traversal methods: printInOrder, printPostOrder, printPreOrder & printLevelOrder * printInOrder: LEFT -> ROOT -> RIGHT * printPreOrder: ROOT -> LEFT -> RIGHT * printPostOrder: LEFT -> RIGHT -> ROOT From df1b68b9eece3c7f98e6d3107bdb65a255fd4394 Mon Sep 17 00:00:00 2001 From: Keqi Huang Date: Wed, 4 Apr 2018 16:49:14 -0500 Subject: [PATCH 2/4] Create TopKWords.java --- Others/TopKWords.java | 92 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Others/TopKWords.java diff --git a/Others/TopKWords.java b/Others/TopKWords.java new file mode 100644 index 000000000..1e8eb00e9 --- /dev/null +++ b/Others/TopKWords.java @@ -0,0 +1,92 @@ +import java.io.*; +import java.util.*; + +/* display the most frequent K words in the file and the times it appear + in the file – shown in order (ignore case and periods) */ + +public class TopKWords { + public static void main(String[] a) { + // you can replace the filePath with yours + CountWords cw = new CountWords("/Users/lisanaaa/Desktop/words.txt"); + Map dictionary = cw.getDictionary(); // get the words dictionary: {word: frequency} + + // we change the map to list for convenient sort + List> list = new ArrayList<>(dictionary.entrySet()); + + // sort by lambda valueComparator + list.sort(Comparator.comparing( + m -> m.getValue()) + ); + + Scanner input = new Scanner(System.in); + Integer k = new Integer(input.nextLine()); + while (k > list.size()) { + System.out.println("Retype a number, your number is too large"); + input = new Scanner(System.in); + k = new Integer(input.nextLine()); + } + for (int i = 0; i getDictionary() { + Map dictionary = new HashMap<>(); + FileInputStream fis = null; + + try { + + fis = new FileInputStream(fileName); // open the file + int in = 0; + StringBuffer sb = new StringBuffer(); // load the word + in = fis.read(); // read one character + boolean notEnd = true; // signal whether is the end of file + + while (notEnd) { + // when in == -1 means get the end of the file + if (-1 == in) { + notEnd = false; //if false, end the while loop + } + if (Character.isLetter((char)in)) { + sb.append((char)in); //if get a letter, put it in StringBuffer + } else { + // this branch means an entire word has just been read + if (sb.length() > 0) { + //see whether word exists in StringBuffer or not + if (dictionary.containsKey(sb.toString())) { + //if exist, count++ + dictionary.put(sb.toString(), dictionary.get(sb.toString()) + 1); + } else { + // if not exist, initiate count of this word with 1 + dictionary.put(sb.toString(), 1); + } + } + sb = new StringBuffer(); //reload the StringBuffer + } + in = fis.read(); //read the character + } + return dictionary; + } + catch (IOException e) { + e.printStackTrace(); + } + finally { + try { + // you always have to close the I/O streams + fis.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } +} From 68d945077ed31c454397a6e6c185e136e0ad32f0 Mon Sep 17 00:00:00 2001 From: Keqi Huang Date: Sun, 8 Apr 2018 17:04:17 -0500 Subject: [PATCH 3/4] Update TopKWords.java All has been improved according to your advice. --- Others/TopKWords.java | 126 ++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 67 deletions(-) diff --git a/Others/TopKWords.java b/Others/TopKWords.java index 1e8eb00e9..96139f68f 100644 --- a/Others/TopKWords.java +++ b/Others/TopKWords.java @@ -1,11 +1,62 @@ -import java.io.*; -import java.util.*; +import java.io.*; +import java.util.*; -/* display the most frequent K words in the file and the times it appear +/* display the most frequent K words in the file and the times it appear in the file – shown in order (ignore case and periods) */ -public class TopKWords { - public static void main(String[] a) { +public class TopKWords { + static class CountWords { + private String fileName; + + public CountWords(String fileName) { + this.fileName = fileName; + } + + public Map getDictionary() { + Map dictionary = new HashMap<>(); + FileInputStream fis = null; + + try { + + fis = new FileInputStream(fileName); // open the file + int in = 0; + String s = new String(); // init a empty word + in = fis.read(); // read one character + + while (-1 != in) { + if (Character.isLetter((char)in)) { + s += (char)in; //if get a letter, append to s + } else { + // this branch means an entire word has just been read + if (s.length() > 0) { + // see whether word exists or not + if (dictionary.containsKey(s)) { + // if exist, count++ + dictionary.put(s, dictionary.get(s) + 1); + } else { + // if not exist, initiate count of this word with 1 + dictionary.put(s, 1); + } + } + s = ""; // reInit a empty word + } + in = fis.read(); + } + return dictionary; + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + // you always have to close the I/O streams + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } + } + public static void main(String[] args) { // you can replace the filePath with yours CountWords cw = new CountWords("/Users/lisanaaa/Desktop/words.txt"); Map dictionary = cw.getDictionary(); // get the words dictionary: {word: frequency} @@ -19,74 +70,15 @@ public class TopKWords { ); Scanner input = new Scanner(System.in); - Integer k = new Integer(input.nextLine()); + int k = input.nextInt(); while (k > list.size()) { System.out.println("Retype a number, your number is too large"); input = new Scanner(System.in); k = new Integer(input.nextLine()); } - for (int i = 0; i getDictionary() { - Map dictionary = new HashMap<>(); - FileInputStream fis = null; - - try { - - fis = new FileInputStream(fileName); // open the file - int in = 0; - StringBuffer sb = new StringBuffer(); // load the word - in = fis.read(); // read one character - boolean notEnd = true; // signal whether is the end of file - - while (notEnd) { - // when in == -1 means get the end of the file - if (-1 == in) { - notEnd = false; //if false, end the while loop - } - if (Character.isLetter((char)in)) { - sb.append((char)in); //if get a letter, put it in StringBuffer - } else { - // this branch means an entire word has just been read - if (sb.length() > 0) { - //see whether word exists in StringBuffer or not - if (dictionary.containsKey(sb.toString())) { - //if exist, count++ - dictionary.put(sb.toString(), dictionary.get(sb.toString()) + 1); - } else { - // if not exist, initiate count of this word with 1 - dictionary.put(sb.toString(), 1); - } - } - sb = new StringBuffer(); //reload the StringBuffer - } - in = fis.read(); //read the character - } - return dictionary; - } - catch (IOException e) { - e.printStackTrace(); - } - finally { - try { - // you always have to close the I/O streams - fis.close(); - } - catch (IOException e) { - e.printStackTrace(); - } - } - return null; - } -} From 0cf0aba68a16a6114779aaa640fa688fb2cf6368 Mon Sep 17 00:00:00 2001 From: Keqi Huang Date: Mon, 9 Apr 2018 09:16:06 -0500 Subject: [PATCH 4/4] Update TopKWords.java --- Others/TopKWords.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Others/TopKWords.java b/Others/TopKWords.java index 96139f68f..e2d3c6fa5 100644 --- a/Others/TopKWords.java +++ b/Others/TopKWords.java @@ -20,7 +20,7 @@ public class TopKWords { fis = new FileInputStream(fileName); // open the file int in = 0; - String s = new String(); // init a empty word + String s = ""; // init a empty word in = fis.read(); // read one character while (-1 != in) { @@ -74,7 +74,7 @@ public class TopKWords { while (k > list.size()) { System.out.println("Retype a number, your number is too large"); input = new Scanner(System.in); - k = new Integer(input.nextLine()); + k = input.nextInt(); } for (int i = 0; i < k; i++) { System.out.println(list.get(list.size() - i - 1));