3

我想在java中计算多个文件/文档的词频。

例如

a1 = {aaa,aaa,aaa,bbb}
a2 = {aaa, aaa, hhh}
a3 = {aaa, hhh, bbb, bbb}

所以,我想计算每个文件的词频:

for a1 file {aaa = 3, bbb = 1}
for a2 file {aaa = 2, hhh = 1}
for a3 file {aaa = 1, hhh = 1, bbb =2}

我有一种方法可以读取单词file,然后将其存储<wordname, worcount>LinkedHashMap. 尽管如此,它会计算所有文件的特定单词的频率,但我想分别计算每个文件的单词频率。

有没有人有任何解决方案?


然后,我写了这个:

Set mapset = fileToWordCount.keySet();           

for(Object filenameFromMap: mapset){
      System.out.println("FILENAME::"+filenameFromMap);
}

但是,它不打印任何东西。

4

2 回答 2

4

您可以创建另一个Map将文件名映射到LinkedHashMap带有字数的文件。所以你会有这样的事情:

Map<String, LinkedHashMap<String, Integer>> fileToWordCount = new HashMap<String, LinkedHashMap<String, Integer>();

然后,对于每个文件,您将像往常一样构建您的词频,并以这种方式将值添加到上面的地图中:

fileToWordCount.put(file.getPath(), wordCountMap);
于 2012-11-21T12:27:39.133 回答
0

导入 java.io。; 导入 java.util。;

public class file1{
 public static void main(String[] args) throws Exception{
HashMap<String,Integer> words_fre = new HashMap<String,Integer>();
HashSet<String> words = new HashSet<String>();
try{  

       File folder = new File("</file path>");
       File[] listOfFiles = folder.listFiles();

       BufferedReader bufferedReader=null;
       FileInputStream inputfilename=null;
       BufferedWriter out= new BufferedWriter(new OutputStreamWriter(new FileOutputStream("outfilename.txt",false), "UTF-8"));

        for(File file : listOfFiles){           
           inputfilename= new FileInputStream(file); 
           /*System.out.println(file); */    
           bufferedReader= new BufferedReader(new InputStreamReader(inputfilename, "UTF-8"));


             String s;
             while((s = bufferedReader.readLine()) != null){
               /*System.out.println(line);*/
                  s = s.replaceAll("\\<.*?>"," ");
                    if(s.contains("॥") || s.contains(":")|| s.contains("।")|| 
                     s.contains(",")|| s.contains("!")|| s.contains("?")){
                         s=s.replace("॥"," ");
                         s=s.replace(":"," ");
                         s=s.replace("।"," ");
                         s=s.replace(","," ");
                         s=s.replace("!"," ");
                         s=s.replace("?"," ");
                       }                                                   
                  StringTokenizer st = new StringTokenizer(s," ");
                  while (st.hasMoreTokens()) {         
                  /*out.write(st.nextToken()+"\n");*/
                  String str=(st.nextToken()).toString();
                  words.add(str);
                }
                for(String str : words){
                  if(words_fre.containsKey(str)){  
                           int a = words_fre.get(str);  
                           words_fre.put(str,a+1);             
                  }else{  
                      words_fre.put(str,1);/*uwords++;//unique words count */  
                  }                      
                }
                words.clear(); 

                  /*out.write("\n");
                  out.close();*/

             }             
             Object[] key =   words_fre.keySet().toArray();   
                  Arrays.sort(key);  
                  for (int i = 0; i < key.length; i++) {  
                    //System.out.println(key[i]+"= "+words_fre.get(key[i]));
                 out.write(key[i]+" : "+words_fre.get(key[i]) +"\n");
               }


         }

            out.close();
            bufferedReader.close();

      }catch(FileNotFoundException ex){
         System.out.println("Error in reading line");
        }catch(IOException ex){
            /*System.out.println("Error in reading line"+fileReader );*/
            ex.printStackTrace();
           }

} }

于 2017-01-29T18:10:57.650 回答