summaryrefslogtreecommitdiff
path: root/Code/count_word.py
blob: cd1d3c6c6df83f6ddc450135c96840810ff539a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Usage: python count_word.py /home/hui/network/v03/Data/information/rnaseq_info_database.txt
#
# Purpose: get all words in a file, order them by their frequencies.
#
# 20 Apr 2017, slcu, hui

FILE_NAME = '/home/hui/network/v03/Data/information/rnaseq_info_database.txt'

from collections import Counter
import operator
import string
import sys

FILE_NAME = sys.argv[1]

def remove_punctuation(s):
    result = ''
    for x in s.split():
        result += ' ' + x.translate(None, string.punctuation)
    return result.strip()

f = open(FILE_NAME)
lines = f.readlines()
f.close()
yourtext = ''
for line in lines[1:]: # don't include header line
    line = line.strip()
    lst = line.split('\t')
    for x in lst[4:]: # only consider fields from 5th column.
        yourtext += ' ' + remove_punctuation(x.lower())

d = Counter(yourtext.split())
sd = sorted(d.items(), key=operator.itemgetter(1), reverse=True)
for t in sd:
    k = t[0]
    print('%s\t\t%s' % (k, d[k]))