单词频率计数为2个文件

时间:2015-05-05 15:57:54

标签: java count

我编写了Java代码来计算出现次数。它使用2 .txt个文件作为输入,并提供单词和频率作为输出。

我还要打印,哪个文件包含给定单词的次数。你知道怎么做吗?

public class JavaApplication2
{

    public static void main(String[] args) throws IOException
    {     
        Path filePath1 = Paths.get("test.txt");
        Path filePath2 = Paths.get("test2.txt");

        Scanner readerL = new Scanner(filePath1);
        Scanner readerR = new Scanner(filePath2);

        String line1 = readerL.nextLine();
        String line2 = readerR.nextLine();

        String text = new String();
        text=text.concat(line1).concat(line2);

        String[] keys = text.split("[!.?:;\\s]");
        String[] uniqueKeys;
        int count = 0;
        System.out.println(text);
        uniqueKeys = getUniqueKeys(keys);

        for(String key: uniqueKeys)
        {
            if(null == key)
            {
                break;
            }           
            for(String s : keys)
            {
                if(key.equals(s))
                {
                    count++;
                }               
            }
            System.out.println("["+key+"] frequency : "+count);
            count=0;
        }
    }

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

4 个答案:

答案 0 :(得分:1)

首先,不要使用数组来表示唯一键,而是使用HashMap<String, Integer>。它效率更高。

您最好的选择是分别对每个行/文件进行处理,并分别存储这些计数。然后将两个计数合并以获得整体频率。

更多细节:

String[] keys = text.split("[!.?:;\\s]");
HashMap<String,Integer> uniqueKeys = new HashMap<>();

for(String key : keys){
    if(uniqueKeys.containsKey(key)){
        // if your keys is already in map, increment count of it
        uniqueKeys.put(key, uniqueKeys.get(map) + 1);
    }else{
        // if it isn't in it, add it
        uniqueKeys.put(key, 1);
    }
}

// You now have the count of all unique keys in a given text
// To print them to console

for(Entry<String, Integer> keyCount : uniqueKeys.getEntrySet()){
    System.out.println(keyCount.getKey() + ": " + keyCount.getValue());
}

// To merge, if you're using Java 8

for(Entry<String, Integer> keyEntry : uniqueKeys1.getEntrySet()){
    uniqueKeys2.merge(keyEntry.getKey(), keyEntry.getValue(), Integer::add);
}

// To merge, otherwise

for(Entry<String, Integer> keyEntry : uniqueKeys1.getEntrySet()){
    if(uniqueKeys2.containsKey()){
        uniqueKeys2.put(keyEntry.getKey(),
            uniqueKeys2.get(keyEntry.getKey()) + keyEntry.getValue());
    }else{
        uniqueKeys2.put(keyEntry.getKey(), keyEntry.getValue());
    }
}

答案 1 :(得分:0)

UPDATE:单词出现的代码(感谢@George)

此示例适用于文件,您可以将其用于多个文件:

@property(nonatomic) PFObject *box;

答案 2 :(得分:0)

import java.io. ; import 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("/home/jsrathore/Dropbox/Semester 6th/IR_Lab/lab_01/one");
       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();
           }

} }

答案 3 :(得分:0)

最新答案,但是如果有多个文件,下面的代码将有效地计算单词频率

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

public class WordCounter implements Runnable {
    private final Scanner scanner;
    private Map<String, AtomicLong> sharedCounter;

    public WordCounter(Scanner scanner, Map<String, AtomicLong> sharedCounter) {
        this.scanner = scanner;
        this.sharedCounter = sharedCounter;
    }

    public void run() {
        if (scanner == null) {
            return;
        }

        while (scanner.hasNext()) {
            String word = scanner.next().toLowerCase();
            sharedCounter.putIfAbsent(word, new AtomicLong(0));
            sharedCounter.get(word).incrementAndGet();
        }
    }

    public static void main(String[] args) throws IOException {
        // Number of parallel thread to run
        int THREAD_COUNT = 10;

        List<Path> paths = new ArrayList<>();
        // Add path
        paths.add(Paths.get("test1.txt"));
        paths.add(Paths.get("test2.txt"));

        // Shared word counter
        Map<String, AtomicLong> sharedCounter = new ConcurrentHashMap<>();

        ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);

        for (Path path : paths) {
            executor.execute(new WordCounter(new Scanner(path), sharedCounter));
        }
        executor.shutdown();
        // Wait until all threads are finish
        while (!executor.isTerminated()) {
        }
        System.out.println(sharedCounter);
    }
}