How to resolve the algorithm Word frequency step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Word frequency step by step in the Scala programming language
Table of Contents
Problem Statement
Given a text file and an integer n, print/display the n most common words in the file (and the number of their occurrences) in decreasing frequency.
For the purposes of this task:
Show example output using Les Misérables from Project Gutenberg as the text file input and display the top 10 most used words.
This task was originally taken from programming pearls from Communications of the ACM June 1986 Volume 29 Number 6 where this problem is solved by Donald Knuth using literate programming and then critiqued by Doug McIlroy, demonstrating solving the problem in a 6 line Unix shell script (provided as an example below).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Word frequency step by step in the Scala programming language
Source code in the scala programming language
import scala.io.Source
object WordCount extends App {
val url = "http://www.gutenberg.org/files/135/135-0.txt"
val header = "Rank Word Frequency\n==== ======== ======"
def wordCnt =
Source.fromURL(url).getLines()
.filter(_.nonEmpty)
.flatMap(_.split("""\W+""")).toSeq
.groupBy(_.toLowerCase())
.mapValues(_.size).toSeq
.sortWith { case ((_, v0), (_, v1)) => v0 > v1 }
.take(10).zipWithIndex
println(header)
wordCnt.foreach {
case ((word, count), rank) => println(f"${rank + 1}%4d $word%-8s $count%6d")
}
println(s"\nSuccessfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
}
You may also check:How to resolve the algorithm Function definition step by step in the Axe programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the jq programming language
You may also check:How to resolve the algorithm URL decoding step by step in the Arturo programming language
You may also check:How to resolve the algorithm Kolakoski sequence step by step in the V (Vlang) programming language