CSC352 Lecture 7
Lecture notes for Lecture 7
(Tonje)
The Idea
Python
sw = {sw1, sw,2...} #Global cat = [c1, c2 ...] #local allWords = [w1,w2....] #local wordFreq = {[f1,w1],[f2,w2] ...} Sort, Reverse mostFreq = wordFreq[0:5] mostFreqWords = [w for f,w in mostFreq] dict = {} dict = {cat1:{w1:count1, w2:count2}...} #This time count is associated with cat1 for c in cat: if c in dict.keys(): wdict = dict[c] #get data associated with c for w in mostFreqWords: if w is in wdict.keys(): wdict[w] +=1 else wdict[w] = 1 else: dict[c] = {} for w in mostFreqWords: dict[c][w] = 1