mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-06-26 22:00:13 +00:00

* Create extractor/entity.py * Aggregate NER words into entities * Support indexing * Add doc strings * Add utility for printing * Update signature of run() to match BaseComponent * Add test * Modify simplify_ner_for_qa to return the dictionary and add its test Co-authored-by: brandenchan <brandenchan@icloud.com>
57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
import pytest
|
|
|
|
from haystack.retriever.sparse import ElasticsearchRetriever
|
|
from haystack.reader import FARMReader
|
|
from haystack.pipeline import Pipeline
|
|
|
|
from haystack.extractor import EntityExtractor, simplify_ner_for_qa
|
|
|
|
|
|
@pytest.mark.parametrize("document_store_with_docs", ["elasticsearch"], indirect=True)
|
|
def test_extractor(document_store_with_docs):
|
|
|
|
es_retriever = ElasticsearchRetriever(document_store=document_store_with_docs)
|
|
ner = EntityExtractor()
|
|
reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2")
|
|
|
|
pipeline = Pipeline()
|
|
pipeline.add_node(component=es_retriever, name="ESRetriever", inputs=["Query"])
|
|
pipeline.add_node(component=ner, name="NER", inputs=["ESRetriever"])
|
|
pipeline.add_node(component=reader, name="Reader", inputs=["NER"])
|
|
|
|
prediction = pipeline.run(
|
|
query="Who lives in Berlin?",
|
|
params={
|
|
"ESRetriever": {"top_k": 1},
|
|
"Reader": {"top_k": 1},
|
|
}
|
|
)
|
|
entities = [entity["word"] for entity in prediction["answers"][0]["meta"]["entities"]]
|
|
assert "Carla" in entities
|
|
assert "Berlin" in entities
|
|
|
|
|
|
@pytest.mark.parametrize("document_store_with_docs", ["elasticsearch"], indirect=True)
|
|
def test_extractor_output_simplifier(document_store_with_docs):
|
|
|
|
es_retriever = ElasticsearchRetriever(document_store=document_store_with_docs)
|
|
ner = EntityExtractor()
|
|
reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2")
|
|
|
|
pipeline = Pipeline()
|
|
pipeline.add_node(component=es_retriever, name="ESRetriever", inputs=["Query"])
|
|
pipeline.add_node(component=ner, name="NER", inputs=["ESRetriever"])
|
|
pipeline.add_node(component=reader, name="Reader", inputs=["NER"])
|
|
|
|
prediction = pipeline.run(
|
|
query="Who lives in Berlin?",
|
|
params={
|
|
"ESRetriever": {"top_k": 1},
|
|
"Reader": {"top_k": 1},
|
|
}
|
|
)
|
|
simplified = simplify_ner_for_qa(prediction)
|
|
assert simplified[0] == {
|
|
"answer": "Carla",
|
|
"entities": ["Carla"]
|
|
} |