Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import java.io.File
  2. import scala.concurrent.Future
  3. import scala.concurrent.ExecutionContext.Implicits.global
  4.  
  5.  
  6. class GetWordCount{
  7.  
  8. def inFileCount(source: File):Future[Map[String,Int]]= {
  9.  
  10. val futureMap:Future[Map[String,Int]]=Future {
  11. val wordList = scala.io.Source.fromFile(source.toString).getLines.flatMap(_.split("\W+")).toList
  12. val wordMap = wordList.groupBy(a => a)
  13. wordMap.map { case (k, v) => (k, v.length) }
  14. }
  15. futureMap
  16.  
  17. }
  18.  
  19. def getWordCount(dir: String): Future[Map[String,Int]]= {
  20.  
  21. val file = new File(dir)
  22. require(file.exists() && file.isDirectory)
  23. Future.sequence(file.listFiles.filter(_.isFile).map { a => inFileCount(a) }.toList) map{
  24. list => list.fold(Map())((a,b) =>a ++ b.map{ case (k,v) => k-> (v+a.getOrElse(k,0)) })
  25. }
  26.  
  27. }
  28.  
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement