Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import edu.duke.FileResource; import java.util.ArrayList; import java.util.HashMap; import java.util.Random; /** * GladLibMap modified to use HashMaps. * * @author Pawel Daniluk * @version 2015-11-24 */ public

import edu.duke.FileResource; import java.util.ArrayList; import java.util.HashMap; import java.util.Random; /** * GladLibMap modified to use HashMaps. * * @author Pawel Daniluk * @version 2015-11-24 */ public class GladLibMap { private HashMap> wordMap; private int wordCount = 0; private ArrayList usedWords; private ArrayList usedLabels; private Random myRandom; private String source; /** * Constructor for setting path to the files and initializing data * structures for storing the information. * @param source name of the folder where to look for the files */ public GladLibMap(String source){ this.source = source; usedLabels = new ArrayList(); initializeFromSource(); myRandom = new Random(); } /** * Helper function for putting lists to HashMap with filename being key * @param fname name of file to be put to HashMap, data from file to be * converted to list */ private void putList(String fname) { wordMap.put(fname, readIt(source+"/"+fname+".txt")); } /** * Initializes all the replacement words from files with putList method. * @see putList */ private void initializeFromSource() { wordMap = new HashMap>(); putList("adjective"); putList("noun"); putList("color"); putList("country"); putList("name"); putList("animal"); putList("timeframe"); putList("verb"); putList("fruit"); // put random numbers ArrayList nums = new ArrayList(); for (int i=0;i<50;i++) nums.add(Integer.toString(i)); wordMap.put("number", nums); usedWords = new ArrayList(); } /** * Return random element from list identified by key. * @param key identifying map item from which to get random * replacement word. Keeps track of words already used and replaced words * count. * @return random word from source */ private String randomFrom(String key){ String randWord; ArrayList source = wordMap.get(key); if (!usedLabels.contains(key)) usedLabels.add(key); while (true) { int index = myRandom.nextInt(source.size()); randWord = source.get(index); int usedIndex = usedWords.indexOf(randWord); if (usedIndex == -1) break; else continue; } usedWords.add(randWord); wordCount++; return randWord; } /** * Return replacement of label in a template. * @param label label to be replaced by random word * @return random, unique word for given label. Return "UNKNOWN" if * no such label is found */ private String getSubstitute(String label) { if (wordMap.containsKey(label)) return randomFrom(label); else return "UNKNOWN"; } /** * Read words from template and replace labels with appropriate replacement * words * @param w word read from template * @return input word or replacement word */ private String processWord(String w){ int first = w.indexOf("<"); int last = w.indexOf(">",first); if (first == -1 || last == -1){ return w; } String prefix = w.substring(0,first); String suffix = w.substring(last+1); String sub = getSubstitute(w.substring(first+1,last)); return prefix+sub+suffix; } /** * Print the String to console with line breaks at specified character * @param s * @param lineWidth */ private void printOut(String s, int lineWidth){ int charsWritten = 0; for(String w : s.split("\\s+")){ if (charsWritten + w.length() > lineWidth){ System.out.println(); charsWritten = 0; } System.out.print(w+" "); charsWritten += w.length() + 1; } } /** * Read template and replace labels with random replacement words. * @return template file with all labels replaced with random words */ private String fromTemplate(String source){ String story = new String(); FileResource resource = new FileResource(source); for(String word : resource.words()){ story = story + processWord(word) + " "; } // before returning story, clear the counters usedWords.clear(); return story; } /** * Helper function for reading files into lists * @param source name of file to be read * @return list of words from file in form of new list */ private ArrayList readIt(String source){ ArrayList list = new ArrayList(); FileResource resource = new FileResource(source); for(String line : resource.lines()){ list.add(line); } return list; } public void makeStory(){ System.out.println(" "); String story = fromTemplate(source+"/madtemplate3.txt"); printOut(story, 60); System.out.println(" Replaced words:\t"+wordCount); System.out.println("Total words:\t" + totalWordsInMap()); System.out.println("Total words considered:\t"+totalWordsConsidered()); wordCount = 0; } /** * This method returns the total number of words in all the ArrayLists * in the HashMap. * @return total number of words that were possible to pick from */ @SuppressWarnings("unused") public int totalWordsInMap(){ int cnt = 0; for (String key : wordMap.keySet()) { for (String word : wordMap.get(key)) cnt++; } return cnt; } /** * This method returns the total number of words in the ArrayLists of the * categories that were used for a particular GladLib. * @return total number of words in categories that were used in GladLib */ @SuppressWarnings("unused") public int totalWordsConsidered() { int cnt = 0; for (String label : usedLabels) for (String word : wordMap.get(label)) cnt++; return cnt; } public static void main(String[] args) { GladLibMap glm = new GladLibMap("data/GladLibData/datalong"); glm.makeStory(); } } Consider the first version of GladLibs we saw in this lesson, which you modified so there would not be duplicate words chosen for the story. Assume an instance variable is used to keep track of the total number of word tags that are replaced. Which one of the following methods is most likely where that variable is updated? getSubstitute The GladLibs constructor. myStory processWord

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

Microsoft Outlook 2023

Authors: James Holler

1st Edition

B0BP9P1VWJ, 979-8367217322

Students also viewed these Databases questions