码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA 面试题: Find most frequency word in a paragraph

时间:2016-08-31 15:36:11      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

My Answer:

package testJava;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
 
public class FindMostFrequency {
    ArrayList<String> words = new ArrayList<String>();
    HashMap<String, Integer> word_frequency = new HashMap<String, Integer>();
    HashMap<String, Integer> word_position = new HashMap<String, Integer>();
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
 
        FindMostFrequency find = new FindMostFrequency();
        find.readFile("d:/test.txt");
        HashMap<String, Integer>  result = find.findMostFrequencyWord();
        System.out.println(result);
    }
 
    public String replacePunc(String str) {
        return str.replaceAll("[\\pP]", " ");
    }
 
    public String[] convertStr(String str) {
        return str.split("\\s+");
    }
 
    public void readFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            try {
                BufferedReader br = new BufferedReader(new FileReader(file));
                String line = null;
                int linenum=1;
                while ((line = br.readLine()) != null) {
                    String[] wordList = this.convertStr(this.replacePunc(line));
                    int count = 0;
                    String word = null;
                    for (int i = 0; i < wordList.length; i++) {
                        word=wordList[i];
                        if (words.contains(word)) {
                            count = word_frequency.get(word) + 1;
                            word_frequency.put(word, count);
                        } else {
                            words.add(word);
                            word_frequency.put(word, 1);
                            this.word_position.put(word, linenum);
                        }
                    }
                    linenum++;
                }
                br.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
 
        } else {
            System.out.println("This file doesn‘t exist!");
        }
    }
 
    public HashMap<String,Integer> findMostFrequencyWord(){
        int max=0;
        HashMap<String,Integer> res = new HashMap<String,Integer>();
        Iterator<Entry<String, Integer>> it=this.word_frequency.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, Integer> entry=it.next();
            if(entry.getValue()>max){
                res = new HashMap<String,Integer>();
                res.put(entry.getKey(),this.word_position.get(entry.getKey()));
                max=entry.getValue();
            }
            else if(entry.getValue()==max){
                res.put(entry.getKey(),this.word_position.get(entry.getKey()));
            }
            else{
                continue;
            }
        }
        return res;
    }
}

 

JAVA 面试题: Find most frequency word in a paragraph

标签:

原文地址:http://www.cnblogs.com/tianm2016/p/5825874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!