From ee6924a2a072ef5325142b7d487166f96addf751 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Thu, 9 May 2024 16:34:57 +0200 Subject: [PATCH] style: include `PATH_TRAVERSAL_IN` (#5148) --- spotbugs-exclude.xml | 3 - .../com/thealgorithms/others/TopKWords.java | 95 ------------------- 2 files changed, 98 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/others/TopKWords.java diff --git a/spotbugs-exclude.xml b/spotbugs-exclude.xml index c2f9593a..4f06c788 100644 --- a/spotbugs-exclude.xml +++ b/spotbugs-exclude.xml @@ -286,7 +286,4 @@ - - - diff --git a/src/main/java/com/thealgorithms/others/TopKWords.java b/src/main/java/com/thealgorithms/others/TopKWords.java deleted file mode 100644 index 4e30bbb4..00000000 --- a/src/main/java/com/thealgorithms/others/TopKWords.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.thealgorithms.others; - -import java.io.FileInputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Scanner; - -/* 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 final class TopKWords { - private TopKWords() { - } - - static class CountWords { - - private String fileName; - - 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 = ""; // 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 - if (fis != null) { - 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} - - // 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); - 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 = input.nextInt(); - } - for (int i = 0; i < k; i++) { - System.out.println(list.get(list.size() - i - 1)); - } - input.close(); - } -}