lede-spacy 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.egg-info/
5
+ .pytest_cache/
6
+ .coverage
7
+ build/
8
+ dist/
9
+ .venv/
10
+ venv/
11
+
12
+ # IDE
13
+ .vscode/
14
+ .idea/
15
+
16
+ # Per-developer agent / tooling config (Claude Code, etc.)
17
+ # These files often contain machine-local paths and per-machine permission
18
+ # allowlists that should not be shared.
19
+ .claude/
20
+ !.claude/agents/
21
+ !.claude/commands/
22
+
23
+ # Skill output (research/audit artifacts — point-in-time, not source).
24
+ # Per the skill-output-hygiene rule. Never commit these to a public repo.
25
+ skill-output/
26
+
27
+ # Secrets — defensive, even though none exist today
28
+ .env
29
+ .env.*
30
+ !.env.example
31
+ .openai
32
+ .openai.*
33
+ .anthropic
34
+ .anthropic.*
35
+ *.pem
36
+ *.key
37
+ *.p12
38
+ *.pfx
39
+ *.jks
40
+ id_rsa
41
+ id_ed25519
42
+ credentials*.json
43
+ secrets*.json
44
+
45
+ # Terraform / IaC state
46
+ *.tfstate
47
+ *.tfstate.*
48
+ *.tfvars
49
+ !*.tfvars.example
50
+
51
+ # OS cruft
52
+ .DS_Store
53
+ Thumbs.db
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,226 @@
1
+ Metadata-Version: 2.4
2
+ Name: lede-spacy
3
+ Version: 0.3.0
4
+ Summary: spaCy-powered enrichment backend for lede — PERSON/ORG/GPE entity extraction via en_core_web_sm.
5
+ Project-URL: Homepage, https://github.com/yonk-labs/lede
6
+ Project-URL: Repository, https://github.com/yonk-labs/lede
7
+ Project-URL: Issues, https://github.com/yonk-labs/lede/issues
8
+ Author: Yonk
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Keywords: entity-extraction,lede,ner,spacy
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Text Processing :: Linguistic
19
+ Requires-Python: >=3.10
20
+ Requires-Dist: lede>=0.3.0
21
+ Requires-Dist: spacy<3.9,>=3.8
22
+ Provides-Extra: dev
23
+ Requires-Dist: pytest>=7; extra == 'dev'
24
+ Description-Content-Type: text/markdown
25
+
26
+ # lede-spacy
27
+
28
+ spaCy-powered enrichment backend for [lede](https://github.com/yonk-labs/lede).
29
+ Adds proper named-entity recognition (PERSON / ORG / GPE) and richer
30
+ phrase / fact extraction by registering itself as the `"spacy"` backend
31
+ for `lede.extract.metadata`, `lede.extract.phrases`, and
32
+ `lede.extract.correlate_facts`.
33
+
34
+ | | regex backend (default in `lede`) | spaCy backend (this package) |
35
+ |---|---|---|
36
+ | Entities (PERSON / ORG / GPE) | always empty | populated from `en_core_web_sm` |
37
+ | Phrases | repeated multi-word n-grams | syntactically-grounded noun chunks (still count-filtered) |
38
+ | Correlate facts | regex pattern → entity↔number | dependency-parse → wider net of entity↔number relationships |
39
+ | Latency | sub-millisecond | ~5 ms after warmup, ~50 ms first call (model load) |
40
+ | Determinism | byte-identical Python ↔ Rust | spaCy is deterministic but Python-only and not byte-comparable to Rust |
41
+ | Install footprint | stdlib only | `spacy>=3.8` + `en_core_web_sm` (~50 MB model) |
42
+
43
+ ## When you actually want this
44
+
45
+ lede's regex backend covers the majority of structured-extract use cases for
46
+ free — dates, amounts, URLs, numeric facts with sentence context, and
47
+ repeated-phrase mining all work with zero dependencies. **You want
48
+ lede-spacy specifically when you need named entities** — people,
49
+ companies, places — pulled out of arbitrary text. That's where the regex
50
+ backend explicitly returns nothing.
51
+
52
+ You also get richer `correlate_facts` (the dep-parser walks the syntax
53
+ tree to find entity↔number relationships even for entities mentioned
54
+ once, where the regex backend requires repetition).
55
+
56
+ ## Side-by-side: the same input, both backends
57
+
58
+ ```python
59
+ from lede.extract import metadata
60
+ import lede_spacy # side effect: registers the 'spacy' backend
61
+ ```
62
+
63
+ The text:
64
+
65
+ > Acme Corp announced today a partnership with Yonk Labs to integrate
66
+ > deterministic summarization into their RAG pipeline. The deal,
67
+ > brokered by CEO Lin Wu and signed in San Francisco on 2024-11-15,
68
+ > covers $2.4M in annual licensing through 2027. Sarah Jones from
69
+ > Acme's engineering team and Marcus Chen from Yonk Labs will lead
70
+ > the joint integration. The first deployment is targeted for European
71
+ > customers, including teams in London, Berlin, and Paris.
72
+
73
+ ### `backend="regex"` (lede default — zero-dep)
74
+
75
+ ```python
76
+ m = metadata(text)
77
+ m.dates # ('2024-11-15', '2027')
78
+ m.amounts # ('$2.4M',)
79
+ m.urls # ()
80
+ m.entities # () ← regex backend returns nothing here
81
+ ```
82
+
83
+ The regex backend caught the structured stuff (ISO date, year, dollar
84
+ amount). It can't do entities — that's not a regex job.
85
+
86
+ ### `backend="spacy"` (this package)
87
+
88
+ ```python
89
+ m = metadata(text, backend="spacy")
90
+ m.dates # ('2024-11-15', '2027') — same regex stage runs
91
+ m.amounts # ('$2.4M',) — same regex stage runs
92
+ m.urls # () — same regex stage runs
93
+ m.entities # ('Acme Corp', 'Yonk Labs', 'RAG', 'Lin Wu',
94
+ # 'San Francisco', 'Sarah Jones', 'Acme',
95
+ # 'Marcus Chen', 'London', 'Berlin', 'Paris')
96
+ ```
97
+
98
+ 11 entities pulled out of the same input. PERSON ('Lin Wu', 'Sarah
99
+ Jones', 'Marcus Chen'), ORG ('Acme Corp', 'Yonk Labs', 'Acme', 'RAG'),
100
+ GPE ('San Francisco', 'London', 'Berlin', 'Paris'). The dates / amounts /
101
+ URLs columns are unchanged — spaCy backend runs the same regex stages
102
+ *plus* the spaCy NER stage on top.
103
+
104
+ ### Use case: `correlate_facts` finds different relationships
105
+
106
+ For some inputs the two backends produce *different but overlapping* fact
107
+ relationships. From the same paragraph above:
108
+
109
+ ```python
110
+ correlate_facts(text) # regex: 2 pairs anchored on Acme Corp
111
+ correlate_facts(text, backend="spacy") # spaCy: 4 pairs, also catches Yonk Labs and customer churn
112
+ ```
113
+
114
+ The dep-parser approach catches relationships the regex misses,
115
+ especially for entities that appear once. (And vice versa — sometimes the
116
+ regex backend catches a pattern the dep-parser doesn't. The two are
117
+ complementary, not strictly better/worse. Switch backend per call if you
118
+ need it.)
119
+
120
+ ## Use this when…
121
+
122
+ - Your callers want **PERSON / ORG / GPE entities** in the output. Default lede can't help.
123
+ - You want richer entity-number correlations on documents where each entity is mentioned once.
124
+ - You're already running spaCy in your pipeline and want to consolidate.
125
+ - Latency budgets are in the 5–50 ms range per chunk, not sub-millisecond.
126
+
127
+ ## Don't use this when…
128
+
129
+ - You need **byte-identical Python ↔ Rust output**. spaCy is Python-only and isn't on the parity contract.
130
+ - You're on a sub-millisecond hot path. spaCy is ~5 ms per call after warmup, ~50 ms first call.
131
+ - You don't actually need entities. The default lede regex backend already handles dates / amounts / URLs / numeric facts with sentence context.
132
+ - You're shipping lede inside a constrained environment (Lambda cold-start, embedded, no-egress) — the 50 MB `en_core_web_sm` model has real cost.
133
+ - You can't tolerate spaCy's transitive dependency graph (NumPy, Cython, blis, thinc, etc.) in your env.
134
+
135
+ ## Install
136
+
137
+ ```bash
138
+ pip install lede-spacy
139
+ python -m spacy download en_core_web_sm
140
+ ```
141
+
142
+ The first command pulls `lede` and `spacy>=3.8,<3.9`. The second pulls
143
+ the ~50 MB `en_core_web_sm` 3.8.0 model. PyPI does not allow direct-URL
144
+ dependencies, so the model is a separate install step (the same
145
+ convention spaCy itself uses).
146
+
147
+ If you want a single reproducible install, pin the model wheel from
148
+ `requirements.txt`:
149
+
150
+ ```
151
+ lede-spacy==0.3.0
152
+ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.8.0/en_core_web_sm-3.8.0-py3-none-any.whl
153
+ ```
154
+
155
+ From source (in the lede repo):
156
+
157
+ ```bash
158
+ pip install -e packages/lede-spacy
159
+ python -m spacy download en_core_web_sm
160
+ ```
161
+
162
+ ## Use
163
+
164
+ ```python
165
+ import lede_spacy # side-effect: registers the "spacy" backend
166
+ from lede.extract import metadata, phrases, correlate_facts
167
+
168
+ # Per-call backend override:
169
+ m = metadata(text, backend="spacy")
170
+
171
+ # Or set the global default once:
172
+ import lede
173
+ lede.set_default_backend("auto") # spaCy if registered, else regex
174
+ ```
175
+
176
+ Pre-load the model once at startup to avoid the ~50 ms first-call model
177
+ load:
178
+
179
+ ```python
180
+ from lede_spacy import warmup
181
+ warmup()
182
+ ```
183
+
184
+ ## Performance
185
+
186
+ | call | latency (typical) |
187
+ |---|---|
188
+ | First call (cold model) | 50–80 ms |
189
+ | Subsequent calls (warm) | ~5 ms |
190
+ | Default lede regex backend (for comparison) | <1 ms |
191
+
192
+ Run `from lede_spacy import warmup; warmup()` at app startup to pay the
193
+ 50 ms once instead of on the first user request.
194
+
195
+ ## What's registered
196
+
197
+ When you `import lede_spacy`, three backends register themselves into
198
+ `lede.extract._backends`:
199
+
200
+ | lede primitive | spaCy backend |
201
+ |---|---|
202
+ | `metadata(text, backend="spacy")` | runs regex `dates`/`amounts`/`urls` + spaCy NER for `entities` |
203
+ | `phrases(text, backend="spacy")` | `doc.noun_chunks` filtered to repeated multi-word chunks (matches the regex backend's count semantics) |
204
+ | `correlate_facts(text, backend="spacy")` | DepMatcher-based entity↔number pairing |
205
+
206
+ The `regex` backend stays the default — `import lede_spacy` is purely
207
+ additive. Existing callers using `backend="regex"` (or no `backend=`
208
+ kwarg) see no behavior change.
209
+
210
+ ## Determinism + parity
211
+
212
+ spaCy is deterministic per-version: `en_core_web_sm` 3.8.0 produces the
213
+ same entities for the same input on every call. **It is not on lede's
214
+ Python ↔ Rust parity contract**, by design. The Rust port has no spaCy
215
+ equivalent and `Metadata.entities` stays empty under any Rust call. See
216
+ [`docs/lede-spacy-integration.md`](https://github.com/yonk-labs/lede/blob/main/docs/lede-spacy-integration.md)
217
+ for the cross-language policy.
218
+
219
+ If you need NER from a Rust service today: call out to a Python
220
+ lede-spacy worker, or to a hosted NER endpoint. A future
221
+ `lede-rust-ner` companion crate is on the roadmap if there's demand —
222
+ file an issue.
223
+
224
+ ## License
225
+
226
+ Apache-2.0, same as lede.
@@ -0,0 +1,201 @@
1
+ # lede-spacy
2
+
3
+ spaCy-powered enrichment backend for [lede](https://github.com/yonk-labs/lede).
4
+ Adds proper named-entity recognition (PERSON / ORG / GPE) and richer
5
+ phrase / fact extraction by registering itself as the `"spacy"` backend
6
+ for `lede.extract.metadata`, `lede.extract.phrases`, and
7
+ `lede.extract.correlate_facts`.
8
+
9
+ | | regex backend (default in `lede`) | spaCy backend (this package) |
10
+ |---|---|---|
11
+ | Entities (PERSON / ORG / GPE) | always empty | populated from `en_core_web_sm` |
12
+ | Phrases | repeated multi-word n-grams | syntactically-grounded noun chunks (still count-filtered) |
13
+ | Correlate facts | regex pattern → entity↔number | dependency-parse → wider net of entity↔number relationships |
14
+ | Latency | sub-millisecond | ~5 ms after warmup, ~50 ms first call (model load) |
15
+ | Determinism | byte-identical Python ↔ Rust | spaCy is deterministic but Python-only and not byte-comparable to Rust |
16
+ | Install footprint | stdlib only | `spacy>=3.8` + `en_core_web_sm` (~50 MB model) |
17
+
18
+ ## When you actually want this
19
+
20
+ lede's regex backend covers the majority of structured-extract use cases for
21
+ free — dates, amounts, URLs, numeric facts with sentence context, and
22
+ repeated-phrase mining all work with zero dependencies. **You want
23
+ lede-spacy specifically when you need named entities** — people,
24
+ companies, places — pulled out of arbitrary text. That's where the regex
25
+ backend explicitly returns nothing.
26
+
27
+ You also get richer `correlate_facts` (the dep-parser walks the syntax
28
+ tree to find entity↔number relationships even for entities mentioned
29
+ once, where the regex backend requires repetition).
30
+
31
+ ## Side-by-side: the same input, both backends
32
+
33
+ ```python
34
+ from lede.extract import metadata
35
+ import lede_spacy # side effect: registers the 'spacy' backend
36
+ ```
37
+
38
+ The text:
39
+
40
+ > Acme Corp announced today a partnership with Yonk Labs to integrate
41
+ > deterministic summarization into their RAG pipeline. The deal,
42
+ > brokered by CEO Lin Wu and signed in San Francisco on 2024-11-15,
43
+ > covers $2.4M in annual licensing through 2027. Sarah Jones from
44
+ > Acme's engineering team and Marcus Chen from Yonk Labs will lead
45
+ > the joint integration. The first deployment is targeted for European
46
+ > customers, including teams in London, Berlin, and Paris.
47
+
48
+ ### `backend="regex"` (lede default — zero-dep)
49
+
50
+ ```python
51
+ m = metadata(text)
52
+ m.dates # ('2024-11-15', '2027')
53
+ m.amounts # ('$2.4M',)
54
+ m.urls # ()
55
+ m.entities # () ← regex backend returns nothing here
56
+ ```
57
+
58
+ The regex backend caught the structured stuff (ISO date, year, dollar
59
+ amount). It can't do entities — that's not a regex job.
60
+
61
+ ### `backend="spacy"` (this package)
62
+
63
+ ```python
64
+ m = metadata(text, backend="spacy")
65
+ m.dates # ('2024-11-15', '2027') — same regex stage runs
66
+ m.amounts # ('$2.4M',) — same regex stage runs
67
+ m.urls # () — same regex stage runs
68
+ m.entities # ('Acme Corp', 'Yonk Labs', 'RAG', 'Lin Wu',
69
+ # 'San Francisco', 'Sarah Jones', 'Acme',
70
+ # 'Marcus Chen', 'London', 'Berlin', 'Paris')
71
+ ```
72
+
73
+ 11 entities pulled out of the same input. PERSON ('Lin Wu', 'Sarah
74
+ Jones', 'Marcus Chen'), ORG ('Acme Corp', 'Yonk Labs', 'Acme', 'RAG'),
75
+ GPE ('San Francisco', 'London', 'Berlin', 'Paris'). The dates / amounts /
76
+ URLs columns are unchanged — spaCy backend runs the same regex stages
77
+ *plus* the spaCy NER stage on top.
78
+
79
+ ### Use case: `correlate_facts` finds different relationships
80
+
81
+ For some inputs the two backends produce *different but overlapping* fact
82
+ relationships. From the same paragraph above:
83
+
84
+ ```python
85
+ correlate_facts(text) # regex: 2 pairs anchored on Acme Corp
86
+ correlate_facts(text, backend="spacy") # spaCy: 4 pairs, also catches Yonk Labs and customer churn
87
+ ```
88
+
89
+ The dep-parser approach catches relationships the regex misses,
90
+ especially for entities that appear once. (And vice versa — sometimes the
91
+ regex backend catches a pattern the dep-parser doesn't. The two are
92
+ complementary, not strictly better/worse. Switch backend per call if you
93
+ need it.)
94
+
95
+ ## Use this when…
96
+
97
+ - Your callers want **PERSON / ORG / GPE entities** in the output. Default lede can't help.
98
+ - You want richer entity-number correlations on documents where each entity is mentioned once.
99
+ - You're already running spaCy in your pipeline and want to consolidate.
100
+ - Latency budgets are in the 5–50 ms range per chunk, not sub-millisecond.
101
+
102
+ ## Don't use this when…
103
+
104
+ - You need **byte-identical Python ↔ Rust output**. spaCy is Python-only and isn't on the parity contract.
105
+ - You're on a sub-millisecond hot path. spaCy is ~5 ms per call after warmup, ~50 ms first call.
106
+ - You don't actually need entities. The default lede regex backend already handles dates / amounts / URLs / numeric facts with sentence context.
107
+ - You're shipping lede inside a constrained environment (Lambda cold-start, embedded, no-egress) — the 50 MB `en_core_web_sm` model has real cost.
108
+ - You can't tolerate spaCy's transitive dependency graph (NumPy, Cython, blis, thinc, etc.) in your env.
109
+
110
+ ## Install
111
+
112
+ ```bash
113
+ pip install lede-spacy
114
+ python -m spacy download en_core_web_sm
115
+ ```
116
+
117
+ The first command pulls `lede` and `spacy>=3.8,<3.9`. The second pulls
118
+ the ~50 MB `en_core_web_sm` 3.8.0 model. PyPI does not allow direct-URL
119
+ dependencies, so the model is a separate install step (the same
120
+ convention spaCy itself uses).
121
+
122
+ If you want a single reproducible install, pin the model wheel from
123
+ `requirements.txt`:
124
+
125
+ ```
126
+ lede-spacy==0.3.0
127
+ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.8.0/en_core_web_sm-3.8.0-py3-none-any.whl
128
+ ```
129
+
130
+ From source (in the lede repo):
131
+
132
+ ```bash
133
+ pip install -e packages/lede-spacy
134
+ python -m spacy download en_core_web_sm
135
+ ```
136
+
137
+ ## Use
138
+
139
+ ```python
140
+ import lede_spacy # side-effect: registers the "spacy" backend
141
+ from lede.extract import metadata, phrases, correlate_facts
142
+
143
+ # Per-call backend override:
144
+ m = metadata(text, backend="spacy")
145
+
146
+ # Or set the global default once:
147
+ import lede
148
+ lede.set_default_backend("auto") # spaCy if registered, else regex
149
+ ```
150
+
151
+ Pre-load the model once at startup to avoid the ~50 ms first-call model
152
+ load:
153
+
154
+ ```python
155
+ from lede_spacy import warmup
156
+ warmup()
157
+ ```
158
+
159
+ ## Performance
160
+
161
+ | call | latency (typical) |
162
+ |---|---|
163
+ | First call (cold model) | 50–80 ms |
164
+ | Subsequent calls (warm) | ~5 ms |
165
+ | Default lede regex backend (for comparison) | <1 ms |
166
+
167
+ Run `from lede_spacy import warmup; warmup()` at app startup to pay the
168
+ 50 ms once instead of on the first user request.
169
+
170
+ ## What's registered
171
+
172
+ When you `import lede_spacy`, three backends register themselves into
173
+ `lede.extract._backends`:
174
+
175
+ | lede primitive | spaCy backend |
176
+ |---|---|
177
+ | `metadata(text, backend="spacy")` | runs regex `dates`/`amounts`/`urls` + spaCy NER for `entities` |
178
+ | `phrases(text, backend="spacy")` | `doc.noun_chunks` filtered to repeated multi-word chunks (matches the regex backend's count semantics) |
179
+ | `correlate_facts(text, backend="spacy")` | DepMatcher-based entity↔number pairing |
180
+
181
+ The `regex` backend stays the default — `import lede_spacy` is purely
182
+ additive. Existing callers using `backend="regex"` (or no `backend=`
183
+ kwarg) see no behavior change.
184
+
185
+ ## Determinism + parity
186
+
187
+ spaCy is deterministic per-version: `en_core_web_sm` 3.8.0 produces the
188
+ same entities for the same input on every call. **It is not on lede's
189
+ Python ↔ Rust parity contract**, by design. The Rust port has no spaCy
190
+ equivalent and `Metadata.entities` stays empty under any Rust call. See
191
+ [`docs/lede-spacy-integration.md`](https://github.com/yonk-labs/lede/blob/main/docs/lede-spacy-integration.md)
192
+ for the cross-language policy.
193
+
194
+ If you need NER from a Rust service today: call out to a Python
195
+ lede-spacy worker, or to a hosted NER endpoint. A future
196
+ `lede-rust-ner` companion crate is on the roadmap if there's demand —
197
+ file an issue.
198
+
199
+ ## License
200
+
201
+ Apache-2.0, same as lede.