How to resolve the algorithm Word frequency step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Word frequency step by step in the Prolog 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 Prolog programming language
Source code in the prolog programming language
print_top_words(File, N):-
read_file_to_string(File, String, [encoding(utf8)]),
re_split("\\w+", String, Words),
lower_case(Words, Lower),
sort(1, @=<, Lower, Sorted),
merge_words(Sorted, Counted),
sort(2, @>, Counted, Top_words),
writef("Top %w words:\nRank\tCount\tWord\n", [N]),
print_top_words(Top_words, N, 1).
lower_case([_], []):-!.
lower_case([_, Word|Words], [Lower - 1|Rest]):-
string_lower(Word, Lower),
lower_case(Words, Rest).
merge_words([], []):-!.
merge_words([Word - C1, Word - C2|Words], Result):-
!,
C is C1 + C2,
merge_words([Word - C|Words], Result).
merge_words([W|Words], [W|Rest]):-
merge_words(Words, Rest).
print_top_words([], _, _):-!.
print_top_words(_, 0, _):-!.
print_top_words([Word - Count|Rest], N, R):-
writef("%w\t%w\t%w\n", [R, Count, Word]),
N1 is N - 1,
R1 is R + 1,
print_top_words(Rest, N1, R1).
main:-
print_top_words("135-0.txt", 10).
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Zebra puzzle step by step in the Tcl programming language
You may also check:How to resolve the algorithm Synchronous concurrency step by step in the OCaml programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Factorial step by step in the Soda programming language