Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview : Use HashMap to implement a word dictionary that stores the position of words in a paragraph. Requirements : Considering the text below from

Overview: Use HashMap to implement a word dictionary that stores the position of words in a paragraph.

Requirements: Considering the text below from a song, your program will store the positions of words in the text using a HashMap of the type Map>, where the words are the keys and the values are lists of position objects.

Again and again and again and again Do it again, do it again Again and again It's a shame, it's a shame It's a perfect shame Creep under my door and we do it again, oh oh

A position (x; y) specifies the position of a word as the xth sentence and yth word in a paragraph.

class Position { // x is line position, and y is word position in a line final int x,y; Position(int x, int y) { this.x = x; this.y = y; } public String toString() { return "(" + x + "," + y + ")"; } }

For example, the positions of the word shame are (3,2), (3,5), and (4,3). Note that we always count from 0 and phrase like It's is treated as a single word. We ignore commas and periods.

You should implement four methods in the Dictionary class:

1. add method adds a word at position p to the dictionary. We do not distinguish the same word with different cases. You should convert all words to lower cases first.

2. toString returns the string representation of the dictionary (see the sample output).

3. most method returns the most frequently used word. If there are more than one, then pick the first one.

4. print method reconstructs the text that the dictionary is generated from except that all occurrences of the most frequently used word are all capitalized (see the sample output). You don't have to recover commas or periods in the original text. All words are lower cases except the most frequently used word.

Sample output:

The program should mimic the sample run using the provided driver. a [(3,1), (3,4), (4,1)] shame [(3,2), (3,5), (4,3)] door [(5,3)] -----------------------------------------------------------------------------------------------------------------------

package assignment;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class assignment {

public static void main(String[] args) {

String lyrics = "Again and again and again and again "+

"Do it again, do it again "+

"Again and again "+

"It's a shame, it's a shame "+

"It's a perfect shame "+

"Creep under my door and we do it again, oh oh";

String[] sentences = lyrics.split(" ");

List> words = new ArrayList<>();

for(String s : sentences) {

String[] ws = s.split(" ");

List l = new ArrayList<>();

words.add(l);

for(String w: ws) {

w = w.replace(",", "").replace(".", "");

l.add(w);

}

}

Dictionary d = new Dictionary();

for(int i=0; i

List l = words.get(i);

for(int j=0; j

d.add(l.get(j), new Position(i, j));

}

}

System.out.println(d);

System.out.println("most frequent word: " + d.most() + " ");

System.out.println(d.print());

}

}

class Position {

// x is line position, and y is word position in a line

final int x,y;

Position(int x, int y) { this.x = x; this.y = y; }

public String toString() { return "(" + x + "," + y + ")"; }

}

class Dictionary {

private Map> map = new HashMap<>();

// add a word at position p to this dictionary

void add(String word, Position p) {

// TODO

}

// return the string representation of the dictionary

public String toString() {

// TODO

}

// return the most frequently used word

String most() {

// TODO

}

// return the paragraph that the dictionary is generated from

// with the most frequently used word capitalized

public String print() {

// TODO

}

}

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

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions

Question

1. Describe the factors that lead to productive conflict

Answered: 1 week ago