Counting words, letters, number and frequency of

A: the statistical frequency of occurrence of letters in the file (case sensitive) is given, and pressing the reverse output frequency of occurrence

Ideas: 1, the contents of the file into the StringBuffer;

            2, the content read is divided;

            3, through the array, which is placed in a TreeMap <String, Integer> in;

            4, its calculated frequency sorting;

package file;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeMap;
import java.util.TreeSet;
public class zimupaixu
{
    public static void main(String [] args) throws IOException {
        //读取文件
        BufferedReader br=new BufferedReader(new FileReader("F:\\\\备用\\\\english.txt"));        
        TreeMap<Character,Integer> tm=new TreeMap<>();//打开哈希表
        int  key;
        while((key=br.read())!=-1) {    
             if((key>='A'&&key<='Z')||(key>'a'&&key<='z')) {
                 tm.put (( char ) Key, tm.containsKey (( char ) Key) tm.get ((? char ) Key) +1: 1); // cumulative number of different letters appear 
            }
            
            }    
        br.close();
        int max=0;
        int sum=0;
        int t=0;
        for(Character k: tm.keySet()) {
            sum=sum+tm.get(k);
          }
        TreeSet<Character> ts=new TreeSet<>(new Comparator<Character>()
        {
            public int compare(Character a,Character b) {
                int num=tm.get(a)-tm.get(b);
                return num==0?1:(-num);
            }
        });
        for(Character k: tm.keySet()) {
            ts.add(k);
        }
        DecimalFormat df = new DecimalFormat("0.00%");
        for (Character c : ts)
        {
            float bai=(float)tm.get(c)/sum;
           
            System.out.println (C + "" + "appears percentage" + "" + df.format (Bai));
        }
          
        
        
        //System.out.println(sum);
}}

Second, the first N English word most commonly occurring single output file

Ideas: 1, from beginning to end through the file, read from a file to traverse every word.

           2, the traverse to put words in TreeHasp, and count the number of times the word appears.

           3, defines how many words a global variable N control output

           4, the number of cycles for all the words are compared to obtain the largest number of occurrences, cycle N times.

package file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class danci {
    class Word//定义单词类
    {
        Value String; // particular word 
        int COUNT; // number appearing 
        Word Next; // the word chain together 
        public Word (String value, int geshu) // parameterized constructor 
        {
             the this .Value = value;
             the this . = COUNT geshu;
            next=null;
        }
        public Word () // constructor 
        {
             the this .Value = "" ;
             the this .count = 0 ;
            next=null;
        }
    }    
    /*
     * Read the file and directory names in the specified path
     */
    public void getFileList() throws IOException {
        BufferedReader br=new BufferedReader(new FileReader("F:\\备用\\english.txt"));        
        TreeMap<Character,Integer> hm=new TreeMap<>();
        int  key;
        while((key=br.read())!=-1) {    
                    Word Word = new new Word (); // word chain head 
                    Word lian, xin;                                             
                    STR String = "" ;
                     char [] = C new new  char [. 1]; // read one letter 
                    int B = 0 ;
                     Boolean exist = to false ; // determines whether there is a word in the word chain, 
                    the while ((B = br.read (C)) = -!. 1) // read letters 
                    {
                         // divided 
                        IF (String.valueOf (C) .equals ( "\ R & lt") || String.valueOf (C) .equals ( "\ n ") || String.valueOf (c) .equals (" ") || String.valueOf (c) .equals (", ") || String.valueOf (c) .equals (". ") || String .valueOf (c) .equals ( "\ " ") || String.valueOf (c) .equals ("'"))
                        {
                            lian=word;
                            while(lian!=null)            
                            {
                                IF (lian.value.equalsIgnoreCase (STR)) // total number of different words 
                                {
                                    lian.count++;exist=true;
                                    break;
                                }
                                else
                                {
                                    lian=lian.next;
                                }
                            }
                            IF (== exist to false ) // If not, the word is added in the chain 
                            {
                                xin=new Word(str,1);
                                xin.next=word.next;
                                word.next=xin;
                                str="";
                            }
                            else
                            {
                                exist=false;
                                str="";
                            }
                        }
                        else 
                        {
                            str+=String.valueOf(c);
                        }
                    }
                    System.out.print ( "the number of the first N longest appearance of the word:" );
                    Scanner scan=new Scanner(System.in);
                    int N=scan.nextInt();
                    System.out.println("");
                    for(int i=1;i<=N;i++)                   
                    { // The N output corresponding to the input words, and the number thereof 
                        XIN = new new Word ( "", 0 );
                        lian=word.next;
                        while(lian!=null)
                        {
                            if(lian.count>xin.count)
                            {
                                XIN = Lian;
                            }
                            lian=lian.next;
                        }
                        System.out.println(+i+"  :"+xin.value+" "+xin.count);
                        Lian = Word;
                         // delete the word chain in the most number of words 
                        the while (lian.next =! null )
                        {
                            if(lian.next.value.equalsIgnoreCase(xin.value))
                            {
                                lian.next=lian.next.next;
                                break;
                            }
                            lian=lian.next;
                        }
                    }
                    
                }            
            }         
    public static void main(String[] args) throws IOException {
        RF danci = new new danci ();
        rf.getFileList();
    }

Code Screenshot:

 

 Three: the specified file directory, but all subdirectories recursively directory, the number of execution statistics for each file and the percentage of words

package file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;

public  class dancichachong
{
    //F:\\\\\\\\备用\\\\\\\\english.txt
   public static void main(String[] args) throws IOException
{
       System.out.println ( "Please enter the path to traverse file:" );
       Scanner sc=new Scanner(System.in);
        File file=getfile();
        bianli (file);
        System.out.println ( "Please select the document you want to query" );
        String name=sc.next();
        display(name);

        
       
        
}
    public  static  void bianli (File File) {
        File [] yue=file.listFiles();
        for(File fi:yue) {
            if(!fi.isDirectory()) {
            if(fi.getName().endsWith(".txt")) {
                System.out.println(fi.getPath());
            }
            }
            else {
                bianli (fi);
            }
        }
      
    }
   public static void display(String path) throws IOException {
       Scanner sc=new Scanner (System.in);
       BufferedReader br=new BufferedReader(new FileReader(path));        
        int c;
        TreeMap<String,Integer> tm=new TreeMap<>();
        String line;
        int kt=0;
        while((line=br.readLine())!=null) {
            String [] str=line.split("[^a-zA-Z]");            
            for(int i=0;i<str.length;i++) {
                if(!str[i].equals("")) {
            tm.put(str[i],tm.containsKey(str[i])?tm.get(str[i])+1:1);}
            }
            }    
        br.close();
        int max=0;
        int sum=0;
        int t=0;
        for(String k: tm.keySet()) {
            sum=sum+tm.get(k);
            if(max<=tm.get(k)) {
                max=tm.get(k);
            }
          }
       
       TreeSet<String> ts=new TreeSet<>(new Comparator<String>()
        {
           public int compare(String a,String b) {
               int num=tm.get(a)-tm.get(b);
               return num==0?1:(-num);
           }
        });
       for(String k: tm.keySet()) {
           ts.add(k);
       }
       DecimalFormat df = new DecimalFormat("0.00%");
        System.out.println ( "Please enter the number to query" );
        int COUNT = sc.nextInt ();
        int Q = 0, I =. 1 ;
        for (String S: TS)
        {
           if(q==count) {
               break;
           }
           else {
               q++;
               float bai=(float)tm.get(s)/sum;
               System.out.println(i+" "+s+" "+tm.get(s)+" "+df.format(bai));
           }
          i++;    
        }
          
   }
   public static File getfile() {
       Scanner sc=new Scanner(System.in);
       while(true) {
       String line=sc.nextLine();
       File kk=new File(line);
       if(!kk.exists()) {
           System.out.println ( "do not enter a folder, please re-enter" );
       }
       else if(kk.isFile()) {
           System.out.println ( "Enter the file path, please re-enter" );
       }
       else {
           return kk;
       }
       
}
}
}

 

Code test shots:

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/hhjing/p/11795598.html