theSwamz

TopFeaturesDesired

Apr 12th, 2021 (edited)
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.58 KB | None | 0 0
  1. package AmazonOA.Past_Tests;
  2.  
  3. import java.util.*;
  4.  
  5. public class TopKMentionedFeatures {
  6.  
  7.     public static ArrayList<String> popularNFeatures(int numFeatures, int topFeatures, List<String> possibleFeatures,
  8.                                                      int numFeatureRequests, List<String> featureRequests){
  9.  
  10.         ArrayList<String> desiredFeatures = new ArrayList<String>(topFeatures); //result will be of size topFeatures
  11.  
  12.         //create the featureSet, we will look for thease features in the List of featureRequests!
  13.         HashSet<String> featureSet = new HashSet<>();
  14.         for( String feature :possibleFeatures)
  15.             featureSet.add(feature.toLowerCase());
  16.  
  17.         HashMap<String, Integer> featureMap = new HashMap<>();
  18.  
  19.         //iterate over the featureRequests, split into string array then check hashset
  20.         for(String feature: featureRequests){
  21.             String[] featuresDesired = feature.toLowerCase().split("\\s|\\.|\\?|!");
  22.  
  23.             HashSet<String> seenSet = new HashSet<>();
  24.             for(String word : featuresDesired){
  25.  
  26.                 if(featureSet.contains(word)){
  27.                     seenSet.add(word);
  28.                 }
  29.             } // String Array for loop ends
  30.             for(String setElement: seenSet)
  31.                 featureMap.put(setElement, featureMap.getOrDefault(setElement,0) + 1);
  32.  
  33.         }//outer forloop for list of features ends
  34.  
  35.         //create PQ
  36.         PriorityQueue<HashMap.Entry<String,Integer>> minHeap = new PriorityQueue<>((feature1,feature2) -> feature1.getValue() -feature2.getValue());
  37.  
  38.         for(HashMap.Entry<String, Integer> entry : featureMap.entrySet()){
  39.             minHeap.offer(entry);
  40.  
  41.             if(minHeap.size() > topFeatures)
  42.                 minHeap.poll();
  43.         }
  44.  
  45.         while (!minHeap.isEmpty())
  46.             desiredFeatures.add(0,minHeap.poll().getKey());
  47.  
  48.         return desiredFeatures;
  49.     }
  50.  
  51.  
  52.  
  53.  
  54.  
  55.     public static void main(String[] args){
  56.         List<String> featureRequests = new ArrayList<String>();
  57.         featureRequests.add("I wish my kindle had even more storage!");
  58.         featureRequests.add("I wish the battery life on my kindle lasted 2 years.");
  59.         featureRequests.add("I read in the bath and would enjoy a waterproof Kindle");
  60.         featureRequests.add("Waterproof and increased battery are my top two requests.");
  61.         featureRequests.add("I want to take my kindle into the shower. Waterproof please waterproof!" );
  62.         featureRequests.add("It would be neat if my kindle would hover on my desk when not in use.");
  63.         featureRequests.add("How cool would it be if my kindle charged in the sun via solar power?");
  64. //        System.out.println(featureRequests);
  65.  
  66.         List<String> possibleFeatures = new ArrayList<String>();
  67.         possibleFeatures.add("storage");
  68.         possibleFeatures.add("battery");
  69.         possibleFeatures.add("hover");
  70.         possibleFeatures.add("alexa");
  71.         possibleFeatures.add("waterproof");
  72.         possibleFeatures.add("solar");
  73. //        System.out.println(possibleFeatures);
  74.  
  75. //        for(String feature : possibleFeatures){
  76. //            String[] features = feature.toLowerCase().split()
  77. //        }
  78. //        String test = "I want to take my kindle into the shower. Waterproof please waterproof!";
  79. //        String[] array = test.toLowerCase().split("\\s|\\.|\\?|!");
  80. //        for(String words: array){
  81. //            System.out.println(words);
  82. //        }
  83.  
  84.         System.out.println();
  85.         System.out.println(popularNFeatures(6,2,possibleFeatures,7,featureRequests));
  86.     }
  87.  
  88. }
  89.  
Add Comment
Please, Sign In to add comment