-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
65 lines (53 loc) · 1.32 KB
/
util.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import nltk
import random
import ast
import operator
# Returns tag sets that meet a certain threshold for appearing in the corpus
def get_highest_tag_freq( txtfile, threshold ):
f = open( txtfile, 'r' )
lines = f.readlines()
tagset_list = []
tag_freq = {}
for line in lines:
if line in tag_freq:
tag_freq[line] += 1
else:
tag_freq[line] = 1
tag_freq_min_2 = {}
for k,v in tag_freq.items():
if v > threshold:
tag_freq_min_2[k] = v
f.close()
return tag_freq_min_2
# Returns lst of POS tags in the input line
def get_tagset( line ):
if line[0].isalpha() and len( line.split() ) > 6:
text = nltk.word_tokenize( line )
tags = nltk.pos_tag( text )
tag_set = []
for tag in tags:
tag_set.append( tag[1] )
return tag_set
else:
return None
# Creates list of all POS tags contained in provided text file
# Takes some time
def generate_tagset( txtfile ):
f = open( txtfile, 'r' )
lines = f.readlines()
tagset_list = []
for line in lines:
tagset = get_tagset( line )
if tagset is not None:
print(tagset)
f.close()
def print_dict(d, iters):
count = 0
for k,v in d.items():
if count < iters:
print(str(k) + str(v))
count += 1
if __name__ == '__main__':
# generate_tagset('rap_corpus_small.txt')
sorted_tag_freq = get_highest_tag_freq( 'raptags.txt', 5)
print_dict(sorted_tag_freq, 20)