Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment4;
- /**
- * CS-202
- * 3/25/17
- * @version 1.0
- * @author Sam Venter
- */
- import java.util.*;
- import java.io.*;
- public class Assignment4 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Assignment4 obj = new Assignment4();
- obj.diction();
- obj.spellCheck();
- }//main
- static long wordsFound = 0;
- static long wordsNotFound = 0;
- static long compsFound = 0;
- static long compsNotFound = 0;
- MyLinkedList[] list = new MyLinkedList[26];
- /**
- * Purpose: set array with all words in the dictionary file
- */
- public void diction() {
- for (int i = 0; i < list.length; i++) {
- list[i] = new MyLinkedList<String>();
- }//for
- File f = new File("random_dictionary.txt");
- try {
- Scanner lineread = new Scanner(f);
- while (lineread.hasNext()) {
- String s = lineread.nextLine();
- s = s.toLowerCase();
- list[s.charAt(0) - 97].add(s);
- }//while
- } catch (IOException e) {
- System.out.println("Failure");
- }
- }//dictionary
- /**
- * Purpose: check external file with dictionary to count words found/ not found
- */
- public void spellCheck() {
- File f = new File("oliver.txt");
- try {
- Scanner lineread = new Scanner(f);
- while (lineread.hasNext()) {
- String s = lineread.next();
- s = s.replaceAll("\\,", "");
- s = s.replaceAll("\\'", "");
- s = s.replaceAll("\"", "");
- s = s.replaceAll("\\.", "");
- s = s.replaceAll("\\?", "");
- s = s.replaceAll("\\!", "");
- s = s.replaceAll("\\-", "");
- s = s.replaceAll("\\#", "");
- s = s.replaceAll("\\*", "");
- s = s.replaceAll("\\;", "");
- s = s.replaceAll("\\(", "");
- s = s.replaceAll("\\)", "");
- s = s.replaceAll("\\[", "");
- s = s.replaceAll("\\]", "");
- s = s.replaceAll("\\>", "");
- s = s.replaceAll("\\<", "");
- s = s.replaceAll("\\@", "");
- s = s.replaceAll("\\+", "");
- s = s.replaceAll("\\&", "");
- s = s.replaceAll("\\_", "");
- s = s.replaceAll("\\|", "");
- s = s.replaceAll("\\=", "");
- s = s.replaceAll("\\`", "");
- s = s.replaceAll("\\:", "");
- s = s.replaceAll("\\~", "");
- s = s.replaceAll("\\/", "");
- s = s.toLowerCase();
- if (s.isEmpty()) {
- } else if (s.contains("0") || s.contains("1") || s.contains("2") || s.contains("3") || s.contains("4")
- || s.contains("5") || s.contains("6") || s.contains("7") || s.contains("8") || s.contains("9")) {
- } else if (list[s.charAt(0) - 97].contains(s)) {
- wordsFound++;
- compsFound += (list[s.charAt(0) - 97].indexOf(s)) + 1;
- } else {
- wordsNotFound++;
- Object temp = list[s.charAt(0) - 97].getLast();
- compsNotFound += (list[s.charAt(0) - 97].indexOf(temp)) + 1;
- }
- }//while
- System.out.println("Words found: "+wordsFound);
- System.out.println("Words not found: "+wordsNotFound);
- System.out.println("Average comparisons for words found: " + (compsFound / wordsFound));
- System.out.println("Average comparisons for words not found: " + (compsNotFound / wordsNotFound));
- } catch (IOException e) {
- System.out.println("failure");
- }
- }//spellcheck
- }//class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement