Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AmazonOA.Past_Tests;
- import java.util.*;
- public class TopKMentionedFeatures {
- public static ArrayList<String> popularNFeatures(int numFeatures, int topFeatures, List<String> possibleFeatures,
- int numFeatureRequests, List<String> featureRequests){
- ArrayList<String> desiredFeatures = new ArrayList<String>(topFeatures); //result will be of size topFeatures
- //create the featureSet, we will look for thease features in the List of featureRequests!
- HashSet<String> featureSet = new HashSet<>();
- for( String feature :possibleFeatures)
- featureSet.add(feature.toLowerCase());
- HashMap<String, Integer> featureMap = new HashMap<>();
- //iterate over the featureRequests, split into string array then check hashset
- for(String feature: featureRequests){
- String[] featuresDesired = feature.toLowerCase().split("\\s|\\.|\\?|!");
- HashSet<String> seenSet = new HashSet<>();
- for(String word : featuresDesired){
- if(featureSet.contains(word)){
- seenSet.add(word);
- }
- } // String Array for loop ends
- for(String setElement: seenSet)
- featureMap.put(setElement, featureMap.getOrDefault(setElement,0) + 1);
- }//outer forloop for list of features ends
- //create PQ
- PriorityQueue<HashMap.Entry<String,Integer>> minHeap = new PriorityQueue<>((feature1,feature2) -> feature1.getValue() -feature2.getValue());
- for(HashMap.Entry<String, Integer> entry : featureMap.entrySet()){
- minHeap.offer(entry);
- if(minHeap.size() > topFeatures)
- minHeap.poll();
- }
- while (!minHeap.isEmpty())
- desiredFeatures.add(0,minHeap.poll().getKey());
- return desiredFeatures;
- }
- public static void main(String[] args){
- List<String> featureRequests = new ArrayList<String>();
- featureRequests.add("I wish my kindle had even more storage!");
- featureRequests.add("I wish the battery life on my kindle lasted 2 years.");
- featureRequests.add("I read in the bath and would enjoy a waterproof Kindle");
- featureRequests.add("Waterproof and increased battery are my top two requests.");
- featureRequests.add("I want to take my kindle into the shower. Waterproof please waterproof!" );
- featureRequests.add("It would be neat if my kindle would hover on my desk when not in use.");
- featureRequests.add("How cool would it be if my kindle charged in the sun via solar power?");
- // System.out.println(featureRequests);
- List<String> possibleFeatures = new ArrayList<String>();
- possibleFeatures.add("storage");
- possibleFeatures.add("battery");
- possibleFeatures.add("hover");
- possibleFeatures.add("alexa");
- possibleFeatures.add("waterproof");
- possibleFeatures.add("solar");
- // System.out.println(possibleFeatures);
- // for(String feature : possibleFeatures){
- // String[] features = feature.toLowerCase().split()
- // }
- // String test = "I want to take my kindle into the shower. Waterproof please waterproof!";
- // String[] array = test.toLowerCase().split("\\s|\\.|\\?|!");
- // for(String words: array){
- // System.out.println(words);
- // }
- System.out.println();
- System.out.println(popularNFeatures(6,2,possibleFeatures,7,featureRequests));
- }
- }
Add Comment
Please, Sign In to add comment