// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class TextFun {
    private static final Pattern w = Pattern.compile("[^a-z]+");
    public static void main(String[] args) throws IOException {
        Map<String, Integer> count = new HashMap<String, Integer>();
        Files.lines(Paths.get(args[0])).forEach(line -> {
            Arrays.stream(w.split(line.toLowerCase())).forEach(word -> {
                if(word.length() > 0) count.put(word, count.getOrDefault(word, 0) + 1);
            });
        });
        count.entrySet().stream().sorted((e1,e2) -> e2.getValue() - e1.getValue()).limit(Integer.parseInt(args[1])).forEach(e -> {
            System.out.printf("%s : %d\n", e.getKey(), e.getValue());
        });
    }
}
