How to resolve the algorithm Word frequency step by step in the Phixmonti programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Word frequency step by step in the Phixmonti 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 Phixmonti programming language

Source code in the phixmonti programming language

include ..\Utilitys.pmt

"loading..." ?
"135-0.txt" "r" fopen var fn
" "
true
while
    fn fgets number? if drop fn fclose false else lower " " chain chain true endif
endwhile

"process..." ?
len for
    var i
    i get dup 96 > swap 123 < and not if 32 i set endif
endfor
split sort

"count..." ?
( ) var words
"" var prev
1 var n
len for
    var i
    i get dup prev ==
    if
        drop n 1 + var n
    else
        words ( n prev ) 0 put var words var prev 1 var n
    endif
endfor
drop
words sort
10 for
    -1 * get ?
endfor
drop

  

You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Action! programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the PL/I programming language
You may also check:How to resolve the algorithm Sorting algorithms/Radix sort step by step in the ATS programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the Mercury programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Haxe programming language