Datasets:

Modalities:
Text
Formats:
json
ArXiv:
License:
File size: 2,099 Bytes
437e44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import sys
import pickle
import logging

log = logging.getLogger(__name__)
log.addHandler(logging.FileHandler('collect.log'))
log.setLevel(logging.DEBUG)

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 get_concepts_id(list_of_eurovoc_terms, eurovoc_dict):
    """Get concept IDs from the preloaded eurovoc dictionary"""
    for e in list_of_eurovoc_terms:
        try:
            yield eurovoc_dict[e.strip().lower()]
        except KeyError:
            log.warning(f"⚠️ Could not find {e} in Eurovoc")

def add_eurovoc_ids_to_jsonl(input_file, output_file, eurovoc_dict):
    """Read JSONL, add eurovoc_concepts_ids, write to new file"""
    with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
        for line_num, line in enumerate(infile, 1):
            line = line.strip()
            if not line or line == 'null':
                log.warning(f"⚠️ Skipping empty/null line {line_num}")
                continue
            try:
                doc = json.loads(line)
                if 'eurovoc_concepts' in doc:
                    # Get the IDs
                    concept_ids = list(get_concepts_id(doc['eurovoc_concepts'], eurovoc_dict))
                    
                    # Reorder: insert eurovoc_concepts_ids right after eurovoc_concepts
                    if 'eurovoc_concepts' in doc:
                        doc['eurovoc_concepts_ids'] = concept_ids
                outfile.write(json.dumps(doc) + '\n')
            except json.JSONDecodeError as e:
                log.error(f"⚠️ Invalid JSON at line {line_num}: {e}")
                continue


if __name__ == '__main__':
    
    # Load the Eurovoc dictionary from pickle
    with open("eurovoc_dict.pkl", "rb") as f:
        eurovoc_dict = pickle.load(f)

    input_file = sys.argv[1]
    output_file = sys.argv[2] if len(sys.argv) > 2 else 'output.jsonl'
    add_eurovoc_ids_to_jsonl(input_file, output_file, eurovoc_dict)
    print(f"Done! Saved to {output_file}", file=sys.stderr)