0

Fully develop the classes for the Linked Implementation of the ADT Bag (i.e., LinkedBag, Node).

Test your classes well (call all methods) before you proceed.

  1. Spell checker Write a spell checker that: a. Reads in words from an external file. b. Tests each word against a dictionary (an instance of your LinkedBag class) of correctly spelled words (if the word is found in the dictionary then it’s spelled correctly).

I have already developed the classes for the linked implementation of the ADT Bag and also made the spellchecker.java

I need help to understand how to use and implement linkedbag class and my words.txt file for this problem.

Dictionary.java

package Bags;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Dictionary {
private int M = 1319; // prime number
final private Bucket[] array;
public Dictionary() {
    this.M = M;
    array = new Bucket[M];
    for (int i = 0; i < M; i++) {
        array[i] = new Bucket();
    }
}
private int hash(String key) {
    return (key.hashCode() & 0x7fffffff) % M;
}
// call hash() to decide which bucket to put it in, do it.
public void add(String key) {
    array[hash(key)].put(key);
}
// call hash() to find what bucket it's in, get it from that bucket.
public boolean contains(String input) {
    input = input.toLowerCase();
    return array[hash(input)].get(input);
}

public void build(String filePath) {
    try {
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        String line;
        while ((line = reader.readLine()) != null) {
            add(line);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

}

//
public String[] getRandomEntries(int num) {
    String[] toRet = new String[num];
    for (int i = 0; i < num; i++) {
        // pick a random bucket, go out a random number
        Node n = array[(int) Math.random() * M].first;
        int rand = (int) Math.random() * (int) Math.sqrt(num);

        for (int j = 0; j < rand && n.next != null; j++)
            n = n.next;
        toRet[i] = n.word;

    }
    return toRet;
}

class Bucket {

    private Node first;

    public boolean get(String in) { // return key true if key exists
        Node next = first;
        while (next != null) {
            if (next.word.equals(in)) {
                return true;
            }
            next = next.next;
        }
        return false;
    }

    public void put(String key) {
        for (Node curr = first; curr != null; curr = curr.next) {
            if (key.equals(curr.word)) {
                return; // search hit: return
            }
        }
        first = new Node(key, first); // search miss: add new node
    }

    class Node {

        String word;
        Node next;

        public Node(String key, Node next) {
            this.word = key;
            this.next = next;
        }

    }

}

My problem is that I am calling Hash() to decide which bucket to put it in and do the whole process. Instead of this I want to use an Instance of my linkedbag class to look through my word.txt file and return output if the input matches or not..

  • "I have already developed the classes for the linked implementation of the ADT Bag and also made the spellchecker.java" - Okay, could you please [edit] your question to include them? Otherwise, sounds like homework you want us to complete – OneCricketeer Sep 18 '16 at 04:04
  • @cricket_007 I added all of the code I had now.. – Bhagirath Bhardwaj Sep 18 '16 at 04:15
  • So, what part do you not understand? Could you make a [mcve] of expected results of your current problem you are trying to implement? – OneCricketeer Sep 18 '16 at 04:18
  • I'll just say that `src/Bags/words.txt` is not a readable location for your file. When the code is compiled, the src directory doesn't exist. You code does not run from the src directory. I'm sure you can search for how to read a file from your project code – OneCricketeer Sep 18 '16 at 04:23
  • For example, reading a file from the classpath. Since you have packages, though, your file should sit outside of the src folder. http://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java – OneCricketeer Sep 18 '16 at 04:25
  • @cricket_007 I edited again, My doubt is that right now I am calling Hash() to decide which bucket to put it in and do the whole process. Instead of this I want to know and understand how can I use an Instance of my linkedbag class to look through my word.txt file and return output if the input matches or not.. (yup I corrected the file location thing too) – Bhagirath Bhardwaj Sep 18 '16 at 04:29
  • So, if I understand correctly, you want to replace this HashSet implementation with a LinkedList? – OneCricketeer Sep 18 '16 at 04:35
  • @cricket_007 yess – Bhagirath Bhardwaj Sep 18 '16 at 04:36
  • Okay, then you don't need `hash()`, and the `add()` method should look fairly close to the logic of the `Bucket` you find the null next node at the end, then set it. Matter of fact, the `contains()` method is almost exactly what `get()` in the Bucket already does... – OneCricketeer Sep 18 '16 at 04:40
  • @cricket_007 Can you please give me an example through code. do not write the answer for this but just one general outline. I'm really lost here about how to go about it – Bhagirath Bhardwaj Sep 18 '16 at 04:43
  • Actually, `add()` and `put()` look would look similar – OneCricketeer Sep 18 '16 at 04:43
  • I think you already have the answer. Replace `Dictionary` with `Bucket`. I'm not sure about the whole spell-checker concept... – OneCricketeer Sep 18 '16 at 04:44
  • @cricket_007 Hey man.. I still can't understand where to use which code.. do I have to write these add, and contains method in linkedbag.java or dictionary.java. Also how do I use it without hash it's giving me errors. – Bhagirath Bhardwaj Sep 19 '16 at 01:11
  • Your Bucket class seems to already do everything that your LinkedBag class should do – OneCricketeer Sep 19 '16 at 03:26

0 Answers0