-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_article_text_downloader.py
70 lines (48 loc) · 1.94 KB
/
source_article_text_downloader.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
import re
from typing import List, Dict
import requests
import json
def write_jsonl(file_name: str, json_lines_content: List[Dict]):
with open(file_name, 'w+') as file:
for json_data in json_lines_content:
json.dump(json_data, file, ensure_ascii=False)
file.write("\n")
def read_jsonl(file_name: str) -> List[Dict]:
json_lines_content = []
with open(file_name, 'r') as file:
for line in file:
json_lines_content.append(json.loads(line))
return json_lines_content
def add_space_between_letters_with_punctuation(text: str):
cleaned_text = re.sub(r'(?<=[^\d\W])[.:](?=[^\d\W])', r'\g<0> ', text)
return cleaned_text
def get_article_text(url):
search_url = url + "/search"
response = requests.get(search_url)
# Ensure we got a successful response
if response.status_code != 200:
print(f"Failed to get data from {search_url}, status code: {response.status_code}")
return None
data = response.json()
paragraphs = data.get('paragraphs', [])
text = add_space_between_letters_with_punctuation('\n'.join(paragraphs))
return text
def set_source_article_texts(dataset_json_lines: List[Dict]):
for cluster in dataset_json_lines:
articles = cluster['articles']
for article in articles:
url = article['article_link']
text = get_article_text(url)
if text is not None:
# Replace the existing text with the downloaded one
article['text'] = text
else:
print(f"The url: {url} does not exist")
def main():
dataset_dir = "Multi-GeNews.jsonl"
dataset_json_lines = read_jsonl(file_name=dataset_dir)
set_source_article_texts(dataset_json_lines=dataset_json_lines)
out_dataset_dir = "Multi-GeNews-With-Text.jsonl"
write_jsonl(file_name=out_dataset_dir, json_lines_content=dataset_json_lines)
if __name__ == '__main__':
main()