2017-12-01 24 views
1

ディレクトリ内のファイル数を最初に数え、各ファイル内で単語数を与えるという割り当てを実行しようとしています。私はファイル数は問題ありませんが、私はインストラクターが私に与えたコードを簡単な単語カウントに周波数カウントするクラスから変換するのに苦労しています。さらに、私は単語をカウントするために各ファイルを見るための適切なコードを見つけることができないようです(私は特定のテキストではなく "汎用のもの"を探していますが、特定のテキストファイルを使ってプログラムをテストしようとしています) 。テキストファイル、Java 8スタイルで単語を数える方法

primes.txt 
but 
are 
sometimes 
sense 
refrigerator 
make 
haiku 
dont 
they 
funny 
word length: 1 ==> {but=1, are=1, sometimes=1, sense=1, refrigerator=1, make=1, haiku=1, dont=1, they=1, funny=1} 

..... 

Count 11 files: 

を私は2つのクラスを使用しています:WORDCOUNTとFileCatch8

WORDCOUNT:

import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.AbstractMap.SimpleEntry; 
import java.util.Arrays; 
import java.util.Map; 
import static java.util.stream.Collectors.counting; 
import static java.util.stream.Collectors.groupingBy; 

    /** 
    * 
    * @author 
    */ 
    public class WordCount { 

     /** 
     * 
     * @param filename 
     * @return 
     * @throws java.io.IOException 
     */ 
     public Map<String, Long> count(String filename) throws IOException { 
      //Stream<String> lines = Files.lines(Paths.get(filename)); 
      Path path = Paths.get("haiku.txt"); 
      Map<String, Long> wordMap = Files.lines(path) 
        .parallel() 
        .flatMap(line -> Arrays.stream(line.trim().split(" "))) 
        .map(word -> word.replaceAll("[^a-zA-Z]", "").toLowerCase().trim()) 
        .filter(word -> word.length() > 0) 
        .map(word -> new SimpleEntry<>(word, 1)) 
        //.collect(Collectors.toMap(s -> s, s -> 1, Integer::sum)); 
        .collect(groupingBy(SimpleEntry::getKey, counting())); 

      wordMap.forEach((k, v) -> System.out.println(String.format(k,v))); 
      return wordMap; 
     } 
    } 

Count 11 files: 
word length: 1 ==> 80 
word length: 2 ==> 321 
word length: 3 ==> 643 

しかし、これは代わりに出力されているものである:これは意図された出力であります

およびFileCatch:

import java.io.IOException; 
import java.nio.file.DirectoryStream; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.List; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author 
*/ 
public class FileCatch8 { 
    public static void main(String args[]) { 
     List<String> fileNames = new ArrayList<>(); 
     try { 
      DirectoryStream<Path> directoryStream = Files.newDirectoryStream 
     (Paths.get("files")); 
      int fileCounter = 0; 
      WordCount wordCnt = new WordCount(); 
      for (Path path : directoryStream) { 
       System.out.println(path.getFileName()); 
       fileCounter++; 
       fileNames.add(path.getFileName().toString()); 
       System.out.println("word length: " + fileCounter + " ==> " + 
         wordCnt.count(path.getFileName().toString())); 
} 
     } catch(IOException ex){ 
    } 
    System.out.println("Count: "+fileNames.size()+ " files"); 

    } 
} 

プログラムは

+0

んが、新しいSimpleEntry <>(単語は、1)1へのマップ値を毎回設定されていますか? –

+0

SimpleEntryを作成せずにSimpleEntry :: getKeyを使用することなく、Function.identity()を使用できます。ファイルを簡単にするために、Files.walkを見てみてください。 – egorlitvinenko

+0

そうだと思います。そんなことをするのは間違っていますか? –

答えて

4

Wordの例カウント8つのストリームラムダ構文を使用してJavaを使用しています。

Files.lines(Paths.get(file)) 
    .flatMap(line -> Arrays.stream(line.trim().split(" "))) 
    .map(word -> word.replaceAll("[^a-zA-Z]", "").toLowerCase().trim()) 
    .filter(word -> !word.isEmpty()) 
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); 

ファイルの数:

私の意見では
Files.walk(Paths.get(file), Integer.MAX_VALUE).count(); 
Files.walk(Paths.get(file)).count(); 
0

、最も簡単な方法は、単語をカウントしますJava 8を使用するファイルでは次のようになります。

Long wordsCount = Files.lines(Paths.get(file)) 
    .flatMap(str->Stream.of(str.split("[ ,.!?\r\n]"))) 
    .filter(s->s.length()>0).count(); 
System.out.println(wordsCount); 

そして、すべてのファイルをカウントする:

Long filesCount = Files.walk(Paths.get(file)).count(); 
System.out.println(filesCount); 
+0

それはいいですが、ファイルのディレクトリを数えたい場合は、DirectoryStreamを使用する必要はありませんか? –

+0

はい、DirectoryStreamを使用してディレクトリ内のエントリを反復処理できます。私はあなたにもう一つのアプローチを示しました。 –

関連する問題