Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Homework assignment for school and the coding is mine import dsa.DiGraph; import dsa.SeparateChainingHashST; import dsa.Set; import stdlib.In; import stdlib.StdOut; public class WordNet { //... private

Homework assignment for school and the coding is mine

import dsa.DiGraph; import dsa.SeparateChainingHashST; import dsa.Set; import stdlib.In; import stdlib.StdOut;

public class WordNet { //... private final ShortestCommonAncestor sca; private final SeparateChainingHashST> st; private final SeparateChainingHashST rst; private final DiGraph G;

// Constructs a WordNet object given the names of the input (synset and hypernym) files. public WordNet(String synsets, String hypernyms) { //... if (synsets == null || hypernyms == null) { throw new IllegalArgumentException("files are not null"); } st = new SeparateChaningHashST<>(); rst = new SeparateChainingHashST<>(); int vertices = 0; In in = new In(synsets);

while (in.hasNextLine()) { vertices++; String[] line = in.readLine().split(","); String[] word = line[1].split(" "); int id = Integer.parseInt(line[0]); rst.put(id, line[1]);

for (int i = 0; i < word.length; i++) { if (st.contains(word[i])) { st.get(word[i]).add(id); } else { st.put(word[i], new Set()); st.get(word[i]).add(id); } } } DiGraph G = new DiGraph(vertices); in = new In(hypernyms); while(in.hasNextLine()) { String[] line = in.readLine().split(","); for (int i = 1; i < line.length; i++) { G.addEdge(Integer.parseInt(line[0]), Integer.parseInt(line[i])); } } sca = new ShortestCommonAncestor(); }

// Returns all WordNet nouns. public Iterable nouns() { //... return st.keys(); }

// Returns true if the given word is a WordNet noun, and false otherwise. public boolean isNoun(String word) { //... if (word == null) { throw new NullPointerException(); return st.contains(word); } }

// Returns a synset that is a shortest common ancestor of noun1 and noun2. public String sca(String noun1, String noun2) { //... if (noun1 == null || noun2 == null) { throw new NullPointerException(); } if (st.get(noun1) == null || st.get(noun2) == null) { throw new NullPointerException(); } Iterable integer1 = st.get(noun1); Iterable integer2 = st.get(noun2); return rst.get(sca.ancestor(integer1, integer2)); }

// Returns the length of the shortest ancestral path between noun1 and noun2. public int distance(String noun1, String noun2) { //... if (noun1 == null || noun2 == null || st.get(noun2) == null){ throw new NullPointerException(); } Iterable integer1 = st.get(noun1); Iterable integer2 = st.get(noun2); return sca.length(integer1, integer2); }

// Unit tests the data type. [DO NOT EDIT] public static void main(String[] args) { WordNet wordnet = new WordNet(args[0], args[1]); String word1 = args[2]; String word2 = args[3]; int nouns = 0; for (String noun : wordnet.nouns()) { nouns++; } StdOut.printf("# of nouns = %d ", nouns); StdOut.printf("isNoun(%s)? %s ", word1, wordnet.isNoun(word1)); StdOut.printf("isNoun(%s)? %s ", word2, wordnet.isNoun(word2)); StdOut.printf("isNoun(%s %s)? %s ", word1, word2, wordnet.isNoun(word1 + " " + word2)); StdOut.printf("sca(%s, %s) = %s ", word1, word2, wordnet.sca(word1, word2)); StdOut.printf("distance(%s, %s) = %s ", word1, word2, wordnet.distance(word1, word2)); } }

I wrote this code please help me fix my mistakes please do not change thform it was given to us to fill in the spaces.

Problem 1. (WordNet Data Type) Implement an immutable data type called WordNet with the following API:

WordNet WordNet(String synsets, String hypernyms) constructs a WordNet object given the names of the input (synset and hypernym) files

Iterable nouns() returns all WordNet nouns

boolean isNoun (String word) returns true if the given word is a WordNet noun, and false otherwise

String sca(String noun1, String noun2) returns a synset that is a shortest common ancestor of noun1 and noun2

int distance(String noun1, String noun2) returns the length of the shortest ancestral path between noun1 and noun2

Corner Cases

The constructor should throw a NullPointerException() with the message "synsets is null" if synsets is null and the message "hypernyms is null" if hypernyms is null.

The isNoun() method should throw a NullPointerException("word is null") if word is null.

The sca() and distance() methods should throw a NullPointerException() with the message "noun1 is null" or "noun2 is null" if noun1 or noun2 is null. The methods should throw an IllegalArgumentException() with the message "noun1 is not a noun" or "noun2 is not a noun" if noun1 or noun2 is not a noun.

Performance Requirements

The constructor and the nouns() method should run in time T(n) n, where n is the size of the WordNet lexicon.

The isNoun() method should run in time T(n) 1.

The sca() and distance() methods should make exactly one call to the ancestor() and length() methods in ShortestCommonAncestor, respectively.

What we will get when compiling program:

$ java WordNet data / synsets . txt data / hypernyms . txt worm bird

# of nouns = 119188

isNoun ( worm )? true

isNoun ( bird )? true

isNoun ( worm bird )? false

sca ( worm , bird ) = animal animate_being beast brute creature fauna

distance ( worm , bird ) = 5

Directions: How ro write this program

Instance variables: - A symbol table that maps a synset noun to a set of synset IDs (a synset noun can belong to multiple synsets), RedBlackBST> st . - A symbol table that maps a synset ID to the corresponding synset string, RedBlackBST rst. - For shortest common ancestor computations, ShortestCommonAncestor sca.

WordNet(String synsets, String hypernyms) - Initialize instance variables st and rst appropriately using the synset file. - Construct a DiGraph object G (representing a rooted DAG) with V vertices (equal to the number of entries in the synset file), and add edges to it, read in from the hypernyms file. - Initialize sca using G.

Iterable nouns() - Return all WordNet nouns.

boolean isNoun(String word) - Return true if the given word is a synset noun, and false otherwise.

String sca(String noun1, String noun2) - Use sca to compute and return a synset that is a shortest common ancestor of the given nouns.

int distance(String noun1, String noun2) - Use sca to compute and return the length of the shortest ancestral path between the given nouns.

Shortest Common Ancestor An ancestral path between two vertices v and w in a rooted DAG is a directed path from v to a common ancestor x, together with a directed path from w to the same ancestor x. A shortest ancestral path is an ancestral path of minimum total length. We refer to the common ancestor in a shortest ancestral path as a shortest common ancestor. Note that a shortest common ancestor always exists because the root is an ancestor of every vertex. Note also that an ancestral path is a path, but not a directed path.

Please help with this java assignment

I wrote the code and have only 4 errors it is the above coding

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

Recommended Textbook for

Mobile Usability

Authors: Jakob Nielsen, Raluca Budiu

1st Edition

0133122131, 9780133122138

More Books

Students also viewed these Programming questions

Question

Monsters as Rows, Players as Columns, Kills as Values.

Answered: 1 week ago