Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create the WordFrequencyAnalyzer constructor using Symbol Table, getCount, maxCount and implement Word class (do not use string for this assignment). The WordFrequencyAnalyzer will be passed

Create the WordFrequencyAnalyzer constructor using Symbol Table, getCount, maxCount and implement Word class (do not use string for this assignment). The WordFrequencyAnalyzer will be passed a text file.

import dsUtils.WordReader;

public class WordFrequencyAnalyzer { /**********************************************************************************/ /* You are not allowed to add any fields to this class beyond the one given below */ /* You may only read in from the file once. This means you may only use a single */ /* word reader object. */ /**********************************************************************************/ // Maintain a counter for each word in the text. SequentialSearchST counters; /** * Stores a count of the number of times any word appears in a file. The file is * read in exactly once at the time time this object is constructed. * * @param filename the name of the file on which to count all word occurrences. */ public WordFrequencyAnalyzer(String filename) { throw new RuntimeException("Not implemented."); } /** * Returns the number of times a given word appears in the file from which this * object was created. * * @param word the word to count * @return the number of times word appears. */ public int getCount(char[] word) { throw new RuntimeException("Not implemented."); } /** * Returns the maximum frequency over all words in the file from which this * ojbect was created. * * @return the maximum frequency of any word in the the file. */ public int maxCount() { throw new RuntimeException("Not implemented."); } }

//WordReader.java class below

import edu.princeton.cs.algs4.*;

import java.util.Iterator;

public class WordReader implements Iterable {

String[] strings;

public WordReader(String filename) {

In in = new In(filename);

strings = in.readAllStrings();

for (int i = 0; i < strings.length; i++) {

strings[i] = strings[i].replaceAll("[^a-zA-Z'\\-]", "").toLowerCase();

}

in.close();

}

public Iterator iterator() {

return new StringReaderIterator();

}

private class StringReaderIterator implements Iterator {

int pos;

public StringReaderIterator() {

pos = 0;

while(pos < strings.length && (strings[pos].length() == 0 || strings[pos].length() > 15))

pos++;

}

public boolean hasNext() {

return pos < strings.length;

}

public char[] next() {

String answer = strings[pos++];

while(pos < strings.length && strings[pos].length() == 0)

pos++;

return answer.toCharArray();

}

}

}

//SequentialSearchST.java class below

import edu.princeton.cs.algs4.Queue;

/**

* The {@code SequentialSearchST} class represents an (unordered)

* symbol table of generic key-value pairs.

* It supports the usual put, get, contains,

* delete, size, and is-empty methods.

* It also provides a keys method for iterating over all of the keys.

* A symbol table implements the associative array abstraction:

* when associating a value with a key that is already in the symbol table,

* the convention is to replace the old value with the new value.

* The class also uses the convention that values cannot be {@code null}. Setting the

* value associated with a key to {@code null} is equivalent to deleting the key

* from the symbol table.

*

* It relies on the {@code equals()} method to test whether two keys

* are equal. It does not call either the {@code compareTo()} or

* {@code hashCode()} method.

*

* This implementation uses a singly linked list and

* sequential search.

* The put and delete operations take Θ(n).

* The get and contains operations takes Θ(n)

* time in the worst case.

* The size, and is-empty operations take Θ(1) time.

* Construction takes Θ(1) time.

*/

public class SequentialSearchST {

private int n; // number of key-value pairs

private Node first; // the linked list of key-value pairs

// a helper linked list data type

private class Node {

private Key key;

private Value val;

private Node next;

public Node(Key key, Value val, Node next) {

this.key = key;

this.val = val;

this.next = next;

}

}

/**

* Initializes an empty symbol table.

*/

public SequentialSearchST() {

}

/**

* Returns the number of key-value pairs in this symbol table.

*

* @return the number of key-value pairs in this symbol table

*/

public int size() {

return n;

}

/**

* Returns true if this symbol table is empty.

*

* @return {@code true} if this symbol table is empty;

* {@code false} otherwise

*/

public boolean isEmpty() {

return size() == 0;

}

/**

* Returns true if this symbol table contains the specified key.

*

* @param key the key

* @return {@code true} if this symbol table contains {@code key};

* {@code false} otherwise

* @throws IllegalArgumentException if {@code key} is {@code null}

*/

public boolean contains(Key key) {

if (key == null) throw new IllegalArgumentException("argument to contains() is null");

return get(key) != null;

}

/**

* Returns the value associated with the given key in this symbol table.

*

* @param key the key

* @return the value associated with the given key if the key is in the symbol table

* and {@code null} if the key is not in the symbol table

* @throws IllegalArgumentException if {@code key} is {@code null}

*/

public Value get(Key key) {

if (key == null) throw new IllegalArgumentException("argument to get() is null");

for (Node x = first; x != null; x = x.next) {

if (key.equals(x.key))

return x.val;

}

return null;

}

/**

* Inserts the specified key-value pair into the symbol table, overwriting the old

* value with the new value if the symbol table already contains the specified key.

* Deletes the specified key (and its associated value) from this symbol table

* if the specified value is {@code null}.

*

* @param key the key

* @param val the value

* @throws IllegalArgumentException if {@code key} is {@code null}

*/

public void put(Key key, Value val) {

if (key == null) throw new IllegalArgumentException("first argument to put() is null");

if (val == null) {

delete(key);

return;

}

for (Node x = first; x != null; x = x.next) {

if (key.equals(x.key)) {

x.val = val;

return;

}

}

first = new Node(key, val, first);

n++;

}

/**

* Removes the specified key and its associated value from this symbol table

* (if the key is in this symbol table).

*

* @param key the key

* @throws IllegalArgumentException if {@code key} is {@code null}

*/

public void delete(Key key) {

if (key == null) throw new IllegalArgumentException("argument to delete() is null");

first = delete(first, key);

}

// delete key in linked list beginning at Node x

// warning: function call stack too large if table is large

private Node delete(Node x, Key key) {

if (x == null) return null;

if (key.equals(x.key)) {

n--;

return x.next;

}

x.next = delete(x.next, key);

return x;

}

/**

* Returns all keys in the symbol table as an {@code Iterable}.

* To iterate over all of the keys in the symbol table named {@code st},

* use the foreach notation: {@code for (Key key : st.keys())}.

*

* @return all keys in the symbol table

*/

public Iterable keys() {

Queue queue = new Queue();

for (Node x = first; x != null; x = x.next)

queue.enqueue(x.key);

return queue;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

2. Describe why we form relationships

Answered: 1 week ago

Question

5. Outline the predictable stages of most relationships

Answered: 1 week ago