-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoherenceVec.py
67 lines (52 loc) · 2.06 KB
/
coherenceVec.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
66
67
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 3 09:40:05 2017
@author: HareeshRavi
"""
from pycorenlp import StanfordCoreNLP
import utils_vist
from coherence_vector import entity_score
import numpy as np
nlp = StanfordCoreNLP('http://localhost:9000')
def get_parse_tree(data):
# extract parse trees using standfordcoreNLP
tree = []
for storyidx in range(len(data)):
tree_temp = ''
curstory = data[storyidx]
for sentidx in range(len(curstory)):
output = nlp.annotate(curstory[sentidx],
properties={'annotators': 'parse',
'outputFormat': 'json'})
if sentidx < len(curstory)-1:
tree_temp += output['sentences'][0][
'parse'].replace('\n', '') + '\n'
else:
tree_temp += output['sentences'][0][
'parse'].replace('\n', ' ')
tree.append(tree_temp)
print('stories processed: {}/{}'.format(storyidx, len(data)), end='\r')
return tree
def main(config):
datadir = './data/'
process = ['train', 'val', 'test']
for proc in process:
# read the stories
stories = utils_vist.getSent(datadir + proc + '/' +
proc + '_text.csv')
parsetree = get_parse_tree(stories)
print('obtained parse trees..')
# save the trees in file
np.save(datadir + proc + '/' + proc + '_parsetree.npy', parsetree)
# get entitiy feature for all stories in the file
entity_feat = entity_score.entity_feature(parsetree)
print('obtained entity features..')
# convert dict to numpy array
cohvec = np.zeros((len(stories), 64), dtype=float)
for i in entity_feat:
cohvec[int(i)] = entity_feat[i]
# save entity feature as numpy file
np.save(datadir + proc + '/cohvec_' + proc + '.npy',
cohvec)
if __name__ == '__main__':
main()