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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA 面试题: Find most frequency word in a paragraph相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

[转]SQL SERVER – Find Most Expensive Queries Using DMV

java中级面试题 之linux 与数据库

文件查找与压缩面试题

企业面试题-find结合sed查找替换

Google - Find Most People in Chat Log

HDU 1598 find the most comfortable road