Count most popular words

Post Reply
User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Count most popular words

Post 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

Post Reply