Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package asi;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Collections;
- import java.io.*;
- public class Asi {
- /**
- * The function should read the contents of the file
- * designated by the filename and return its contents
- * as a string.
- * @param filename - A file name to read.
- * @return null if file is inaccessible (cannot be read or does not exist),
- * a string containing file contents otherwise.
- */
- public static String readFile(String filename) {
- try {
- File file = new File(filename);
- FileReader fileReader = new FileReader(file);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- StringBuffer stringBuffer = new StringBuffer();
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- stringBuffer.append(line);
- stringBuffer.append("\n");
- }
- fileReader.close();
- return stringBuffer.toString();
- } catch (IOException e) {
- return null;
- }
- }
- /**
- * The function returns a list containing
- * all the words from the input string.
- * @param text - an input string.
- * @return null, if the string is not supplied,
- * an empty list, if the string contains no words,
- * a list of words otherwise.
- */
- public static ArrayList<String> getWords(String text) {
- text = readFile(text);
- if (text == null) {
- return null;
- }
- text = text.toLowerCase();
- text = text.replace("\n", " ").replace("\r", " ").replace(".", "").replace("\t", " ").replace(",", "").replace("?", "").replace("!","").replace("/", "").replace(":", "").replace(" -", " ").replace("- ", " ").replace("(", "").replace(")", "").replace("–", "").replace("\"", "");
- text = text.trim().replaceAll(" +", " ");
- ArrayList<String> wordArrayList = new ArrayList<String>();
- for(String word : text.split(" ")) {
- wordArrayList.add(word);
- }
- return wordArrayList;
- }
- /**
- * The function returns the set containing only
- * unique words from the input string.
- * @param text - an input string
- * @return null, if the string is not supplied,
- * an empty set, if the string contains no words,
- * a set of unique words otherwise.
- */
- public static HashSet<String> getUniqueWords(String text) {
- if (readFile(text) == null) {
- return null;
- }
- HashSet<String> set = new HashSet<>();
- for (String item : getWords(text)) {
- if (!set.contains(item)) {
- set.add(item);
- }
- }
- return set;
- }
- /**
- * The function counts how many times each word
- * can be found in the text and saves this
- * information in the Map object, where the key is
- * the word, and the value is the amount of times
- * the considered word can be found in the text.
- * @param text - an input string
- * @return null, if the string is not supplied,
- * an empty set, if the string contains no words,
- * a map of words otherwise.
- */
- public static HashMap<String, Integer> getWordCount(String text) {
- if (readFile(text) == null) {
- return null;
- }
- HashSet<String> uniqueWords = getUniqueWords(text);
- ArrayList<String> allWords = getWords(text);
- HashMap<String, Integer> result = new HashMap<String, Integer>();
- for (String str : uniqueWords) {
- result.put(str, Collections.frequency(allWords, str));
- }
- System.out.println(allWords.size());
- System.out.println(result.size());
- return result;
- }
- /**
- * The main function should print out
- * the result of the getWordCount() method.
- * @param args - input parameters.
- */
- public static void main(String[] args) {
- // readFile("C:/Users/Kristjan/Documents/Garbage_Collector_Article.txt");
- //getWords(readFile("readFile("C:/Users/Kristjan/Documents/Garbage_Collector_Article.txt")"));
- //getUniqueWords("");
- System.out.println(getWordCount("C:/Users/kristjan.laimets/Downloads/Garbage_Collector_Article.txt"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement