You have two problems: Parsing the file and calculating the frequencies.
For parsing the file, I recommend using library(pio)
. In that manner you can use dcgs to process the file. So, I'd recommend you learn first about DCGs. They are Prolog's way to describe/generate and parse text. They are even more general than that. But to start with, just see it that way.
This you can then combine with calculating the frequencies. To make this also efficient for very large data see this question.