您的位置:首页 > 其它

删除一篇文章中出现次数最多的单词

2015-03-09 20:44 393 查看
今天记起来师兄找工作时面试的一个题目,觉得还蛮有意思的,就想了想,用java代码实现了一下(只是目前想到的一种方法,还有想到的方法后续补上)。
需要删除文章中的出现次数最多的单词:无非就是遍历全部的单词,找到出现次数最多的单词,输出的时候不给予输出即可。
代码还有缺陷:
1,正则表达式需要匹配标点符号,目前没有匹配标点,输出文章中无法断句。
贴出代码:
</pre><pre name="code" class="java">import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
* 方法:1,通过java io读取txt文本,存放到list中,
* 		    2,再遍历list,将单词作为key值,出现的次数作为key值
* 			3,使用keySet方法,遍历map,并找出出现次数最多的单词
* 			4,拿到出现次数最多的单词之后,在讲list输出到文本的时候,不输出该单词即可
* 删除一篇文章中出现次数最多的单词
* 1,单词读取的正则表达式
* 2,遍历map的方法
* 3,判断字符串相等使用equals方法,==是判断两个对象是否相等的方法
* 4,文本的读取和写入方法
*/
public class DeleteWord {

private static final File SOURCE = new File("D:/bigData/", "targent.txt");
private static final File RESULTSOURCE = new File("D:/bigData/", "targentcopy.txt");
//读取targent.txt文件,存放到tempList中
private static List<String> tempList=new ArrayList<>();
//hashMap存放单词和出现的次数
private static Map<String, Integer>tempMap =new HashMap<String, Integer>();
//记录查找到的最大单词
private static String  maxWord=null;
private static int max=0;

public static void main(String[] args) {
//读取文本文件
readArticle();
//将tempList中的单词装入map中,并找出map中出现次数最多的一个单词
findMap();
//输出内存数组中的文章
writeArticle();
System.out.println(maxWord+":"+max);
}

/*
* 输出文章
*/
private static void writeArticle() {
long startTime=System.currentTimeMillis();
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(RESULTSOURCE)));
for(String word:tempList){
//判断字符串相等使用equals方法
if (!word.equals(maxWord)) {
bw.write(word+" ");
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
long endTime=System.currentTimeMillis();
System.out.println("write time:"+(endTime-startTime)+"ms");
}
}

/*
* 查找hashMap中value值最大的key值,即为出现次数最多的单词
*/
private static void findMap() {
long startTime=System.currentTimeMillis();
int temp=0;
//将单词存入map中
for(String key:tempList){
if(tempMap.containsKey(key))
tempMap.put(key, tempMap.get(key)+1 );
else
tempMap.put(key, 1);
}
//使用keySet来遍历map,获取出现value最大的单词
for(String key:tempMap.keySet()){
temp=tempMap.get(key);
if(max<temp){
max=temp;
maxWord=key;
}
}
long endTime=System.currentTimeMillis();
System.out.println("findMaxTimesWord time="+(endTime-startTime)+"ms");
}

/*
* 从文本中读取文章并使用正则表达式取出单词
*/
private static void readArticle() {
long startTime=System.currentTimeMillis();
BufferedReader br=null;
String line=null;
//正则表达式后续需要补充,目前只能匹配单词,无法匹配标点符号
String reg="[a-z|A-Z|0-9|,|.|!|?|;]+";
Pattern p=Pattern.compile(reg);
try {
br=new BufferedReader(new InputStreamReader(new FileInputStream(SOURCE)));
while((line=br.readLine())!=null){
Matcher mat=p.matcher(line);
while(mat.find()){
tempList.add(mat.group());
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
long endTime=System.currentTimeMillis();
System.out.println("readArticle time="+(endTime-startTime)+"ms");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: