Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. package asi;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Collections;
  8.  
  9. import java.io.*;
  10.  
  11. public class Asi {
  12.  
  13. /**
  14. * The function should read the contents of the file
  15. * designated by the filename and return its contents
  16. * as a string.
  17. * @param filename - A file name to read.
  18. * @return null if file is inaccessible (cannot be read or does not exist),
  19. * a string containing file contents otherwise.
  20. */
  21. public static String readFile(String filename) {
  22. try {
  23. File file = new File(filename);
  24. FileReader fileReader = new FileReader(file);
  25. BufferedReader bufferedReader = new BufferedReader(fileReader);
  26. StringBuffer stringBuffer = new StringBuffer();
  27. String line;
  28. while ((line = bufferedReader.readLine()) != null) {
  29. stringBuffer.append(line);
  30. stringBuffer.append("\n");
  31. }
  32. fileReader.close();
  33. return stringBuffer.toString();
  34. } catch (IOException e) {
  35. return null;
  36. }
  37. }
  38.  
  39. /**
  40. * The function returns a list containing
  41. * all the words from the input string.
  42. * @param text - an input string.
  43. * @return null, if the string is not supplied,
  44. * an empty list, if the string contains no words,
  45. * a list of words otherwise.
  46. */
  47. public static ArrayList<String> getWords(String text) {
  48.  
  49. text = readFile(text);
  50.  
  51. if (text == null) {
  52. return null;
  53. }
  54.  
  55. text = text.toLowerCase();
  56. text = text.replace("\n", " ").replace("\r", " ").replace(".", "").replace("\t", " ").replace(",", "").replace("?", "").replace("!","").replace("/", "").replace(":", "").replace(" -", " ").replace("- ", " ").replace("(", "").replace(")", "").replace("–", "").replace("\"", "");
  57. text = text.trim().replaceAll(" +", " ");
  58.  
  59. ArrayList<String> wordArrayList = new ArrayList<String>();
  60. for(String word : text.split(" ")) {
  61. wordArrayList.add(word);
  62. }
  63. return wordArrayList;
  64. }
  65.  
  66. /**
  67. * The function returns the set containing only
  68. * unique words from the input string.
  69. * @param text - an input string
  70. * @return null, if the string is not supplied,
  71. * an empty set, if the string contains no words,
  72. * a set of unique words otherwise.
  73. */
  74. public static HashSet<String> getUniqueWords(String text) {
  75.  
  76. if (readFile(text) == null) {
  77. return null;
  78. }
  79.  
  80.  
  81. HashSet<String> set = new HashSet<>();
  82.  
  83. for (String item : getWords(text)) {
  84.  
  85. if (!set.contains(item)) {
  86. set.add(item);
  87. }
  88. }
  89. return set;
  90.  
  91. }
  92.  
  93.  
  94. /**
  95. * The function counts how many times each word
  96. * can be found in the text and saves this
  97. * information in the Map object, where the key is
  98. * the word, and the value is the amount of times
  99. * the considered word can be found in the text.
  100. * @param text - an input string
  101. * @return null, if the string is not supplied,
  102. * an empty set, if the string contains no words,
  103. * a map of words otherwise.
  104. */
  105. public static HashMap<String, Integer> getWordCount(String text) {
  106. if (readFile(text) == null) {
  107. return null;
  108. }
  109.  
  110. HashSet<String> uniqueWords = getUniqueWords(text);
  111. ArrayList<String> allWords = getWords(text);
  112.  
  113. HashMap<String, Integer> result = new HashMap<String, Integer>();
  114.  
  115. for (String str : uniqueWords) {
  116. result.put(str, Collections.frequency(allWords, str));
  117. }
  118.  
  119. System.out.println(allWords.size());
  120. System.out.println(result.size());
  121. return result;
  122. }
  123.  
  124. /**
  125. * The main function should print out
  126. * the result of the getWordCount() method.
  127. * @param args - input parameters.
  128. */
  129. public static void main(String[] args) {
  130. // readFile("C:/Users/Kristjan/Documents/Garbage_Collector_Article.txt");
  131. //getWords(readFile("readFile("C:/Users/Kristjan/Documents/Garbage_Collector_Article.txt")"));
  132. //getUniqueWords("");
  133. System.out.println(getWordCount("C:/Users/kristjan.laimets/Downloads/Garbage_Collector_Article.txt"));
  134. }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement