mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-06-26 22:00:13 +00:00
87 lines
3.3 KiB
Python
87 lines
3.3 KiB
Python
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
import pytest
|
|
|
|
from haystack import Document
|
|
from haystack.components.evaluators.document_mrr import DocumentMRREvaluator
|
|
|
|
|
|
def test_run_with_all_matching():
|
|
evaluator = DocumentMRREvaluator()
|
|
result = evaluator.run(
|
|
ground_truth_documents=[[Document(content="Berlin")], [Document(content="Paris")]],
|
|
retrieved_documents=[[Document(content="Berlin")], [Document(content="Paris")]],
|
|
)
|
|
|
|
assert result == {"individual_scores": [1.0, 1.0], "score": 1.0}
|
|
|
|
|
|
def test_run_with_no_matching():
|
|
evaluator = DocumentMRREvaluator()
|
|
result = evaluator.run(
|
|
ground_truth_documents=[[Document(content="Berlin")], [Document(content="Paris")]],
|
|
retrieved_documents=[[Document(content="Paris")], [Document(content="London")]],
|
|
)
|
|
|
|
assert result == {"individual_scores": [0.0, 0.0], "score": 0.0}
|
|
|
|
|
|
def test_run_with_partial_matching():
|
|
evaluator = DocumentMRREvaluator()
|
|
result = evaluator.run(
|
|
ground_truth_documents=[[Document(content="Berlin")], [Document(content="Paris")]],
|
|
retrieved_documents=[[Document(content="Berlin")], [Document(content="London")]],
|
|
)
|
|
|
|
assert result == {"individual_scores": [1.0, 0.0], "score": 0.5}
|
|
|
|
|
|
def test_run_with_complex_data():
|
|
evaluator = DocumentMRREvaluator()
|
|
result = evaluator.run(
|
|
ground_truth_documents=[
|
|
[Document(content="France")],
|
|
[Document(content="9th century"), Document(content="9th")],
|
|
[Document(content="classical music"), Document(content="classical")],
|
|
[Document(content="11th century"), Document(content="the 11th")],
|
|
[Document(content="Denmark, Iceland and Norway")],
|
|
[Document(content="10th century"), Document(content="10th")],
|
|
],
|
|
retrieved_documents=[
|
|
[Document(content="France")],
|
|
[Document(content="10th century"), Document(content="9th century"), Document(content="9th")],
|
|
[Document(content="rock music"), Document(content="dubstep"), Document(content="classical")],
|
|
[Document(content="11th"), Document(content="the 11th"), Document(content="11th century")],
|
|
[Document(content="Denmark"), Document(content="Norway"), Document(content="Iceland")],
|
|
[
|
|
Document(content="10th century"),
|
|
Document(content="the first half of the 10th century"),
|
|
Document(content="10th"),
|
|
Document(content="10th"),
|
|
],
|
|
],
|
|
)
|
|
|
|
assert result == {
|
|
"individual_scores": [1.0, 0.5, 0.3333333333333333, 0.5, 0.0, 1.0],
|
|
"score": pytest.approx(0.555555555555555),
|
|
}
|
|
|
|
|
|
def test_run_with_different_lengths():
|
|
with pytest.raises(ValueError):
|
|
evaluator = DocumentMRREvaluator()
|
|
evaluator.run(
|
|
ground_truth_documents=[[Document(content="Berlin")]],
|
|
retrieved_documents=[[Document(content="Berlin")], [Document(content="London")]],
|
|
)
|
|
|
|
with pytest.raises(ValueError):
|
|
evaluator = DocumentMRREvaluator()
|
|
evaluator.run(
|
|
ground_truth_documents=[[Document(content="Berlin")], [Document(content="Paris")]],
|
|
retrieved_documents=[[Document(content="Berlin")]],
|
|
)
|