📘 Finding the most frequent word using Perl 6

Find the most frequent word in the given text.

To find the most frequent word, you need first to find all the words in the text. 

This can be done via the global regex m:g/(\w+)/ or by using the comb method. The method returns a list of all the matched substrings. In the following example of solving the task, the regex matching is placed inside the for loop, which immediately updates the %count hash, which keeps the number of occurrences of each found word. To allow counting words case-insensitively, the $text value is first lower-cased with the help of the lc string method.

my $text = prompt('Text> ');
my %count;
%count{$_}++ for $text.lc.comb(/\w+/);
say (sort {%count{$^b} <=> %count{$^a}}, %count.keys)[0];

The sort function sorts the hash using the word frequency as the sorting parameter. Then, the first element, [0], is taken and printed.

Test the program on different texts to see how it works. What you may notice is that the program always prints only one word, even if there are other words with the same number of occurrences. To solve the problem, extract the number of repetitions and filter the %count hash to find all the words that match this condition.

my $max = %count{(sort {%count{$^b} <=> %count{$^a}},
                 count.keys)[0]};
.say for %count.keys.grep({%count{$_} == $max});

This program prints all the words having the maximum values in %count.

3 thoughts on “📘 Finding the most frequent word using Perl 6”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s