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...
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.
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...
Create a java program that has a code file with main() in it and another code...
Create a java program that has a code file with main() in it and another code file with a separate class. You will be creating objects of the class in the running program, just as the chapter example creates objects of the Account class. Your system handles employee records and processes payroll for them. Create a class called Employee that holds the following information: first name, last name, monthly salary, and sales bonus. The class should have all the gets...
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...
Document where I have # to explain what the code is doing. (5 of the 20...
Document where I have # to explain what the code is doing. (5 of the 20 points will be for documenting) Fill in the __ and __________ spaces to complete the code. Logic """ This program approximates the value of pi using an algorithm designed by the German mathematician Gottfried Leibniz. The algorithm is as follows: pi = 4 - 4 / 3 + 4 / 5 - 4 / 7 + . . . This program allows the user...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT