Skip to content

Instantly share code, notes, and snippets.

@cb372
Created May 10, 2016 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cb372/acce7b3b5f7a67dc2e0106e083d1ba43 to your computer and use it in GitHub Desktop.
Save cb372/acce7b3b5f7a67dc2e0106e083d1ba43 to your computer and use it in GitHub Desktop.
import scala.meta._
import java.nio.file._
object CommentWordCount extends App {
val scalaDir = "/Users/chris/code/scala"
val stdlibSrcDir = s"$scalaDir/src/library/scala"
val scalaFileContents: Array[String] = Files.walk(Paths.get(stdlibSrcDir)).toArray.collect {
case p: Path if p.toString.endsWith(".scala") => new String(Files.readAllBytes(p), "UTF-8")
}
val commentWords = scalaFileContents.flatMap{ string =>
val tokens = string.parse[Source].get.tokens
val commentTokens = tokens.filter(_.name == "comment")
val commentsAsStrings = commentTokens.map(_.toString)
commentsAsStrings.flatten(_.split(" ")).collect { case x if x.matches("""[a-zA-Z]{2,}""") => x.toLowerCase }
}.toSeq
println(commentWords.groupBy(l => l).map(t => (t._1, t._2.length)).toSeq.sortBy(_._2).reverse)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment