Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For full project description, please refer to the link below. I only need help with the last part with handing k!=0, so I will copy

For full project description, please refer to the link below. I only need help with the last part with handing k!=0, so I will copy my code (at the bottom) and the section description for that part specifically, all I need is some modification in my code to deal with the k != 0 case.

https://sp23.datastructur.es/materials/proj/proj2b/

Handling k != 0

Above, we handled the situation where k = 0, which is the default value when the user does not enter a k value.

Your final required task is to handle the case where the user enters k. k represents the maximum number of hyponyms that we want in our output. For example, if someone enters the word "dog", and then enters k = 5, your code would return exactly five words.

To choose the 5 hyponyms, you should return the k words which occurred the most times in the time range requested. For example, if someone entered words = "food, cake", startYear = 1950, endYear = 1990, and k = 5, then you would find the 5 most popular words in that time period that are hyponyms of both food and cake. Here, the popularity is defined as the total number of times the word appears over the entire time period. The words should then be returned in alphabetical order. In this case, the answer is [biscuit, cake, kiss, snap, wafer] if we're using top_49887_words.csv, synsets.txt, and hyponyms.txt.

Note that if the front end doesn't supply a year, default values of startYear = 1900 and endYear = 2020 are provided by NGordnetQueryHandler.readQueryMap.

If k = 0, or the user does not enter k (which results in a default value of zero), then the startYear and endYear should be totally ignored.

If a word never occurs in the time frame specified, i.e. the count is zero, it should not be returned. In other words, if k > 0, we should not show any words that do not appear in the ngrams dataset.

If there are no words that have non-zero counts, you should return an empty list, i.e. [].

If there are fewer than k words with non-zero counts, return only those words. For example if you enter the word "potato" and enter "k = 15", but only 7 hyponyms of potato have non-zero counts, you'd return only seven words.

Task: Modify your HyponymsHandler and the rest of your implementation to deal with the k != 0 case.

This task will be a little trickier since you'll need to figure out how to pass information around so that the HyponymsHandler knows how to access a useful NGramMap.

DO NOT MAKE A STATIC NGRAMMAP FOR THIS TASK! It might be tempting to simply make some sort of public static NGramMap that can be accessed from anywhere in your code. This is called a "global variable".

We strongly discourage this way of thinking about programming, and instead suggest that you should be passing an NGramMap to either constructors or methods. We'll come back to talking about this during the software engineering lectures.

Tips

  • Until you use the autograder, you'll need to construct your own test cases. We provide one above: words = "food, cake" , startYear = 1950, endYear = 1990, k = 5.

package ngordnet.main; import ngordnet.browser.NgordnetQuery; import ngordnet.browser.NgordnetQueryHandler; import java.util.*; import java.util.stream.Collectors; import edu.princeton.cs.algs4.In; import org.checkerframework.checker.units.qual.A; public class HyponymsHandler extends NgordnetQueryHandler { private TreeMap> tmWords = new TreeMap(); private TreeMap> tmInts = new TreeMap(); private HyponymsGraph hg; public HyponymsHandler(String synsetFilename, String hyponymFilename) { hg = new HyponymsGraph(synsetFilename, hyponymFilename); In in1 = new In(synsetFilename); String s; String[] def; String [] separate; ArrayList tempSIDs; while (in1.hasNextLine()){ s = in1.readLine(); def = s.split(","); separate = def[1].split(" "); Set temp = new HashSet(); Set temp3 = new HashSet(); for (int i = 0; i parseInt(def[0])); tmWords.put(word, temp3); } else{ HashSet temp2 = new HashSet(); temp2.add(Integer.parseInt(def[0])); tmWords.put(word, temp2); } temp.add(word); } tmInts.put(Integer.parseInt(def[0]),temp); } } public ArrayList returnDuplicates(ArrayList firstWord, ArrayList secondWord, List multipleWords, Integer size){ ArrayList duplicates = new ArrayList(); for (String word : firstWord){ if (secondWord.contains(word)){ duplicates.add((String) word); } } size++; if (multipleWords.size() == size){ return duplicates; } return returnDuplicates(duplicates, hyponyms((String) multipleWords.get(size)), multipleWords, size); } @Override public String handle(NgordnetQuery q) { List words = q.words(); TreeSet alph = new TreeSet(); int k = q.k(); if (words.size() > 1){ ArrayList hyps = new ArrayList(); TreeSet tempp = new TreeSet(); hyps = returnDuplicates(hyponyms(words.get(0)), hyponyms(words.get(1)), words, 1); for (String xx : hyps){ alph.add(xx); } } else{ ArrayList hyps = hyponyms(words.get(0)); for (String xx : hyps){ alph.add(xx); } } String print = "["; int y = 0; for (String x : alph){ if (y == alph.size()-1){ print += x; } else{ print += x; print += ", "; } y++; } print += "]"; return print; } public ArrayList hyponyms(String word) { ArrayList words2 = new ArrayList(); for (Integer SID: tmWords.get(word)){ words2.addAll(tmInts.get(SID)); Iterable hyps2 = hg.adj(SID); for (Integer id : hyps2){ words2.addAll(tmInts.get(id)); } } //Integer SID = tmWords.get(word); return words2; } }

helper method (graph):

package ngordnet.main; import edu.princeton.cs.algs4.Digraph; import edu.princeton.cs.algs4.DirectedDFS; import edu.princeton.cs.algs4.In; import java.util.*; public class HyponymsGraph { private TreeMap> tm; //private HashMap public HyponymsGraph(String synsetFilename, String hyponymFilename){ tm = new TreeMap(); String s; String[] hyponyms; Integer number; In in1 = new In(synsetFilename); while (in1.hasNextLine()) { s = in1.readLine(); number = Integer.parseInt(s.split(",")[0]); tm.put(number, new HashSet()); } In in2 = new In(hyponymFilename); while(in2.hasNextLine()){ s = in2.readLine(); hyponyms = s.split(","); Integer SID = Integer.parseInt(hyponyms[0]); for (int i = 1; iparseInt(hyponyms[i])); } } } public Iterable adj(Integer SID){ ArrayList all = new ArrayList(); for (Integer num: tm.get(SID)){ all.add(num); if (tm.get(num).size() != 0){ all.addAll((Collection extends Integer>) adj(num)); } } return all; } } data:

image text in transcribed
X X E hyponyms X E synsets X File Edit View File Edit View 503 852, Anglo-Saxon, a native or inhabitant of England prior to the Norman 32, 77393, 64712, 51671, 37744, 27392 Conquest 38, 31746 853, Anglo-Saxon_deity, (Anglo-Saxon mythology) a deity worshipped by the 40, 71751 Anglo-Saxons 101, 2778 854, Anglomania, an excessive enthusiasm for all things English 228, 15385 855, Anglophilia, admiration for Britain and British customs 234, 4490 856, Anglophobia, dislike (or fear) of Britain and British customs 235, 14340, 9641, 5014, 4489, 3638, 239 857, Angola Republic_of_Angola, a republic in southwestern Africa on the 271, 12914, 8815 Atlantic Ocean; achieved independence from Portugal in 1975 and was the 273, 63716, 19590 scene of civil war until 1990 277, 18621, 18587, 18535, 18529, 18399, 17358, 17232, 17189, 16945, 16521, 16320, 16234 858, Angolan, a native or inhabitant of Angola , 15816, 15776, 15542, 15322, 14670, 11963, 11961, 11604, 11392, 11338, 10649, 10315, 10 859, Angolan_monetary_unit, monetary unit in Angola 286, 10196, 9711, 9698, 8967, 7072, 6662, 6457, 6390, 6360, 5861, 5745, 5692, 5345, 4985, 860, Angolese, a member of the Bantu tribes resident in Angola 4065, 3545, 3388, 3147, 2904, 2690, 2133, 1990, 1975, 1818, 1751, 858, 486 861, Angora Angora_cat, a long-haired breed of cat similar to the Persian cat 283, 18586, 18534, 17357, 17231, 16943, 16520, 16322, 16232, 15815, 15775, 15540, 15320 862, Angora Angora_goat, a domestic breed of goat raised for its long silky , 14668, 11960, 11956, 11603, 11393, 11341, 10648, 10313, 10284, 10198,9710, 9697, 9642 hair which is the true mohair , 8966, 8420, 7066, 7065, 6661, 6456, 6388, 5691, 5636, 5594, 5344, 4982, 4063, 4062, 3387 863, Angora Angora_rabbit, domestic breed of rabbit with long white silky , 3290, 2902, 2688, 2335, 1973, 857, 485 hair 285, 1774 864, Angostura_Bridge, a suspension bridge across the Orinoco River at Ciudad 308, 15310, 12387, 7283, 5346, 4471, 3386, 1991 Bolivar 380, 1372 865, Anguidae family_Anguidae, alligator lizards 409, 402 866, Anguilla, a British colony in the West Indies 416, 16992, 6665 867, Anguilla genus_Anguilla, type genus of the Anguillidae: eels 417, 64085, 50929 868, Anguillan, a native or inhabitant of the island of Anguilla in the West 489, 488 Indies 496, 17951, 15438, 14992, 13719, 13717, 13697, 13007, 12889, 12740, 12583, 12291, 11625 869, Anguillidae family_Anguillidae, eels that live in fresh water as adults , 11165, 10987, 10966, 10851, 10587, 10310, 9022, 8134, 6197, 4739, 4325, 4098, 3546, 214 but return to the sea to spawn n 1000 AnA 75 1 1: r-. . .: LL Ln 12, Col 9 100% Unix (LF) UTF-8 Ln 853, Col 44 100% Unix (LF) UTF-8

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions