I'm looking to create a function that calculates the top occurring keywords in some text and returns the top 10 most common keywords.
So far I've been using the following code:
public static String KeywordDensityCalc(String articles )
{
String[] body = articles.split(" ");
HashMap<String, Integer> densityTracker = new HashMap<String, Integer>();
for(int densityCounter = 0; densityCounter < body.length; densityCounter++)
{
if (!densityTracker.containsKey(body[densityCounter]))
{
densityTracker.put(body[densityCounter], 1);
} else
{
densityTracker.put(body[densityCounter], densityTracker.get(body[densityCounter]) + 1);
}
}
This allows me to create a hashmap of string (as keys) and number of occurrences (as values). Now my question is how to sort them from highest to lowest and return the top 10 (or some other number)?