簡體   English   中英

使用Java從文件中查找“ n”個最常見的單詞?

[英]Finding 'n' most frequent words from a file using Java?

我想讀取文件,並想收集前n個單詞,具體取決於單詞的頻率。

我嘗試了以下代碼來計算字符串中的每個單詞。

public static void main(String[] args) throws FileNotFoundException, IOException {
     FileReader fr = new FileReader("txtFile.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;
     System.out.println(text);
     uniqueLabels = getLabels(words);

     for (String l: uniqueLabels) {
         if ("".equals(l) || null == l) {
             break;
         }
         for (String s: words) {
             if (l.equals(s)) {
                 count++;
             }
         }
         System.out.println("Word :: " + l + " Count :: " + count);
         count = 0;
     }
 }

而且我使用以下代碼來收集來自link的唯一lbels( words )get,

private static String[] getLabels(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;
  }

而且效果很好,我想根據文件的頻率收集排名前10位的單詞。

首先,如果您希望它運行得比較快,請不要通過數組中的所有String循環...使用HashMap ...甚至不要為基元查找一些映射。

然后通過單詞。 如果單詞在映射中,則增加該值,否則輸入1。最后,對映射條目進行排序並獲取前10個。

不是全部重復,但此答案幾乎說明了如何完成計數: 在Java中計算句子中每個單詞的頻率

我建議使用Hashmap<String, Integer>()來計算單詞頻率。 哈希使用鍵值對。 這意味着鍵是唯一的(您的單詞)和值變量。 如果使用已經存在的密鑰執行放置操作,則該值將被更新。

哈希圖

這樣的事情應該起作用:

hashmap.put(key, hashmap.get(key) + 1);

為了獲得最重要的單詞,我將對哈希圖進行排序並檢索前十個條目。

我解決了

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;
                }
            }
            count=0;
        }
        display(n);
    } catch (Exception e) {
        System.err.println("ERR "+e.getMessage());
    }
}

public static void display(int n){
    for(int k=0; k<n; k++){
        System.out.println("Label :: "+w[k]+"\tCount :: "+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;
}

}

樣本輸出是

Enter 'n' value :: 
5
Label :: computer   Count :: 30
Label :: company    Count :: 22
Label :: express    Count :: 20
Label :: offer  Count :: 16
Label :: shearson   Count :: 16

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM