books-discovery / utils.py
teddyllm's picture
Upload 50 files
b917edb verified
raw
history blame
920 Bytes
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