' We do not count superfluous spaces as words OPTION COLLAPSE TRUE ' Optional: use TRE regex library to speed up the program PRAGMA RE tre INCLUDE LDFLAGS -ltre ' We're using associative arrays DECLARE frequency ASSOC NUMBER ' Load the text and remove all punctuation, digits, tabs and cr book$ = EXTRACT$(LOAD$("miserables.txt"), "[[:punct:]]|[[:digit:]]|[\t\r]", TRUE) ' Count each word in lowercase FOR word$ IN REPLACE$(book$, NL$, CHR$(32)) INCR frequency(LCASE$(word$)) NEXT ' Sort the associative array and then map the index to a string array LOOKUP frequency TO term$ SIZE x SORT DOWN ' Show results FOR i = 0 TO 9 PRINT term$[i], " : ", frequency(term$[i]) NEXT