-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCorpusComparison.py
203 lines (142 loc) · 5.4 KB
/
CorpusComparison.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import json
import os
import sys
import time
from googletrans import Translator
from SentenceEncoding import encode_sentence
import requests
from nltk import sent_tokenize
from mtranslate import translate
def download_and_translate():
translator = Translator(service_urls=[
'translate.google.com',
])
spanish_data_file = open("Spanish_articles.tsv", "r")
relevant_articles = []
skip_count = 0
if os.path.exists("counter.txt"):
skip_count = int(open("counter.txt", "r").read())
print (skip_count)
relevant_articles = json.load(open("translated_articles.json", "r"))
i = 0
print ("Skip Count is "+str(skip_count))
lines = spanish_data_file.readlines()[skip_count:]
for line in lines:
link = line.split("\t")[0]
type = line.split("\t")[1] #can be Irrelevant, Political or Non-Political
sub_type = None
try:
sub_type = line.split("\t")[2] #represents Root Code in CAMEO ontology
except IndexError:
sub_type = "Irrelevant"
if not link.startswith("http"):
continue
print (link)
response = requests.get(link)
article = json.loads(response.content)["data"]
#print article
article["sub_type"] = sub_type
#not processing any irrelevant article
if type in ["Political", "Non-Political"]:
print("Relevent article, processing for next steps")
# sentences = sent_tokenize(article["content"]) #spliting up into sentences
# article["sentences"] = sentences
# trans_sents = []
# trans_sents.append(translator.translate(sentences, dest="en"))
#adding english translation to the corresponding object
try:
print ("========")
print(article["content"])
article["translation"] = translate(article["content"].encode("utf-8"), to_language="en")
except (TypeError, ValueError) as e:
print("Inside Exception handling")
json.dump(relevant_articles, open("translated_articles.json", "w+"))
counter_file = open("counter.txt", "w+")
counter_file.write(str(skip_count))
counter_file.close()
sys.exit(1)
# article["translated_sents"] = trans_sents
# article.pop("content")
relevant_articles.append(article)
print (len(relevant_articles))
time.sleep(10)
else:
print "Skipping an irrelevant article"
skip_count += 1
#saving the articles in file for future processing
json.dump(relevant_articles, open("translated_articles.json", "w+"))
download_and_translate()
articles = json.load(open("translated_articles.json", "r"))
from UDParser import UDParser
from UniversalPetrarch.EventCoder import EventCoder
from pprint import pprint
from nltk import sent_tokenize
parser_en = UDParser()
parser_es = UDParser(lang="es")
event_coder_es = EventCoder(config_file="PETR_config_es.ini")
event_coder_en = EventCoder()
coders = {"en": event_coder_en, "es": event_coder_es}
parsers = {"en": parser_en, "es": parser_es}
def parse_ud(corenlp_data, language = "en"):
entries = corenlp_data["sentences"]
for entry in entries:
entry["parse_sentence"] = parsers[language].parse(entry['sentence'])
return corenlp_data
def create_article(art, lang="english", content_key="translation"):
article = {}
article["type"] = "story"
article["doc_id"] = art["_id"]
article["head_line"] = art["title"]
article["date_line"] = "Tue, 21 Jun 2017 03:52:15 GMT" #helpful to identify the role based on timeline
article["sentences"] = []
i = 1
sentences = sent_tokenize(art[content_key], lang)
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(article, language="en"):
events = coders[language].encode_dict(article)
event_set = []
for i in range(1, len(events['dummy_001']['sents'])+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
def process(article):
art_es = create_article(article, content_key="content")
art_en = create_article(article, content_key="translation")
art_es = parse_ud(art_es, language="es")
art_en = parse_ud(art_en, language="en")
try:
events_es = encode_sentence(art_es,language="es")
events_en = encode_sentence(art_en,language="en")
return events_en, events_es
except (KeyError, TypeError):
return
from multiprocessing import Pool
workers = Pool(4)
# dup_articles = []
#
# for i in range(10):
# for article in articles:
# dup_articles.append(article)
#
# print ("Number of articles: ", str(len(dup_articles)))
#
# for article in dup_articles:
# if article["sub_type"] is not "Irrelevant":
start = time.time()
events_tuples = workers.map(process, articles)
print(len(events_tuples))
workers.close()
end = time.time()
for event_en, event_es in events_tuples:
print event_en, event_es
json.dump(events_tuples, open("events_es_en.json", "w+"))
print (end-start)