File size: 12,837 Bytes
437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 437e44a d884266 | 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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | """
Cellar text and eurovoc extraction
python update.py 10000 dataset.jsonl
will extract for the last 10,000 days text in english and eurovoc labels in the JSON line file.
requirements:
beautifulsoup4==4.12.2
docx2txt==0.8
ipython==8.14.0
jinja2==3.1.2
joblib==1.3.1
pdfminer.six==20221105
pip-chill==1.0.3
pycryptodome==3.18.0
requests==2.31.0
tqdm==4.65.0
xmltodict==0.13.0
"""
import datetime
import json
from concurrent.futures import ProcessPoolExecutor
from bs4 import BeautifulSoup
import logging
import re
import sys
from tqdm import tqdm
from io import BytesIO
import jinja2
from joblib import Memory
location = './cache'
memory = Memory(location, verbose=0)
log = logging.getLogger(__name__)
log.addHandler(logging.FileHandler('collect.log'))
log.setLevel(logging.DEBUG)
import xmltodict
import docx2txt as docx2txt
import requests
from joblib import expires_after
from pdfminer.high_level import extract_text
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
def clean_text(func):
"""
Decorator used to clean the text
:param func:
:return:
"""
def inner(*args, **kwargs):
text = func(*args, **kwargs)
# Handle None or empty returns
if not text:
return ""
text = text.replace("\n", " ")
text = text.replace(" .", ".")
text = re.sub(' +', ' ', text)
text = re.sub(' *[.] *', '. ', text)
text = re.sub(r'\.\s*\.\s*\.+', '. ', text)
text = '. '.join([s.strip() for s in text.split(".") if len(s.strip())])
return text
return inner
@memory.cache(cache_validation_callback=expires_after(minutes=120))
def get_eurovoc_terms_and_id_old():
eurovoc_terms_and_id = {}
response = requests.get('http://publications.europa.eu/resource/dataset/eurovoc',
headers={'Accept': 'application/xml',
'Accept-Language': 'en',
'User-Agent': user_agent
}
)
data = xmltodict.parse(response.content)
for term in data['xs:schema']['xs:simpleType']['xs:restriction']['xs:enumeration']:
try:
name = term['xs:annotation']['xs:documentation'].split('/')[0].strip()
for r in term['xs:annotation']['xs:appinfo']['record']:
if r['@thesaurus_id'] != '':
eurovoc_terms_and_id[name.lower()] = r['@thesaurus_id']
except KeyError as e:
log.warning("⚠️ Could not parse", term)
return eurovoc_terms_and_id
@memory.cache(cache_validation_callback=expires_after(minutes=120))
def get_eurovoc_terms_and_id():
eurovoc_terms_and_id = {}
response = requests.get('http://publications.europa.eu/resource/dataset/eurovoc',
headers={'Accept': 'application/xml',
'Accept-Language': 'en',
'User-Agent': user_agent
}
)
data = xmltodict.parse(response.content)
for term in data['xs:schema']['xs:simpleType']['xs:restriction']['xs:enumeration']:
try:
name = term['xs:annotation']['xs:documentation'].split('/')[0].strip()
eurovoc_id = term['@value'].split(':')[1]
eurovoc_terms_and_id[name.lower()] = eurovoc_id
except (KeyError, IndexError) as e:
log.warning("⚠️ Could not parse", term)
return eurovoc_terms_and_id
def get_sparql_query(d):
start = d.strftime('%Y-%m-%d')
end = d + datetime.timedelta(days=2)
end = end.strftime('%Y-%m-%d')
environment = jinja2.Environment()
template = environment.from_string(open("query.j2", 'r').read())
return template.render(start=start, end=end)
def get_json_response(d):
url = "https://publications.europa.eu/webapi/rdf/sparql"
headers = {'User-Agent': user_agent}
params = {"default-graph-uri": "",
"query": get_sparql_query(d),
"format": "application/sparql-results+json",
"timeout": "0",
"debug": "on",
"run": "Run Query"}
response = requests.get(url, headers=headers, params=params)
assert response.status_code == 200
return response.json()
def get_concepts_id(list_of_eurovoc_terms):
terms = get_eurovoc_terms_and_id()
# keep track of returned IDs
seen = set()
for e in list_of_eurovoc_terms:
try:
concept_id = terms[e.strip().lower()]
if concept_id not in seen:
seen.add(concept_id)
yield concept_id
except KeyError:
log.warning(f"⚠️ Could not find {e} in Eurovoc")
def get_docs(d):
results = get_json_response(d)
for r in results['results']['bindings']:
try:
# Get terms safely
subjects_value = r.get('subjects', {}).get('value', '')
if not subjects_value:
log.warning(f"⚠️ No subjects found for record")
continue
terms = subjects_value.replace(u'\xa0', u' ').split(',')
# Remove empty strings
terms = [t.strip() for t in terms if t.strip()]
if not terms:
log.warning(f"⚠️ No valid terms after splitting")
continue
r['eurovoc_concepts'] = terms
# Generate IDs - guarantee it's a list
eurovoc_concepts_ids = list(get_concepts_id(terms))
r['eurovoc_concepts_ids'] = eurovoc_concepts_ids if eurovoc_concepts_ids else []
# Skip records with no valid IDs
if not r['eurovoc_concepts_ids']:
log.warning(f"⚠️ Skipping record with no valid eurovoc_concepts_ids: {r.get('title', {}).get('value', 'unknown')}")
continue
r['url'] = r['cellarURIs']['value']
r['title'] = r['title']['value']
r['date'] = r['date']['value']
r['lang'] = r['langIdentifier']['value'].lower()
r['formats'] = [t.strip() for t in r['mtypes']['value'].split(',')]
for c in ['cellarURIs', 'mtypes', 'langIdentifier', 'subjects', 'authors', 'workTypes', 'workIds']:
if c in r:
del r[c]
yield r
except Exception as e:
log.error(f"⚠️ Error processing record in get_docs: {e}")
continue
def get_docs_text(d):
docs = list(get_docs(d))
print(f"Processing documents ... {len(docs)}")
with ProcessPoolExecutor(max_workers=16) as executor:
for v in tqdm(executor.map(get_body, docs), total=len(docs), colour='green'):
# Filter out None results
if v is not None:
yield v
def get_body(r):
try:
text = None
if 'pdf' in r['formats']:
text = get_pdf_body(r)
elif 'docx' in r['formats']:
text = get_docx_body(r)
elif 'doc' in r['formats']:
text = get_doc_body(r)
elif 'xhtml' in r['formats']:
text = get_xhtml_body(r)
elif 'html' in r['formats']:
text = get_html_body(r)
else:
log.warning(f"⚠️ Could not find a parser for {r['formats']}")
return None
r['text'] = text
# Validate: must have text and eurovoc_concepts
if not text or not text.strip():
log.warning(f"⚠️ No text extracted for {r.get('url', 'unknown')}")
return None
if not r.get('eurovoc_concepts') or len(r.get('eurovoc_concepts', [])) == 0:
log.warning(f"⚠️ No eurovoc_concepts for {r.get('url', 'unknown')}")
return None
return r
except Exception as e:
log.error(f"⚠️ Exception in get_body: {str(e)} for record: {r.get('url', 'unknown')}")
return None
@clean_text
@memory.cache()
def get_pdf_body(r):
url = r['url']
language = r['lang']
accept = 'application/pdf'
response = requests.get(url, headers={'Accept': accept, 'Accept-Language': language, 'User-Agent': user_agent})
if response.status_code == 300:
return " ".join(_multiple_choice(get_pdf_body, response, accept, language))
elif response.status_code == 200:
mem = BytesIO(response.content)
return extract_text(mem)
else:
log.warning(f"⚠️ PDF fetch failed with status {response.status_code} for {url}")
return ""
@clean_text
@memory.cache()
def get_xhtml_body(r):
url = r['url']
language = r['lang']
accept = 'application/xhtml+xml'
response = requests.get(url, headers={'Accept': accept, 'Accept-Language': language, 'User-Agent': user_agent})
if response.status_code == 300:
return " ".join(_multiple_choice(get_xhtml_body, response, accept, language))
elif response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
return soup.get_text()
else:
log.warning(f"⚠️ XHTML fetch failed with status {response.status_code} for {url}")
return ""
@clean_text
@memory.cache()
def get_html_body(r):
"""Handle plain HTML format"""
url = r['url']
language = r['lang']
accept = 'text/html'
response = requests.get(url, headers={'Accept': accept, 'Accept-Language': language, 'User-Agent': user_agent})
if response.status_code == 300:
return " ".join(_multiple_choice(get_html_body, response, accept, language))
elif response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
return soup.get_text()
else:
log.warning(f"⚠️ HTML fetch failed with status {response.status_code} for {url}")
return ""
def get_docx_body(r):
accept = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml'
url = r['url']
lang = r['lang']
try:
return _get_doc_body(url, accept, lang)
except AssertionError as e:
log.warning(f"⚠️ Could not download {url} {e}")
return ""
def get_doc_body(r):
accept = 'application/msword'
url = r['url']
lang = r['lang']
try:
return _get_doc_body(url, accept, lang)
except AssertionError as e:
log.warning(f"⚠️ Could not download {url} {e}")
return ""
def _multiple_choice(func, response, accept, language):
soup = BeautifulSoup(response.text, 'html.parser')
for link in soup.find_all('a'):
if 'href' in link.attrs:
url = link.attrs['href']
yield func(url, accept, language)
@clean_text
@memory.cache()
def _get_doc_body(url, accept, language='en'):
response = requests.get(url, headers={'Accept': accept, 'Accept-Language': language, 'User-Agent': user_agent})
if response.status_code == 300:
return " ".join(_multiple_choice(_get_doc_body, response, accept, language))
elif response.status_code == 200:
mem = BytesIO(response.content)
log.info(f"📄 MS Word doc download and parsed {url}")
return docx2txt.process(mem)
else:
log.warning(f"📄 MS Word doc download failed {url} {response.status_code}")
return ""
if __name__ == '__main__':
output = sys.argv[1]
max = int(sys.argv[2])
ofiles = {}
valid_count = 0
skipped_count = 0
for i in range(max):
d = datetime.date.today() - datetime.timedelta(days=i)
print(d)
ym = d.strftime('%Y-%m')
if ym not in ofiles:
ofiles[ym] = open(output + ym + '.jsonl', 'w')
try:
for doc in get_docs_text(d):
if doc is not None:
# Extra validation before writing
if doc.get('text') and doc.get('eurovoc_concepts_ids'):
ofiles[ym].write(json.dumps(doc) + '\n')
ofiles[ym].flush()
valid_count += 1
else:
skipped_count += 1
log.warning(f"⚠️ Skipping invalid doc in main loop")
else:
skipped_count += 1
except Exception as e:
log.error('Day ' + str(d) + ' ' + str(e))
print('Day ' + str(d) + ' ' + str(e))
for f in ofiles.values():
f.close()
print(f"\n✓ Download complete!")
print(f" Valid records: {valid_count}")
print(f" Skipped records: {skipped_count}") |