-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentenceEncoding.py
80 lines (57 loc) · 2.2 KB
/
SentenceEncoding.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
68
69
70
71
72
73
74
75
76
77
78
79
80
# coding=utf-8
import json
from UDParser import UDParser
from UniversalPetrarch.EventCoder import EventCoder
from pprint import pprint
from nltk import sent_tokenize
import nltk
nltk.data.path.append("/Volumes/Untitled 2/Users/sayeed")
parser = UDParser(lang="en")
event_coder = EventCoder(config_file="PETR_config.ini")
def parse_ud(corenlp_data):
entries = corenlp_data["sentences"]
for entry in entries:
entry["parse_sentence"] = parser.parse(entry['sentence'])
print entry["parse_sentence"]
return corenlp_data
def create_dummy_article(sentences):
article = {}
article["type"] = "story"
article["doc_id"] = "dummy_001"
article["head_line"] = "dummy headline"
article["date_line"] = "Tue, 21 Jun 2017 03:52:15 GMT" #helpful to identify the role based on timeline
article["sentences"] = []
i = 1
for sentence in sentences:
sentence_node = {}
sentence_node["sentence_id"] = i
i += 1
sentence_node["sentence"] = sentence
article["sentences"].append(sentence_node)
return article
def encode_sentence(sentence):
article = create_dummy_article(sentence)
article = parse_ud(article)
pprint(article)
#print article
events = event_coder.encode_dict(article)
event_set = []
for i in range(1, len(sentence)+1):
if events is not None and 'events' in events['dummy_001']['sents'][i]:
event_set.append(events['dummy_001']['sents'][i])
i += 1
else:
event_set.append({})
return event_set
if __name__ == "__main__":
summary = "The UN Security Council on Tuesday unanimously approved a United States’ resolution on the recent deal between the U.S. and the Afghan Taliban, a rare endorsement of an agreement with a militant group."
print summary
summary_sentences = sent_tokenize(summary)
print(type(summary_sentences))
events = encode_sentence(summary_sentences)
for event_dict in events:
pprint(event_dict)
for key in event_dict["events"]:
print event_dict['triplets'][key]["source_text"]
print event_dict['triplets'][key]["verb_text"]
print event_dict['triplets'][key]["target_text"]