在Java中从文本文件中查找最频繁和最不频繁的单词

在Java中从文本文件中查找最频繁和最不频繁的单词,java,count,frequency,decoding,Java,Count,Frequency,Decoding,这是我在网上练习java问题时遇到的一个问题。我专注于寻找最频繁的单词,因为我认为编码最不频繁的单词会很容易。我最终成功地编码了最频繁的单词部分,但我无法编码最不频繁的单词部分。期待你们的帮助 提前谢谢 这是最常见部分的代码 import java.io.*; public class wordFreq { private static String[] w = null; private static int[] r = null; public static void main(String

这是我在网上练习java问题时遇到的一个问题。我专注于寻找最频繁的单词,因为我认为编码最不频繁的单词会很容易。我最终成功地编码了最频繁的单词部分,但我无法编码最不频繁的单词部分。期待你们的帮助 提前谢谢 这是最常见部分的代码

import java.io.*;
public class wordFreq {
private static String[] w = null;
private static int[] r = null;
public static void main(String[] args){
    try {
        System.out.println("Enter 'n' value :: ");
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        w = new String[n];
        r = new int[n];
        FileReader fr = new FileReader("acq.txt");
        BufferedReader br = new BufferedReader(fr);
        String text = "";
        String sz = null;
        while((sz=br.readLine())!=null){
            text = text.concat(sz);
        }
        String[] words = text.split(" ");
        String[] uniqueLabels;
        int count = 0;
        uniqueLabels = getUniqLabels(words);
        for(int j=0; j<n; j++){
                r[j] = 0;
            }
        for(String l: uniqueLabels)
        {
            if("".equals(l) || null == l)
            {
                break;
            }           
            for(String s : words)
            {
                if(l.equals(s))
                {
                    count++;
                }               
            }

            for(int i=0; i<n; i++){
                if(count>r[i]){
                    r[i] = count;
                    w[i] = l;
                    break;
                }
               /* else if(count==1){
                    System.out.println("least frequent");
                    System.out.println("("+w[i]+":"+r[i]+"),");
                }*/
            }

            count=0;
        }
        display(n);
    } catch (Exception e) {
        System.err.println("ERR "+e.getMessage());
    }
}

public static void display(int n){
    System.out.println("Most Frequent");
    for(int k=0; k<n; k++){
        System.out.print("("+w[k]+":"+r[k]+"),");
    }
}

private static String[] getUniqLabels(String[] keys)
{
    String[] uniqueKeys = new String[keys.length];

    uniqueKeys[0] = keys[0];
    int uniqueKeyIndex = 1;
    boolean keyAlreadyExists = false;

    for(int i=1; i<keys.length ; i++)
    {
        for(int j=0; j<=uniqueKeyIndex; j++)
        {
            if(keys[i].equals(uniqueKeys[j]))
            {
                keyAlreadyExists = true;
            }
        }           

        if(!keyAlreadyExists)
        {
            uniqueKeys[uniqueKeyIndex] = keys[i];
            uniqueKeyIndex++;               
        }
        keyAlreadyExists = false;
    }       
    return uniqueKeys;
}

}
import java.io.*;
公共类wordFreq{
私有静态字符串[]w=null;
私有静态int[]r=null;
公共静态void main(字符串[]args){
试一试{
System.out.println(“输入'n'值::”);
扫描仪输入=新扫描仪(系统输入);
int n=in.nextInt();
w=新字符串[n];
r=新整数[n];
FileReader fr=新的FileReader(“acq.txt”);
BufferedReader br=新的BufferedReader(fr);
字符串文本=”;
字符串sz=null;
而((sz=br.readLine())!=null){
text=text.concat(sz);
}
String[]words=text.split(“”);
字符串[]唯一标签;
整数计数=0;
uniqueLabels=getUniqLabels(文字);

对于(int j=0;jmap
如何快速而肮脏的解决方案?然后只取最大值和最小值(及其键)。这段代码真的有效吗?
getUniqLabels
中有一个主要缺陷things@AxelH.SortedMap,具体地说,据我所知,这是无问题的代码。请向我们展示最不常见的部分的代码,并详细解释问题所在。可能重复的