Create abc
Browse files
abc
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer, util
|
| 2 |
+
print("import done")
|
| 3 |
+
# Input chunks
|
| 4 |
+
retrieved_chunks = [
|
| 5 |
+
"The Eiffel Tower is a landmark in Paris.",
|
| 6 |
+
"Paris is the capital of France.",
|
| 7 |
+
"The Louvre is also in Paris.",
|
| 8 |
+
"Eiffel Tower was built in 1889.",
|
| 9 |
+
"It is a famous tourist spot."
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
relevant_chunks = [
|
| 13 |
+
"The Eiffel Tower is a landmark in Paris.",
|
| 14 |
+
"Eiffel Tower was built in 1889."
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
# Load sentence transformer model
|
| 18 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 19 |
+
|
| 20 |
+
# Compute embeddings
|
| 21 |
+
retrieved_embeddings = model.encode(retrieved_chunks, convert_to_tensor=True)
|
| 22 |
+
relevant_embeddings = model.encode(relevant_chunks, convert_to_tensor=True)
|
| 23 |
+
|
| 24 |
+
# Calculate pairwise cosine similarities
|
| 25 |
+
cosine_sim_matrix = util.cos_sim(retrieved_embeddings, relevant_embeddings)
|
| 26 |
+
|
| 27 |
+
# Print similarity matrix
|
| 28 |
+
print("Cosine Similarity Matrix (rows: retrieved, columns: relevant):\n")
|
| 29 |
+
for i, retrieved in enumerate(retrieved_chunks):
|
| 30 |
+
for j, relevant in enumerate(relevant_chunks):
|
| 31 |
+
score = cosine_sim_matrix[i][j].item()
|
| 32 |
+
print(f"Similarity between:\n Retrieved: \"{retrieved}\"\n Relevant : \"{relevant}\"\n Score : {score:.4f}\n")
|