-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
281 lines (199 loc) · 8.47 KB
/
dataset.py
File metadata and controls
281 lines (199 loc) · 8.47 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import requests
import re
import json
import argparse
# Wikipedia & Wikidata API URLs
WIKI_API_URL = "https://pt.wikipedia.org/w/api.php"
WIKIDATA_API_URL = "https://www.wikidata.org/wiki/Special:EntityData/{}.json"
DATA_API_URL = "https://www.wikidata.org/w/api.php"
HEADERS = {
"User-Agent": "MyWikidataTool/1.0 (https://github.com/yourusername/yourrepo; you@example.com)"
}
def translate_relation(input_string):
mapping_dict = {
"P31": "instância de",
"P106": "ocupação",
"P19": "local de nascimento",
"P279": "subclasse de",
"P17": "país",
"P159": "sede",
"P921": "tema(s) principal(is)",
"P50": "autor",
"P105": "categoria taxonómica",
"P641": "desporto",
"P136": "género",
"P140": "religião",
"P22": "pai",
"P206": "banhado por",
"P86":"compositor",
"P272": "empresa de produção",
"P178": "desenvolvedor",
"P57": "realizador",
}
return mapping_dict.get(input_string, None)
# Function to get Wikipedia page title for an entity (from Wikidata ID)
def get_wikipedia_title(wikidata_id: str) -> str | None:
url = WIKIDATA_API_URL.format(wikidata_id)
try:
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
data = response.json()
except requests.RequestException:
return None
except ValueError:
return None
try:
return data["entities"][wikidata_id]["sitelinks"]["ptwiki"]["title"]
except KeyError:
return None # No Wikipedia page found
def get_wikidata_label(wikidata_id):
headers = {
'User-Agent': 'MyWikiBot/1.0 (https://github.com/yourusername/yourrepo; myemail@example.com)'
}
params = {
"action": "wbgetentities",
"ids": wikidata_id,
"format": "json",
"props": "labels",
"languages": "pt|en"
}
try:
response = requests.get(DATA_API_URL, params=params, headers=headers)
response.raise_for_status() # Raises exception for 4xx/5xx status codes
data = response.json()
# Check if the entity exists and has labels
if ("entities" in data and
wikidata_id in data["entities"] and
"labels" in data["entities"][wikidata_id]):
labels = data["entities"][wikidata_id]["labels"]
# Try Portuguese first, then English, then any available language
if "pt" in labels:
return labels["pt"].get("value")
elif "en" in labels:
return labels["en"].get("value")
elif labels: # If there are any labels, return the first one
first_label = next(iter(labels.values()))
return first_label.get("value")
return None # No labels found
except requests.exceptions.RequestException as e:
print(f"Request error for {wikidata_id}: {e}")
return None
except ValueError as e: # JSON decoding error
print(f"JSON decode error for {wikidata_id}: {e}")
return None
except Exception as e:
print(f"Unexpected error for {wikidata_id}: {e}")
return None
# Function to fetch Wikipedia page content and clean it
def get_wikipedia_text(title: str) -> str | None:
params = {
"action": "query",
"format": "json",
"prop": "extracts",
"exintro": True, # Only get the introduction
"titles": title
}
try:
response = requests.get(WIKI_API_URL, params=params, headers=HEADERS)
response.raise_for_status()
data = response.json()
except requests.RequestException:
return None
except ValueError:
return None
pages = data["query"]["pages"]
for page_id in pages:
if "extract" in pages[page_id]:
# Use BeautifulSoup to clean the HTML and extract text
#soup = BeautifulSoup(pages[page_id]["extract"], "lxml")
#return soup.get_text()
return pages[page_id]["extract"]
return None
def extract_sentences(text, entity1, entity2):
"""
Extracts the first sentence where both entities appear together.
Args:
text (str): The input text to search in.
entity1 (str): The first entity to search for.
entity2 (str): The second entity to search for.
Returns:
str: The first sentence where both entities appear, or an empty array if no match is found.
"""
# Split the text into sentences
sentences = re.split(r"(?<=[.!?])(?:\s|\n|<)", text)
# Use lower() for case-insensitive comparison
entity1_lower = entity1.lower()
entity2_lower = entity2.lower()
# Create word-boundary regex patterns for stricter matching
pattern1 = re.compile(rf"\b{re.escape(entity1_lower)}\b", re.IGNORECASE)
pattern2 = re.compile(rf"\b{re.escape(entity2_lower)}\b", re.IGNORECASE)
matched_sentences = []
# Iterate through sentences and find the first match
for sentence in sentences:
sentence_lower = sentence.lower()
match1 = pattern1.search(sentence_lower)
match2 = pattern2.search(sentence_lower)
if match1 and match2:
clean_text = re.sub(r"<[^>]+>", "", sentence) # Remove any HTML tags
matched_sentences.append(clean_text.strip())
return matched_sentences
# Return an empty array if no match is found
return matched_sentences
# Function to process a Wikidata triple and get Wikipedia sentences
def process_wikidata_triple(subject_id, relation, object_id, object_label, search_obj = 0):
title = get_wikipedia_title(subject_id)
if not title:
print(f"❌ Sem página da Wikipédia para {subject_id}")
return None
text = get_wikipedia_text(title)
if not text:
print(f"❌ Nenhum texto encontrado para {title}")
return None
matched_sentences = extract_sentences(text, title, object_label)
if len(matched_sentences) == 0:
if search_obj:
title2 = get_wikipedia_title(object_id)
if title2 == None:
print(f"❌ Sem página da Wikipédia para {object_id}")
return None
text2 = get_wikipedia_text(title2)
if text2 == None:
print(f"❌ Nenhum texto encontrado para {title2}")
return None
subject_label = get_wikidata_label(subject_id)
if subject_label == None:
return None
matched_sentences2 = extract_sentences(text2, title2, subject_label)
if len(matched_sentences2) == 0:
return None
else:
return {"sentence": matched_sentences2[0], "subject": subject_label, "relation": relation, "object": title2}
else:
return None
else:
return {"sentence": matched_sentences[0], "subject": title, "relation": relation, "object": object_label}
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="dataset creation")
parser.add_argument("input_file", type=str, help="Path to the input triples csv file.")
parser.add_argument("-o", "--output_file", type=str, default="dataset.jsonl", help="output dataset file path")
parser.add_argument("-f", "--filtered_file", type=str, default="filtered_triples.csv", help="path to the triples used on the dataset")
args = parser.parse_args()
print("Starting the Wikidata and Wikipedia connection")
valid_lines = []
with open(args.input_file, 'r') as infile, open(args.output_file, "w", encoding="utf-8") as f:
for line in infile:
subject_id, relation_id, object_id = line.strip().split('\t') # Split the line into components
object_label = get_wikidata_label(object_id)
translated = translate_relation(relation_id)
relation = translated if translated is not None else relation_id
if object_label == None:
continue
sentences = process_wikidata_triple(subject_id, relation, object_id, object_label,1)
if sentences == None:
continue
valid_lines.append(line)
json.dump(sentences, f, ensure_ascii=False)
f.write('\n')
print("Creating the new filtered triples file")
with open(args.filtered_file, 'w', encoding='utf-8') as outfile:
outfile.writelines(valid_lines)