How to resolve the algorithm Word frequency step by step in the Frink programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Word frequency step by step in the Frink 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 Frink programming language
Source code in the frink programming language
d = new dict
for w = select[wordList[read[normalizeUnicode["https://www.gutenberg.org/files/135/135-0.txt", "UTF-8"]]], %r/[[:alnum:]]/ ]
d.increment[lc[w], 1]
println[join["\n", first[reverse[sort[array[d], {|a,b| a@1 <=> b@1}]], 10]]]
formatTable[first[countToArray[select[wordList[lc[normalizeUnicode[read["https://www.gutenberg.org/files/135/135-0.txt", "UTF-8"]]]], %r/[[:alnum:]]/ ]], 10], "right"]
You may also check:How to resolve the algorithm Find limit of recursion step by step in the Uxntal programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Conway's Game of Life step by step in the R programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Lhogho programming language
You may also check:How to resolve the algorithm Longest string challenge step by step in the Kotlin programming language