Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview : Use HashMap to implement a word dictionary that stores the position o

ID: 3741141 • Letter: O

Question

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<String, List<Position>>,
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<List<String>> words = new ArrayList<>();

for(String s : sentences) {

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

List<String> 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<words.size(); i++) {

List<String> l = words.get(i);

for(int j=0; j<l.size(); 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<String, List<Position>> 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

}

}

Explanation / Answer

Hi buddy,

Check the commented code below, which has comments to explain what actually done.

Hope this helps, feel free to ask for doubts if any.

All the best.