Advertisement
Guest User

kaylie

a guest
Apr 2nd, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.NoSuchElementException;
  5. import java.util.Scanner;
  6. /**
  7. *
  8. * @author Kaylie Lu
  9. * @period # 5
  10. */
  11.  
  12. public class CountWords
  13. {
  14. private ArrayList <Word> myWords = new ArrayList <Word>();//ArrayList of Word objects
  15. private String fileName = "";//The name of the file read
  16. private String text = "";//The file in a String
  17. boolean hasError = false;
  18. int numberOfWords = 0;//The number of total words read
  19. int uniqueWords = 0;//The number of unique words there are
  20.  
  21. /**
  22. * Loads the words from the text file and adds ot the ArrayList
  23. * @param fName
  24. */
  25. public CountWords(String fName){
  26. fileName = fName;
  27. loadFile(fName);
  28.  
  29. addList(text);
  30. sort(myWords);
  31. }
  32. /**
  33. * Reads the file and stores the contents in a myWords ArrayList.
  34. * @param s The name of the file to be read.
  35. */
  36. public void loadFile(String s ){
  37. try{
  38. Scanner sc = new Scanner(new File(s));
  39. if (!sc.hasNextLine())
  40. throw new NoSuchElementException();
  41. while (sc.hasNextLine()) {
  42. String word = sc.nextLine();
  43. text = text + word + " ";
  44.  
  45.  
  46. }
  47. }catch(IOException i){
  48. System.out.println("File: " + fileName);
  49. System.out.println("Error:" + i.getMessage());
  50. hasError = true;
  51. }
  52. }
  53. /**
  54. * Private method to remove the punctuation from the list of words.
  55. * @param s
  56. */
  57. private String removePunc(String s){
  58. String copy = s;
  59. String newS= "";
  60.  
  61. for (int i = 0; i < copy.length(); i++) {
  62. char loc = copy.charAt(i);
  63. if ((loc >= 'a' && loc <= 'z')||( loc >= 'A' && loc <= 'Z')
  64. || (loc >= '0' && loc <= '9')||(loc == (char)32 )||(loc == (char)39) )
  65. newS += loc;
  66. }
  67. return newS;
  68.  
  69. }
  70. /**
  71. * Helper method adds words to the myWords list from a String
  72. * @param s the String to be divided into words
  73. */
  74. private void addList(String s){
  75. removePunc(s);
  76. String w = "";
  77.  
  78. while ((!(text.indexOf(' ')==-1))&&(text.length() > 0)) {
  79. int checked,spaceIndex;
  80. checked= 0;
  81. spaceIndex = text.indexOf(' ');
  82. w = text.substring(0, spaceIndex);
  83. text = text.substring(spaceIndex + 1);
  84.  
  85. if (w.indexOf("-") < 0) {
  86.  
  87. Word currWord = new Word(w,0);
  88. if (!currWord.getString().equalsIgnoreCase("")) {
  89. for (int i = 0; i <myWords.size(); i++) {
  90. if (currWord.equals(myWords.get(i))) {
  91. (myWords.get(i)).incrementCount();
  92. numberOfWords++;
  93. checked++;
  94.  
  95. }
  96. }
  97. if (checked == 0) {
  98. myWords.add(currWord);
  99. numberOfWords++;
  100. uniqueWords++;
  101.  
  102.  
  103. }
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * Prints the statistics of the myWords file.
  110. */
  111. public void printStats(){
  112. if (hasError == false){
  113. System.out.println("File: " + fileName);
  114. }
  115.  
  116. System.out.println("Total number of unique words used in the file: " + uniqueWords);
  117. System.out.println("Total number of words in file: " + numberOfWords);
  118. System.out.println("Top 30 words are: ");
  119. int i = 0;
  120. for(int j = 0; j<myWords.size();j++){
  121. if(i<30)
  122. {
  123. System.out.printf("%2d%6d%12s",j,myWords.get(j).getCount(),myWords.get(j).getString());
  124. System.out.println();
  125. i++;
  126. }
  127. }
  128. reset();
  129. }
  130.  
  131.  
  132. /**
  133. * Selection sorts the arrayList of words
  134. * @param words ArrayList to be sorted
  135. */
  136. private void sort(ArrayList<Word> words) {
  137. for (int i = 1; i < words.size(); i++) {
  138. int pos = i;
  139. Word w = words.get(pos);
  140. while (pos > 0 && (words.get(pos - 1).getString().
  141. toLowerCase().compareTo(w.getString().toLowerCase())) < 0) {
  142. words.set(pos, words.get(pos - 1));
  143. pos--;}
  144. words.set(pos, w);
  145. }
  146. for (int i = 1; i < words.size(); i++) {
  147. int pos = i;
  148. Word w = words.get(pos);
  149. while (pos > 0 && words.get(pos - 1).compareCount(w) < 0) {
  150. words.set(pos, words.get(pos- 1));
  151. pos--;
  152. }
  153. words.set(pos, w);
  154.  
  155. }
  156.  
  157.  
  158. }
  159. /**
  160. * Resets the variables for a new File
  161. */
  162. public void reset(){
  163. ArrayList <Word> myWords = new ArrayList<Word>();
  164. String fileName = "";
  165. String text = "";
  166. numberOfWords = 0;
  167. uniqueWords = 0;
  168.  
  169.  
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement