2016-07-29 85 views
1

我想读取一个文件,并且要收集顶部n单词取决于词频。使用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; 
    } 
} 

而且我用下面的代码,以收集独特lbels()得到,如果从link

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; 
    } 

而且这工作得很好,我想收集排名前十的话取决于它的文件中的频率。

+0

能使用'Apache的集合IO HashBag'。 – ifly6

+0

也应该切换到'List'而不是'String []'。 – ifly6

+0

不,你不应该,因为在这种情况下循环是一个坏主意。 – Silverclaw

回答

0

我解决它,

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 
3

首先,如果你想让它以适中的速度运行,不要循环遍历数组中的所有字符串......使用HashMap ...或者甚至为原语找到一些映射。

然后通过单词。如果单词在地图中,则增加该值,否则输入1. 最后,对地图条目进行排序并获取前10个。

不完全重复,但该答案几乎显示如何获取计数完成:Calculating frequency of each word in a sentence in java

+0

感谢您的编辑:D + rep –

+0

@Silverclaw,谢谢,当然我会尝试使用hashmap。并采取upvote :) –

2

我推荐使用Hashmap<String, Integer>()来计算字频率。哈希使用键值对。这意味着关键是唯一的(你的话)和价值变量。如果您使用已存在的键执行放入操作,该值将被更新。

Hashmap

像这样的东西应该工作:

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

让高层接话,我将实现排序HashMap和检索前十个条目。

+0

呃...我只是有另一个想法。由于整数是一个引用类型,如果该键不存在,你将有空+ 1. – Silverclaw

+0

好吧,但这可以很容易地解决try catch块... –

+0

@TobiasFriedinger,谢谢我会尝试..: ) –

相关问题