Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. package assignment4;
  2.  
  3. /**
  4. * CS-202
  5. * 3/25/17
  6. * @version 1.0
  7. * @author Sam Venter
  8. */
  9. import java.util.*;
  10. import java.io.*;
  11.  
  12. public class Assignment4 {
  13.  
  14. /**
  15. * @param args the command line arguments
  16. */
  17. public static void main(String[] args) {
  18. Assignment4 obj = new Assignment4();
  19. obj.diction();
  20. obj.spellCheck();
  21.  
  22. }//main
  23. static long wordsFound = 0;
  24. static long wordsNotFound = 0;
  25. static long compsFound = 0;
  26. static long compsNotFound = 0;
  27. MyLinkedList[] list = new MyLinkedList[26];
  28.  
  29. /**
  30. * Purpose: set array with all words in the dictionary file
  31. */
  32. public void diction() {
  33. for (int i = 0; i < list.length; i++) {
  34. list[i] = new MyLinkedList<String>();
  35. }//for
  36. File f = new File("random_dictionary.txt");
  37. try {
  38. Scanner lineread = new Scanner(f);
  39. while (lineread.hasNext()) {
  40. String s = lineread.nextLine();
  41. s = s.toLowerCase();
  42. list[s.charAt(0) - 97].add(s);
  43. }//while
  44.  
  45. } catch (IOException e) {
  46. System.out.println("Failure");
  47. }
  48.  
  49. }//dictionary
  50.  
  51. /**
  52. * Purpose: check external file with dictionary to count words found/ not found
  53. */
  54. public void spellCheck() {
  55. File f = new File("oliver.txt");
  56. try {
  57. Scanner lineread = new Scanner(f);
  58.  
  59. while (lineread.hasNext()) {
  60. String s = lineread.next();
  61. s = s.replaceAll("\\,", "");
  62. s = s.replaceAll("\\'", "");
  63. s = s.replaceAll("\"", "");
  64. s = s.replaceAll("\\.", "");
  65. s = s.replaceAll("\\?", "");
  66. s = s.replaceAll("\\!", "");
  67. s = s.replaceAll("\\-", "");
  68. s = s.replaceAll("\\#", "");
  69. s = s.replaceAll("\\*", "");
  70. s = s.replaceAll("\\;", "");
  71. s = s.replaceAll("\\(", "");
  72. s = s.replaceAll("\\)", "");
  73. s = s.replaceAll("\\[", "");
  74. s = s.replaceAll("\\]", "");
  75. s = s.replaceAll("\\>", "");
  76. s = s.replaceAll("\\<", "");
  77. s = s.replaceAll("\\@", "");
  78. s = s.replaceAll("\\+", "");
  79. s = s.replaceAll("\\&", "");
  80. s = s.replaceAll("\\_", "");
  81. s = s.replaceAll("\\|", "");
  82. s = s.replaceAll("\\=", "");
  83. s = s.replaceAll("\\`", "");
  84. s = s.replaceAll("\\:", "");
  85. s = s.replaceAll("\\~", "");
  86. s = s.replaceAll("\\/", "");
  87. s = s.toLowerCase();
  88.  
  89. if (s.isEmpty()) {
  90.  
  91. } else if (s.contains("0") || s.contains("1") || s.contains("2") || s.contains("3") || s.contains("4")
  92. || s.contains("5") || s.contains("6") || s.contains("7") || s.contains("8") || s.contains("9")) {
  93.  
  94. } else if (list[s.charAt(0) - 97].contains(s)) {
  95. wordsFound++;
  96. compsFound += (list[s.charAt(0) - 97].indexOf(s)) + 1;
  97. } else {
  98. wordsNotFound++;
  99. Object temp = list[s.charAt(0) - 97].getLast();
  100. compsNotFound += (list[s.charAt(0) - 97].indexOf(temp)) + 1;
  101. }
  102. }//while
  103. System.out.println("Words found: "+wordsFound);
  104. System.out.println("Words not found: "+wordsNotFound);
  105. System.out.println("Average comparisons for words found: " + (compsFound / wordsFound));
  106. System.out.println("Average comparisons for words not found: " + (compsNotFound / wordsNotFound));
  107. } catch (IOException e) {
  108. System.out.println("failure");
  109. }
  110.  
  111. }//spellcheck
  112.  
  113. }//class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement