forked from NikhilPr95/book-characters-persona-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_feelings.py
52 lines (37 loc) · 1014 Bytes
/
get_feelings.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
import pickle
import json
import nltk
from nltk.corpus import wordnet as wn
#from wordnet_methods import *
feeling = wn.synset('feeling.n.01')
happy = wn.synset('happiness.n.01')
def get_hyponyms(li):
return [x.hyponyms() for x in li]
def flatten(li):
return [item for sublist in li for item in sublist]
def get_hyponyms_list(li):
if li:
return list(set(flatten(get_hyponyms(li))))
def get_the_hyponyms(li, hyps):
if li:
hyps |= set(li)
get_the_hyponyms(get_hyponyms_list(li), hyps)
return hyps
def get_all_hyponyms(li):
hyps = set()
return get_the_hyponyms(li, hyps)
def write_pickle(feels):
with open('feelings.pkl', 'wb') as fp:
pickle.dump(feels, fp, pickle.HIGHEST_PROTOCOL)
feels = set(sorted(get_all_hyponyms([feeling])))
feeleys = set()
for x in feels:
feeleys.add(str(x))
feels = feeleys
print feels
write_pickle(feels)
#def write_json():
# data = {'feels': feels}
# with open('feelings.json', 'w') as fp:
# json.dump(data, fp, sort_keys=True, indent=4)
#write_json()