Find 5 Most Frequent Phrases from a given file

This article will help you to understand “Find 5 most frequent phrases in java.”

Given a file has 8 phrases per line separated by a pipe (|). Assume that the phrases do not contain pipe.

Example line look like: Foobar Candy | Olympics 2012 | Software | one | CodeNuclear | SOme Phrase | Cricket| CodeNuclear

Please see attached file This file is used in our example.

To achieve above requirement, we will prepare a map with word as key and count of words as it’s value. We will iterate through each line and check each word with key of map. If it already exists then we will increase the count, otherwise we will store with count as 1.

Then we will sort map by value with help of Collections.sort. And then we will display most 5 frequent phrases.

Find 5 most frequent phrases in java Program


Most 5 Frequent Phrases are displayed below. CodeNuclear–> 7 Foobar Candy–> 5 Olympics 2012–> 4 one–> 3 Software–> 2

Note :- Here consider changing file path with your system’s file path.

2 Thoughts on “Find 5 Most Frequent Phrases from a given file”

Leave a Reply

Your email address will not be published. Required fields are marked *