Count Frequency of Words

Question

I am trying to code to read info from a text file. I need to find out how many times each word separated by white space occurs. I then need to output in alphabetical order with the count of each word. I am looking to use a TreeMap, keySet() and an Iterator. My code is very incomplete and I am quite stuck.

 

import java.util.HashMap;

import java.util.Map

public class WordCount<E extends Comparable<E>> {

private static Map<String, Integer> map = new HashMap<String, Integer>();

static {

fillMap(map, "Alice.txt");

}

private static void fillMap(Map<String, Integer> map, String fileName) {

}

}

 

Answer

Split the text file into a sequence of words and group members by different words. It’s complicated to handle it in Java. You can get it done in SPL (Structured Process Language) and then embed the script into Java.

A

1

=file("d:\\data.txt").read()

2

=A1.words()

3

=A2.groups(~:word;count(~):count)

A1: Read the text file as a string.

A2: Split the string into a sequence of words.

A3: Group these words through accumulation and count the frequency of each unique word.

SPL groups function is written in Java HashMap and so has great performance. Abut calling an SPL script in Java, see How to Call an SPL Script in Java.