File size: 920 Bytes
b917edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def combine_metadata_with_distance(metadatas, distances):
    # Flatten the nested lists if they are nested
    metadatas = metadatas[0] if isinstance(metadatas[0], list) else metadatas
    distances = distances[0] if isinstance(distances[0], list) else distances
    print(metadatas)
    if len(metadatas) != len(distances):
        raise ValueError("Number of metadata entries must match the number of distances")
    
    combined_result = []
    for metadata, distance in zip(metadatas, distances):
        new_metadata = {
            'title': metadata.get('Title', ''),
            'authors': metadata.get('Authors', ''),
            'description': metadata.get('Description', ''),
            'category': metadata.get('Category', ''),
            'distance': distance
        }
        combined_result.append(new_metadata)
        
    combined_result.sort(key=lambda x: x['distance'])

    return combined_result