Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javaapplication66;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. import java.util.Comparator;
  11. import java.util.HashMap;
  12. import java.util.HashSet;
  13. import java.util.LinkedHashMap;
  14. import java.util.LinkedList;
  15. import java.util.List;
  16. import java.util.Map;
  17.  
  18. /**
  19.  *
  20.  * @author Usuario
  21.  */
  22. public class JavaApplication66 {
  23.  
  24.    
  25.     public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
  26.     {
  27.         // Create a list from elements of HashMap
  28.         List<Map.Entry<String, Integer> > list =
  29.                new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());
  30.  
  31.         // Sort the list
  32.         Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
  33.             public int compare(Map.Entry<String, Integer> o1,  
  34.                                Map.Entry<String, Integer> o2)
  35.             {
  36.                 return (o2.getValue()).compareTo(o1.getValue());
  37.             }
  38.         });
  39.          
  40.         // put data from sorted list to hashmap  
  41.         HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
  42.         for (Map.Entry<String, Integer> aa : list) {
  43.             temp.put(aa.getKey(), aa.getValue());
  44.         }
  45.         return temp;
  46.     }
  47.    
  48.     public String mostCommonWord(String paragraph, String[] banned) {
  49.         //
  50.         String[] arr = paragraph.split(" ");
  51.        
  52.         for(int i =0; i<arr.length; i++) {
  53.             arr[i] = arr[i].toLowerCase().trim();
  54.             char ultimo = arr[i].charAt(arr[i].length() - 1);
  55.             //if( ultimo == ',' || ultimo == '.' || ultimo){
  56.             if(!Character.isLetter(ultimo)){
  57.                 arr[i] = arr[i].substring(0, arr[i].length() - 1);
  58.             }
  59.         }
  60.        
  61.         HashMap<String, Integer> hash_parag =
  62.                 new HashMap();
  63.         for(int i =0; i<arr.length; i++) {
  64.             if(hash_parag.containsKey(arr[i])) {
  65.                 hash_parag.put(arr[i], hash_parag.get(arr[i]) + 1);
  66.             }
  67.             else
  68.             {
  69.                hash_parag.put(arr[i], 1);
  70.             }
  71.         }
  72.  
  73.         HashMap<String, Integer> ord_hash_parag = sortByValue( hash_parag );
  74.  
  75.         HashSet<String> hash_banned =
  76.                 new HashSet();
  77.        
  78.         for(int i =0; i<banned.length; i++) {
  79.             hash_banned.add(banned[i]);
  80.         }
  81.        
  82.        
  83.         for(String key: ord_hash_parag.keySet()) {
  84.             if(!hash_banned.contains(key)) {
  85.                 return key;
  86.             }
  87.         }
  88.        
  89.         return "";
  90.     }
  91.    
  92.    
  93.     public static void main(String[] args) {
  94.         // TODO code application logic here
  95.        
  96.         String paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.";
  97.         String[] banned =  {"hit"};
  98.        
  99.         String[] arr = paragraph.split(" ");
  100.        
  101.         for(int i =0; i<arr.length; i++) {
  102.             arr[i] = arr[i].toLowerCase().trim();
  103.             char ultimo = arr[i].charAt(arr[i].length() - 1);
  104.             if( ultimo == ',' || ultimo == '.'){
  105.                 arr[i] = arr[i].substring(0, arr[i].length() - 1);
  106.             }
  107.         }
  108.        
  109.         HashMap<String, Integer> dic =
  110.                 new HashMap();
  111.        
  112.         for(int i =0; i<arr.length; i++) {
  113.             if(dic.containsKey(arr[i])) {
  114.                 dic.put(arr[i], dic.get(arr[i]) + 1);
  115.             }
  116.             else{
  117.                 dic.put(arr[i], 1);
  118.             }
  119.         }
  120.        
  121.         HashMap<String, Integer> hash = sortByValue(dic);
  122.        
  123.         for(String key: hash.keySet()) {
  124.             System.out.println(key + " " + hash.get(key) );
  125.         }
  126.        
  127.     }
  128.    
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement