Page 1 of 1

Count most popular words

Posted: Tue May 30, 2017 7:01 pm
by Mr. Kibernetik
This program counts most popular words in a text file. In this example it uses Shakespeare's Hamlet.

Code: Select all

text = #.readtext("hamlet.txt")
words = #.split(text, " ", ".", ",", ";", "'", "!", "?", "-", "(", ")", "[", "]", #.crlf, #.quot)
> i, 1..#.size(words,1)
  >> words[i] = ""
  key = #.lower(words[i])
  dict[key] += 1
  total += 1
<
#.sortval(dict)
#.reverse(dict)
#.output(total, " words; ", #.size(dict), " unique words")
> i, 1..10
  key = dict[i]
  #.output(i, " : ", key, " = ", dict[key])
<
Output of the program:

Code: Select all

32885 words; 4634 unique words
1 : the = 1091
2 : and = 969
3 : to = 767
4 : of = 675
5 : i = 633
6 : a = 571
7 : you = 558
8 : my = 520
9 : in = 451
10 : it = 421