📄 wordcount.java
字号:
import java.io.*;
import java.awt.*;
import java.util.*;
class Counter
{
private int i = 1;
int read() { return i; }
void increment() { i++; }
}
public class WordCount
{
FileReader file;//文件
private TreeMap[] counts=new TreeMap[197];//存各个单词
int mod=0,mostword=0,totalword=0,distinctword=0;//分别是最多,总数,不同单词
WordCount(String filename)throws FileNotFoundException
{
try
{
for(int i=0;i<197;i++)//初始化队列
{
counts[i]= new TreeMap();
}
file = new FileReader(filename); //打开一个文件
} catch(FileNotFoundException e)
{
System.err.println("Could not open " + filename);
throw e;
}
}
void cleanup() //关闭文件
{
try
{
file.close();
} catch(IOException e)
{
System.err.println("file.close() unsuccessful");
}
}
void countWords() //记单词的总数
{
try
{
BufferedReader st1 =new BufferedReader(file);//把文件读入缓冲
String line;
while((line = st1.readLine())!=null)//文件是否结束
{
StringTokenizer st=new StringTokenizer(line," ,.;?!/\"\f\t,'(',')'");
while(st.hasMoreTokens())
{
String s = st.nextToken();//取下一个单词
totalword ++;//单词总数加一
if(s.hashCode()>0)//取Hash码,如为负取为正
mod=(s.hashCode()%197);
else
mod=((-s.hashCode())%197);
if(counts[mod].containsKey(s))//对单词记数
((Counter)counts[mod].get(s)).increment();
else
counts[mod].put(s,new Counter());//放入队列
}
}
} catch(IOException e)
{
System.err.println("st.nextToken() unsuccessful");
}
}
Collection values() //返回值
{
return this.values();
}
Set keySet()//返回一个Set
{
return this.keySet();
}
Counter getCounter(String s,int i) //返回一个Counter对象
{
return (Counter)counts[i].get(s);//
}
/* public static void main(String[] args)
throws FileNotFoundException
{
WordCount wc = new WordCount("file.txt");
wc.countWords();
int listlong=0;
for(int i=0;i<197;i++)//找出最冲突最多的次数
{
if(wc.counts[i].size()>0) //是否有单词
{
if(listlong<wc.counts[i].size())//记录最多的冲突
{
listlong=wc.counts[i].size();
}
wc.distinctword += wc.counts[i].size();//不同单词数
Iterator keys = wc.counts[i].keySet().iterator();
System.out.print("counts["+i+"]:");
while(keys.hasNext())//某队列是否完成
{
String key = (String)keys.next();//取出关键字
System.out.print(key+" "+wc.getCounter(key,i).read()+"; ");//输出单词及次数
if(wc.getCounter(key,i).read()>wc.mostword)//找出出现最多的单词次数
wc.mostword = wc.getCounter(key,i).read();//记录出现最多的单词次数
}
System.out.println();
}
}
System.out.println("不同的单词共有"+wc.distinctword);
System.out.println("总单词数"+wc.totalword);
System.out.println("冲突最多的地址有单词"+listlong+"个冲突最多的地址分别是:");
for(int i=0;i<197;i++)//输出冲突最多的
{
if(wc.counts[i].size()==listlong) //找出冲突最多的
{
Iterator keys1 = wc.counts[i].keySet().iterator();
System.out.print("counts["+i+"]:");//输出
while(keys1.hasNext())
{
String key1 = (String)keys1.next();//取出关键字
System.out.print(key1+" :"+wc.getCounter(key1,i).read()+";");//输出单词及次数
}
System.out.println();
}
}
System.out.println("出现最多的单词是:");
for(int i=0;i<197;i++)//统计出现最多的单词
{
if(wc.counts[i].size()>0) //是否有单词
{
Iterator keys1 = wc.counts[i].keySet().iterator();//把SET转换成迭代器
while(keys1.hasNext())//某队列是否完成
{
String key1 = (String)keys1.next();//取出关键字
if(wc.getCounter(key1,i).read() == wc.mostword)//找出出现最多的单词
System.out.print(key1+" :"+wc.getCounter(key1,i).read()+";");//输出单词及次数
}
}
}
System.out.println();
wc.cleanup();//关闭文件
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -