Question

In: Computer Science

Java code pick out 5 articles/ documents and have a program that find the top 20...

Java code

pick out 5 articles/ documents and have a program that find the top 20 bigrams that are in articles with the total number of times they are in each article. Then have the results in descending order. Also make sure that ./<> and other symbols are not included in the results

Solutions

Expert Solution

Below is the java program to find top 20 bigrams. You can replace the hardcoded strings to read data from files as well.

package demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import javax.sound.sampled.ReverbType;

public class abcd {
   public static void main(String[] args)
   {
       String article1 = "article in.<> the document and bigrams";
       String article2 = "documents and. have a program that find the top 20 bigrams that are in articles";
       String article3 = "this is a .random article or document or bigram";
       String article4 = "and bigrams /and bigrams";
       String article5 = "that<> are and bigrams that find top 20 bigrams";
      
      
       String[] articles = {article1, article2,article3,article4,article5};
       Bigram b = new Bigram(articles);
       HashMap<String, Integer> bigrams = b.controlled_func();
      
       HashMap<String, Integer> sorted_bigrams = sort_bigrams(bigrams);
      
       ArrayList keyList = new ArrayList(sorted_bigrams.keySet());
       System.out.println("Printing top 20 bigrams");
       for(int i=0; i< 20; i++)
       {
           System.out.println(keyList.get(i));
       }
      
      
   }
  
   public static HashMap<String, Integer> sort_bigrams(HashMap<String, Integer> bigrams)
    {
        List<Map.Entry<String, Integer> > my_list =
               new LinkedList<Map.Entry<String, Integer> >(bigrams.entrySet());


        Collections.sort(my_list, new Comparator<Map.Entry<String, Integer> >() {
            public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2)
            {
                return (e1.getValue()).compareTo(e2.getValue());
            }
        });
        
        HashMap<String, Integer> temp_list = new LinkedHashMap<String, Integer>();       
        for (Map.Entry<String, Integer> aa : my_list) {
            temp_list.put(aa.getKey(), aa.getValue());
        }
      
        ArrayList keyList = new ArrayList(temp_list.keySet());
        HashMap<String, Integer> return_temp_list = new LinkedHashMap<String, Integer>();
        for(int i=temp_list.size()-1; i>=0;i--){
            return_temp_list.put(keyList.get(i).toString(), temp_list.get(keyList.get(i)));
        }
        return return_temp_list;
    }

}

class Bigram{
  
   static String[] articles;
  
   public String[] getArticles() {
       return articles;
   }

   public void setArticles(String[] articles) {
       this.articles = articles;
   }

   public Bigram(String[] articles) {
       super();
       this.articles = articles;
   }


   public static List<String> getbigrams(String article)
   {
       List<String> bigrams = new ArrayList<String>();
       article = article.replace(".", "").replace("/", "").replace("<", "").replace(">", "");
       String[] words = article.split(" ");
       for(int i=0;i<words.length-1;i++)
       {
           bigrams.add(words[i].concat(" ").concat(words[i+1])   );
       }
       return bigrams;
   }
  
   public static HashMap<String, Integer> controlled_func()
   {
       HashMap<String, Integer> bigramcount = new HashMap<String, Integer>();
       for(int i=0; i< articles.length; i++ )
       {
           List<String> bigrams = getbigrams(articles[i]);
           for(String s : bigrams)
           {
               if(bigramcount.containsKey(s))
               {
                   bigramcount.put(s, bigramcount.get(s) + 1);
               }
               else
               {
                   bigramcount.put(s, 1);
               }
           }
       }
      
       return bigramcount;
   }
  
  
  
}


Related Solutions

I need original java code that completes this program and gets it to print out results...
I need original java code that completes this program and gets it to print out results just like in the example. Also please upload answer in a word document format only. Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times. Each time the search is conducted the number of checks (IE...
Java programming Write the max-heapify code and test it and then write the program to find...
Java programming Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.
Matlab code problems I have to write a code using functions to find out if a...
Matlab code problems I have to write a code using functions to find out if a year is a leap year. I'm on the write track i feel like but I keep getting an error message and matlab isnt helping to troubleshoot. The issue is on line 30. Here is my code: function project_7_mfp() %   PROJECT_7_ABC   project_7_abc() creates a function that prompts the user %   enter a year after 1582 It then determines if it is a leap year %...
write a java program. 10-20 lines of code You are a landscaper, one of your first...
write a java program. 10-20 lines of code You are a landscaper, one of your first tasks is to determine the cost of landscaping a yard. You charge a flat fee of $40 to landscape a yard, and an extra $10 a bag for raking leaves. Not all yards you work on have leaves that need to be raked. Houses without any leaves will be discounted $5. Using your program, use the JOption Pane to ask the homeowner to enter...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
Write out a generic java code to find a largest element from n comparable objects (default)...
Write out a generic java code to find a largest element from n comparable objects (default) and use a main method to test.
Write a java code that ask users to pick two of the job categories and enter...
Write a java code that ask users to pick two of the job categories and enter two working areas from each category. The users must enter the start and end time for each of the two selection and calculate the hours for each category and the total hours for all the categories. If the users total hours is more that 10 hours, they are awesome, if not, they are lazy. Shor and simple code using while or for loops. Thanks...
Program in Java code Write a program with total change amount in pennies as an integer...
Program in Java code Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. .Ex1: If the input is: 0 the output is:    No change            Ex2: If the input is:    45   the output is:   1 Quarter 2 Dimes
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT