Source:
Package statistics English text file Most occurrences of the word;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.util.Map;
Import Java.util.Scanner;
Import Java.util.TreeMap;
Class zimu{
String name;
int num;
Zimu () {
Name=null;
Num=-1;
}
}
public class Tongji {
public static void Main (string[] args) throws IOException {
File File = new file ("Yingyuwenzhang.txt");//define a file object to initialize the FileReader
FileReader reader = new FileReader (file);//defines a FileReader object that is used to initialize BufferedReader
BufferedReader breader = new BufferedReader (reader),//new a BufferedReader object, reads the contents of the file into the cache
StringBuilder sb = new StringBuilder ();//define a string cache and store the string in the cache
String s = "";
while ((S =breader.readline ()) = null) {//Line reads the contents of the file, does not read the line break and the trailing space
Sb.append (s);//Adds a newline character to the read string and stores it in the cache.
}
Breader.close ();
String str = sb.tostring ();
Findenglishnum (str);
}
public static void Findenglishnum (String text) {
Scanner scan=new Scanner (system.in);
int i=0;
String[] Array = {".", ",", "?", "!"};
for (int i1 = 0; i1 < array.length; i1++) {
Text = Text.replace (Array[i1], "");
}
string[] Textarray = Text.split ("");
for (int i=0;i<textarray.length;i++)
// {
System.out.println (Textarray[i]);
// }
map<string, integer> map = new treemap<string, integer> ();
for (int i1 = 0; i1 < textarray.length; i1++) {
String key = Textarray[i1];
Convert to lowercase
String key_l = Key.tolowercase ();
if (! "". Equals (key_l)) {
Integer num = map.get (key_l);
if (num = = NULL | | num = = 0) {
Map.put (key_l, 1);
}else if (num > 0) {
Map.put (key_l, num+1);
}
}
}
For (String E:map.keyset ()) {
System.out.println ("Word:" +e+ "number of times:" +map.get (e));
i++;
}
Zimu [] z=new zimu[i];
for (int m=0;m<=i-1;m++) {
Z[m]=new Zimu ();
}
int j=0;
For (String E:map.keyset ()) {
if (z[j]!=null) {
z[j].name=e;
Z[j].num=map.get (e);
}
j + +;
}
for (int p=0;p<=i-1;p++) {
System.out.println ("Word:" +z[p].name+ "number of times:" +z[p].num);
}
Zimu t=new Zimu ();
for (int m=0;m<=i-1;m++)
{
for (int n=m;n<=i-1;n++) {
if (z[m]!=null&& (Z[m].num<z[n].num)) {
T=Z[M];
Z[m]=z[n];
z[n]=t;
}
}
}
System.out.println ("Please enter the words that you want to output several times before:");
int B=scan.nextint ();
for (int m=0;m<=b-1;m++) {
if (z[m]!=null) {
System.out.println ("Word:" +z[m].name+ "number of times:" +z[m].num);
}
}
}
}
Experiment Summary:
To be added.
Statistics the most frequently occurring words in English text files