Skip to content

Commit d10d4e8

Browse files
authored
add Spacy integration (#513)
* add Spacy integration * use logo in markdown file * fix svg * png
1 parent f13f7e9 commit d10d4e8

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

integrations/spacy.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
layout: integration
3+
name: spaCy
4+
description: Annotate named entities in your Haystack pipelines with spaCy models
5+
authors:
6+
- name: deepset
7+
socials:
8+
github: deepset-ai
9+
twitter: deepset_ai
10+
linkedin: https://www.linkedin.com/company/deepset-ai/
11+
pypi: https://pypi.org/project/spacy-haystack
12+
repo: https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/spacy
13+
type: Custom Component
14+
report_issue: https://github.com/deepset-ai/haystack-core-integrations/issues
15+
logo: /logos/spacy.png
16+
version: Haystack 2.0
17+
toc: true
18+
---
19+
20+
### **Table of Contents**
21+
22+
- [Overview](#overview)
23+
- [Installation](#installation)
24+
- [Usage](#usage)
25+
- [Components](#components)
26+
- [Standalone](#standalone)
27+
- [Pipeline](#pipeline)
28+
- [License](#license)
29+
30+
## Overview
31+
32+
[spaCy](https://spacy.io/) is a popular open-source library for Natural Language Processing in Python. The `spacy-haystack` integration provides the `SpacyNamedEntityExtractor`, which uses spaCy models to recognize named entities — such as people, organizations, and locations — and attach them to your documents.
33+
34+
## Installation
35+
36+
Install the `spacy-haystack` package:
37+
38+
```bash
39+
pip install spacy-haystack
40+
```
41+
42+
## Usage
43+
44+
### Components
45+
46+
This integration provides one component:
47+
48+
- [`SpacyNamedEntityExtractor`](https://docs.haystack.deepset.ai/docs/spacynamedentityextractor): annotates named entities in documents using a spaCy model.
49+
50+
When initializing it, you must set a `model`. Optionally, you can pass `pipeline_kwargs` (forwarded to the spaCy pipeline) and a `device` to run the model on.
51+
52+
### Standalone
53+
54+
The component works with any [spaCy model](https://spacy.io/models) that contains an NER component. `SpacyNamedEntityExtractor` accepts a list of `Documents`, annotates the text, and stores the result in each document's `meta` under the `named_entities` key. Use the `get_stored_annotations` helper to read the annotations back, and the span offsets to recover the entity text:
55+
56+
```python
57+
from haystack import Document
58+
from haystack_integrations.components.extractors.spacy import SpacyNamedEntityExtractor
59+
60+
extractor = SpacyNamedEntityExtractor(model="en_core_web_sm")
61+
62+
documents = [
63+
Document(content="My name is Clara and I live in Berkeley, California."),
64+
Document(content="New York State is home to the Empire State Building."),
65+
]
66+
67+
results = extractor.run(documents=documents)["documents"]
68+
69+
for doc in results:
70+
print(doc.content)
71+
for ann in SpacyNamedEntityExtractor.get_stored_annotations(doc):
72+
print(f" {ann.entity}: {doc.content[ann.start:ann.end]}")
73+
74+
# My name is Clara and I live in Berkeley, California.
75+
# PERSON: Clara
76+
# GPE: Berkeley
77+
# GPE: California
78+
# New York State is home to the Empire State Building.
79+
# GPE: New York State
80+
# ORG: the Empire State Building
81+
```
82+
83+
### Pipeline
84+
85+
The most common place for the extractor is right after the preprocessing step of an indexing pipeline, so that the entities are stored alongside the documents you write to a Document Store:
86+
87+
```python
88+
from haystack import Pipeline
89+
from haystack.components.converters import TextFileToDocument
90+
from haystack.components.preprocessors import DocumentSplitter
91+
from haystack.components.writers import DocumentWriter
92+
from haystack.document_stores.in_memory import InMemoryDocumentStore
93+
from haystack_integrations.components.extractors.spacy import SpacyNamedEntityExtractor
94+
95+
document_store = InMemoryDocumentStore()
96+
97+
pipeline = Pipeline()
98+
pipeline.add_component("converter", TextFileToDocument())
99+
pipeline.add_component("splitter", DocumentSplitter(split_by="word", split_length=200))
100+
pipeline.add_component("extractor", SpacyNamedEntityExtractor(model="en_core_web_sm"))
101+
pipeline.add_component("writer", DocumentWriter(document_store=document_store))
102+
103+
pipeline.connect("converter", "splitter")
104+
pipeline.connect("splitter", "extractor")
105+
pipeline.connect("extractor", "writer")
106+
107+
pipeline.run({"converter": {"sources": ["document.txt"]}})
108+
109+
# Each stored document now carries its named entities in meta["named_entities"].
110+
print(document_store.filter_documents()[0].meta["named_entities"])
111+
```
112+
113+
## License
114+
115+
`spacy-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.

logos/spacy.png

79.6 KB
Loading

0 commit comments

Comments
 (0)