2020-04-27 12:54:12 -07:00
|
|
|
from haystack import Finder
|
2020-07-10 10:54:56 +02:00
|
|
|
import pytest
|
2020-04-27 12:54:12 -07:00
|
|
|
|
2020-07-31 11:34:06 +02:00
|
|
|
|
2020-10-26 19:19:10 +01:00
|
|
|
@pytest.mark.slow
|
2020-10-30 18:06:02 +01:00
|
|
|
@pytest.mark.elasticsearch
|
2020-10-14 16:15:04 +02:00
|
|
|
@pytest.mark.parametrize("retriever_with_docs", ["tfidf"], indirect=True)
|
|
|
|
def test_finder_get_answers(reader, retriever_with_docs, document_store_with_docs):
|
|
|
|
finder = Finder(reader, retriever_with_docs)
|
2020-07-10 10:54:56 +02:00
|
|
|
prediction = finder.get_answers(question="Who lives in Berlin?", top_k_retriever=10,
|
|
|
|
top_k_reader=3)
|
|
|
|
assert prediction is not None
|
2020-11-30 17:50:04 +01:00
|
|
|
assert prediction["query"] == "Who lives in Berlin?"
|
2020-07-10 10:54:56 +02:00
|
|
|
assert prediction["answers"][0]["answer"] == "Carla"
|
|
|
|
assert prediction["answers"][0]["probability"] <= 1
|
|
|
|
assert prediction["answers"][0]["probability"] >= 0
|
|
|
|
assert prediction["answers"][0]["meta"]["meta_field"] == "test1"
|
|
|
|
assert prediction["answers"][0]["context"] == "My name is Carla and I live in Berlin"
|
2020-04-27 12:54:12 -07:00
|
|
|
|
2020-07-10 10:54:56 +02:00
|
|
|
assert len(prediction["answers"]) == 3
|
2020-04-27 12:54:12 -07:00
|
|
|
|
2020-07-10 10:54:56 +02:00
|
|
|
|
2020-10-30 18:06:02 +01:00
|
|
|
@pytest.mark.elasticsearch
|
2020-10-14 16:15:04 +02:00
|
|
|
@pytest.mark.parametrize("retriever_with_docs", ["tfidf"], indirect=True)
|
|
|
|
def test_finder_offsets(reader, retriever_with_docs, document_store_with_docs):
|
|
|
|
finder = Finder(reader, retriever_with_docs)
|
2020-07-10 10:54:56 +02:00
|
|
|
prediction = finder.get_answers(question="Who lives in Berlin?", top_k_retriever=10,
|
2020-04-27 12:54:12 -07:00
|
|
|
top_k_reader=5)
|
|
|
|
|
2020-07-10 10:54:56 +02:00
|
|
|
assert prediction["answers"][0]["offset_start"] == 11
|
2020-07-14 18:53:15 +02:00
|
|
|
assert prediction["answers"][0]["offset_end"] == 16
|
2020-07-10 10:54:56 +02:00
|
|
|
start = prediction["answers"][0]["offset_start"]
|
|
|
|
end = prediction["answers"][0]["offset_end"]
|
2020-07-14 18:53:15 +02:00
|
|
|
assert prediction["answers"][0]["context"][start:end] == prediction["answers"][0]["answer"]
|
2020-04-27 12:54:12 -07:00
|
|
|
|
|
|
|
|
2020-10-26 19:19:10 +01:00
|
|
|
@pytest.mark.slow
|
2020-10-30 18:06:02 +01:00
|
|
|
@pytest.mark.elasticsearch
|
2020-10-14 16:15:04 +02:00
|
|
|
@pytest.mark.parametrize("retriever_with_docs", ["tfidf"], indirect=True)
|
|
|
|
def test_finder_get_answers_single_result(reader, retriever_with_docs, document_store_with_docs):
|
|
|
|
finder = Finder(reader, retriever_with_docs)
|
2020-07-14 18:53:15 +02:00
|
|
|
query = "testing finder"
|
|
|
|
prediction = finder.get_answers(question=query, top_k_retriever=1,
|
2020-04-27 12:54:12 -07:00
|
|
|
top_k_reader=1)
|
|
|
|
assert prediction is not None
|
2020-07-10 10:54:56 +02:00
|
|
|
assert len(prediction["answers"]) == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|