首页 > 代码库 > JAVA 面试题: Find most frequency word in a paragraph

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