day1/exercise/kwfreq.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 27 Oct 2009 19:25:25 +0530
changeset 204 87f914f38ba1
parent 180 e442b9e23972
child 380 669b72283b55
permissions -rw-r--r--
Session 3 day 1, Statistical operations finalized.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
import keyword
180
e442b9e23972 Made English and low level id changes to session1.tex
rivermaker@RivermakerMBP.local
parents: 86
diff changeset
     2
f = open('amicable.py')
64
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
freq = {}
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
for line in f:
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
    words = line.split()
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
    for word in words:
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
        key = word.strip(',.!;?()[]: ')
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
        if keyword.iskeyword(key):
180
e442b9e23972 Made English and low level id changes to session1.tex
rivermaker@RivermakerMBP.local
parents: 86
diff changeset
    10
            if key in freq:
e442b9e23972 Made English and low level id changes to session1.tex
rivermaker@RivermakerMBP.local
parents: 86
diff changeset
    11
                freq[key] += 1
e442b9e23972 Made English and low level id changes to session1.tex
rivermaker@RivermakerMBP.local
parents: 86
diff changeset
    12
            else:
e442b9e23972 Made English and low level id changes to session1.tex
rivermaker@RivermakerMBP.local
parents: 86
diff changeset
    13
                freq[key] = 1
64
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
print freq
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16