Search and rank docs in java


How to Search and Rank Documents in Java
here is Example of Java code

java source code example

package rankeword;
import java.io.*;
import java.io.IOException;
import javax.swing.JOptionPane;
import java.util.*;
import java.util.Map.*;

public class RanKeword {

 
     public static void main(String[] arg) throws IOException{
   
System.out.println("Search a word \n");
       Scanner console = new Scanner(System.in);
        String searchWord = console.next();
        
        searchWord = searchWord.toLowerCase();
        System.out.println();
        int counter=0;
        FileReader fr = new FileReader("d:/plotsubset.txt");
        BufferedReader br = new BufferedReader(fr);
        Scanner inputfile = new Scanner (br);
        FileOutputStream fos = new FileOutputStream("d:/java/kk" + counter + ".txt");   
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));  
        System.out.println("Rank\t Phrase\t\t\tMovies");
         while(inputfile.hasNextLine()){
        String input1 = inputfile.nextLine();
         
            String input = input1.toLowerCase();
          
        String[] split = input.split(" ");
         
        Map counts = new HashMap();
        for(int i=0; i[] entries = counts.entrySet().toArray(new Map.Entry[0]);
        Arrays.sort(entries, new Comparator>() {
            @Override
            public int compare(Map.Entry o1, Map.Entry o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        for(Map.Entry entry:entries){
         //   rank++;
            int count = entry.getValue();
             
            if(count>1){
  
        System.out.printf(" %4d \t %s\t\t %s\n", count,entry.getKey(),input);
        
        }
         
      }
        
     }
     
    }
         
     }
}

//Result
run:
Search a word

comedy

Rank   Phrase   Movies 3   about comedy   pl: #cake is a hour-long about comedy serial about comedy narrative comedy about comedy a manhunt for a high
3  comedy comedy  pl: #cake is a hour-long about comedy serial about comedy narrative comedy about comedy a manhunt for a high
2   about comedy comedy  pl: #cake is a hour-long about comedy serial about comedy narrative comedy about comedy a manhunt for a high
2   about comedy comedy comedy   pl: #cake is a hour-long about comedy serial about comedy narrative comedy about comedy a manhunt for a high
2   comedy comedy comedy   pl: #cake is a hour-long about comedy serial about comedy narrative comedy about comedy a manhunt for a high
2   who comedy  pl: who track him, and a enthused entrepreneur who runs comedy a bitcoin mining
2   of comedy  pl: #elmira elmira elmira follows the story of a bunch of comedy strangers who all respond to the
2   elmira comedy  pl: #elmira elmira elmira follows the story of a bunch of comedy strangers who all respond to the

BUILD SUCCESSFUL (total time: 4 seconds)


Sort words in java

How to Sort words using map in java?
here is Example of Java code



package sortmap;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class SortMap {

   
    public static void main(String[] args) {
                Map map = new HashMap();
        map.put("C sharp", 20);
        map.put("javascript", 45);
        map.put("delphi", 2);
        map.put("Linux", 67);
        map.put("java", 26);
        map.put("C++", 93);
        Set> set = map.entrySet();
        List> list = new ArrayList>(set);
        Collections.sort( list, new Comparator>()
        {
            public int compare( Map.Entry o1, Map.Entry o2 )
            {
                return (o2.getValue()).compareTo( o1.getValue() );
            }
        } );
        for(Map.Entry entry:list){
            System.out.println(entry.getKey()+" ==== "+entry.getValue());
        }
    } 
}