Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. /**
  2. * Nick Tinsley
  3. * Programming Assignment 4
  4. * March 25, 2017
  5. */
  6. package assignmentfour;
  7.  
  8. import java.io.*;//file
  9. import java.util.*;//scanner
  10.  
  11. /**
  12. * @author Nick Tinsley
  13. */
  14. public class AssignmentFour {
  15.  
  16. //instantiate
  17. static int wordsFound = 0;
  18. static int wordsNotFound = 0;
  19. static long comparisonsFound = 0;
  20. static long comparisonsNotFound = 0;
  21.  
  22. MyLinkedList[] list = new MyLinkedList[26];//make linked list of size 26
  23.  
  24. /**
  25. * This method will read the dictionary and load each word into the correct
  26. * list index based on the first character of the word(alphabetically).
  27. */
  28. private void populateDictionary() {
  29. for (int i = 0; i < list.length; i++) {
  30. list[i] = new MyLinkedList<String>();//instantiate 26 linked lists
  31. }//for
  32. try {
  33. Scanner dictReader = new Scanner(new File("random_dictionary.txt"));//read file
  34. while (dictReader.hasNext()) {
  35. String word = dictReader.next().toLowerCase();//lowercase everything
  36. list[word.charAt(0) - 97].addFirst(word);//word to correct based off first letter(ASCII)
  37. }//while
  38. dictReader.close();
  39. }//try
  40. catch (IOException e) {
  41. System.out.println(e + "populateDictionary");
  42. }//catch
  43. }//populateDictionary
  44.  
  45. /**
  46. * This method will read the Oliver text and go through word by word checking if each
  47. * word is contained in the dictionary. If it is in the dictionary the word is found and assumed
  48. * to be spelled correctly. If it is not found in the dictionary it is assumed to be spelled
  49. * incorrectly. The method will count the number of words found and the number of words not found.
  50. * It will also count the total number of comparisons for words found and not found.
  51. */
  52. private void readOliver() {
  53. try {
  54. Scanner bookReader = new Scanner(new File("oliver.txt"));//read file
  55. while (bookReader.hasNext()) {//go through words
  56. String words = bookReader.next().toLowerCase().replaceAll("[^a-z]", "");//replace all special characters
  57. while (words.isEmpty()) {//if hits a null reference
  58. words = bookReader.next().toLowerCase().replaceAll("[^a-z]", "");//replace all special characters
  59. }//while
  60. int[] count = new int[1];//integer array with 1 element
  61. if (list[words.charAt(0) - 97].contains(words, count)) {//goes to correct linked list and checks if word is contained
  62. wordsFound++;//add to found if it is there
  63. comparisonsFound+=count[0];//number of comparisons to find words
  64. }//if
  65. else {
  66. wordsNotFound++;//add to not found if it is not there
  67. comparisonsNotFound+=count[0];//number of comparisons to not find words
  68. }//else
  69. }//while
  70. }//try
  71. catch (IOException e) {
  72. System.out.println(e + "readOliver");
  73. }//catch
  74. }//readOliver
  75.  
  76. /**
  77. * This method begins execution of all the testing. It will output the words found, words not
  78. * found, comparisons found, comparisons not found, and the averages for comparisons
  79. * per word found and not found.
  80. *
  81. * @param args : the command line arguments
  82. */
  83. public static void main(String[] args) {
  84. AssignmentFour test = new AssignmentFour();
  85. test.populateDictionary();
  86. test.readOliver();
  87. System.out.println("Words found " + wordsFound);
  88. System.out.println("Words not found " + wordsNotFound);
  89. System.out.println("Comparisons found " + comparisonsFound);
  90. System.out.println("Comparisons not found " + comparisonsNotFound);
  91. System.out.println("Average number of comparisons per word found " + comparisonsFound/wordsFound);
  92. System.out.println("Average number of comparisons per word not found " + comparisonsNotFound/wordsNotFound);
  93. }//main
  94. }//class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement