-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.py
59 lines (37 loc) · 1.47 KB
/
process.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
import spacy
def asc_parser(docx):
"""Returns a list of dictionaries containing the verb, object, and context of each sentence given.
"""
nlp = spacy.load('en')
doc = nlp(docx)
return_list = []
for sent in doc.sents:
parsed = {'action': '', 'object': '', 'context': ''}
for word in sent:
if word.dep_ == 'dobj':
span = ''
for child in word.lefts:
if span:
span += ' '
span += child.text
span += (' ' + word.text)
for sub in word.children:
if sub.dep_ == 'conj':
span += ' '
for child in sub.subtree:
span += (child.text + ' ')
parsed['object'] = str(span)
parsed['action'] = str(word.head)
if word.dep_ == 'pobj':
for child in word.lefts:
parsed['context'] += str(child.text) + ' '
parsed['context'] += str(word.text) + ' '
for child in word.rights:
parsed['context'] += str(child.text) + ' '
print("Action: ", parsed['action'])
print("Object: ", parsed['object'])
print('Context: ', parsed['context'])
print('{}\n'.format(parsed))
return_list.append(parsed)
print("Return List ===> {}".format(return_list))
return return_list