ruby-spacy 0.6.0 → 0.8.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +75 -92
- data/Gemfile +7 -0
- data/README.md +68 -15
- data/Rakefile +6 -0
- data/docs/syntax_trees.md +59 -0
- data/examples/llm/anthropic_chat.rb +1 -1
- data/examples/openai_integration/openai_completion.rb +2 -3
- data/examples/openai_integration/openai_query_1.rb +2 -3
- data/examples/openai_integration/openai_query_2.rb +2 -3
- data/examples/openai_integration/openai_query_3.rb +2 -3
- data/examples/openai_integration/openai_query_4.rb +2 -3
- data/examples/rsyntaxtree/outputs/tree_ar_projection.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_de_projection.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_en_chunks.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_en_morphology.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_en_path.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_en_projection.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_ja_chunks.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_ja_projection.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_ru_morphology.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_ru_projection.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_zh_projection.png +0 -0
- data/examples/rsyntaxtree/syntax_tree_ar.rb +36 -0
- data/examples/rsyntaxtree/syntax_tree_de.rb +19 -0
- data/examples/rsyntaxtree/syntax_tree_en.rb +30 -0
- data/examples/rsyntaxtree/syntax_tree_ja.rb +21 -0
- data/examples/rsyntaxtree/syntax_tree_ru.rb +23 -0
- data/examples/rsyntaxtree/syntax_tree_zh.rb +21 -0
- data/lib/ruby-spacy/anthropic_client.rb +8 -7
- data/lib/ruby-spacy/anthropic_helper.rb +15 -7
- data/lib/ruby-spacy/llm_client_base.rb +69 -12
- data/lib/ruby-spacy/openai_client.rb +27 -16
- data/lib/ruby-spacy/openai_helper.rb +12 -7
- data/lib/ruby-spacy/syntax_tree.rb +349 -0
- data/lib/ruby-spacy/version.rb +1 -1
- data/lib/ruby-spacy.rb +138 -34
- data/ruby-spacy.gemspec +8 -1
- metadata +24 -4
- data/.github/FUNDING.yml +0 -6
- data/.github/workflows/ci.yml +0 -74
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8092ed24c921dbc2275034ff35939a762fe14c761322450472f337361a039b87
|
|
4
|
+
data.tar.gz: d6009fc3acbf3bff355fed3e0f556509f081978791e31398f3ee97690224fccc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 27b956204bf65b822b2145628213989202bc4cc6d5af7e5030b41b357402069fef29981ffef3f09585d200197019dedfa905c0caa9af8170b88a1081320ecc65
|
|
7
|
+
data.tar.gz: ebc7aefbba1a330b991de002716eb886c1314b76be7a215d19f0a24453359a9aec0edb3a2f3d9157865e2ca4c6c6b9ae57d7767a388499529feb4fc5297f88c3
|
data/CHANGELOG.md
CHANGED
|
@@ -1,147 +1,130 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 0.8.0 - 2026-11-28
|
|
4
|
+
### Added
|
|
5
|
+
- `path:` in `syntax_tree` — draws the route between two tokens in a heavier line
|
|
6
|
+
- `reasoning_effort:` for OpenAI and `effort:` for Anthropic, per block or per call
|
|
7
|
+
|
|
8
|
+
### Changed
|
|
9
|
+
- Default models are now `gpt-6-luna` and `claude-sonnet-5-5`; the previous default `gpt-5-mini` is retired on 2026-12-11
|
|
10
|
+
- Default output limit raised to 4000 tokens, since reasoning counts toward it
|
|
11
|
+
- Syntax trees line up the words on the bottom row; `leaf_align: "off", leafstyle: "nothing"` restores the previous drawing
|
|
12
|
+
- `syntax_tree` requires rsyntaxtree 2.10.0 or later
|
|
13
|
+
- Models that reject `reasoning_effort` are retried without it, as with `temperature`
|
|
14
|
+
|
|
15
|
+
### Fixed
|
|
16
|
+
- API keys no longer appear in `inspect` output or error messages
|
|
17
|
+
- `Retry-After` waits are capped at 60 seconds and HTTP dates are honored
|
|
18
|
+
- `openai_query` answers every tool call, and `get_tokens` analyzes the text the model asks about
|
|
19
|
+
- Syntax tree options given as Symbols, `corner_label`, and `direction: "ltr"` for right-to-left languages work as documented
|
|
20
|
+
|
|
21
|
+
## 0.7.0 - 2026-09-03
|
|
22
|
+
### Added
|
|
23
|
+
- `Doc#syntax_tree` and `Span#syntax_tree` — render a parse as rsyntaxtree notation, SVG, PNG, PDF, TikZ, or JSON
|
|
24
|
+
- `Language.new(py_nlp:)` — wrap a Python pipeline built elsewhere, such as one from spacy-stanza
|
|
25
|
+
- `docs/syntax_trees.md` — a gallery of syntax trees in six languages
|
|
26
|
+
|
|
3
27
|
## 0.6.0 - 2026-08-31
|
|
4
28
|
### Added
|
|
5
|
-
- GitHub Actions CI — Ruby 3.2 to 4.0 (plus ruby-head) and Python 3.11 to 3.14,
|
|
6
|
-
with a weekly run to catch breakage from new spaCy or PyCall releases
|
|
7
29
|
- `:label` in `Matcher#match` results — the label string of the matched pattern
|
|
8
30
|
|
|
9
31
|
### Changed
|
|
10
|
-
- PyCall 1.5.3 or later is
|
|
11
|
-
process when Ruby's GC releases a Python object on a non-main thread, which
|
|
12
|
-
affects any threaded application (Rails, Puma, Sidekiq)
|
|
32
|
+
- PyCall 1.5.3 or later is required; earlier versions can freeze threaded applications
|
|
13
33
|
- Minimum Ruby version raised to 3.2
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
- Language
|
|
17
|
-
|
|
18
|
-
- `Language.new(timeout:)` now actually fires. The wait happens on a Python
|
|
19
|
-
thread rather than through Ruby's `Timeout`, whose watcher thread cannot run
|
|
20
|
-
while PyCall holds the GVL. `timeout: nil` waits indefinitely, and a timeout
|
|
21
|
-
is never retried
|
|
22
|
-
- README documents the thread-safety constraint: all spaCy calls must be made
|
|
23
|
-
from the thread that initialized PyCall
|
|
34
|
+
- The `numpy` gem is no longer a dependency
|
|
35
|
+
- `terminal-table` requirement relaxed to `>= 3.0, < 5`
|
|
36
|
+
- A `Language` no longer keeps its pipeline alive until the process exits
|
|
37
|
+
- README documents that all spaCy calls must come from the thread that initialized PyCall
|
|
24
38
|
|
|
25
39
|
### Deprecated
|
|
26
|
-
- `Language#spacy_nlp_id` — use `#py_nlp` instead
|
|
27
|
-
but creates a Python global variable that is never released
|
|
40
|
+
- `Language#spacy_nlp_id` — use `#py_nlp` instead
|
|
28
41
|
|
|
29
42
|
### Fixed
|
|
30
|
-
- `
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
which accepts large ids again
|
|
34
|
-
- Integer attributes reached through `method_missing`, such as `Token#orth`,
|
|
35
|
-
were silently corrupted for the same reason: 7 of 12 common words returned a
|
|
36
|
-
negative `orth`. They are now fetched safely, which also restores
|
|
37
|
-
`Token#rank` for out-of-vocabulary tokens
|
|
43
|
+
- `Language.new(timeout:)` now times out; `timeout: nil` waits indefinitely
|
|
44
|
+
- `Matcher#match` returns the correct `:match_id` for every label
|
|
45
|
+
- Large integer attributes such as `Token#orth` and `Token#rank` are no longer corrupted
|
|
38
46
|
|
|
39
47
|
## 0.5.0 - 2026-07-21
|
|
40
48
|
### Added
|
|
41
|
-
- `Language#with_llm(provider:)` —
|
|
42
|
-
|
|
43
|
-
- `
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
- `base_url:` option for OpenAI client/helper — works with any
|
|
47
|
-
OpenAI-compatible server (Ollama, LM Studio, llama.cpp server, vLLM,
|
|
48
|
-
OpenRouter, etc.)
|
|
49
|
-
- `schema:` option for `chat` — Structured Outputs on both providers;
|
|
50
|
-
returns a validated, parsed Ruby Hash
|
|
51
|
-
- New examples under `examples/llm/` (Anthropic, local Ollama, and a
|
|
52
|
-
spaCy-vs-LLM NER comparison with structured outputs)
|
|
49
|
+
- `Language#with_llm(provider:)` — block API for `:openai`, `:anthropic`, and `:ollama`
|
|
50
|
+
- `AnthropicHelper` and `AnthropicClient` — Anthropic Messages API support (requires `ANTHROPIC_API_KEY`)
|
|
51
|
+
- `base_url:` for the OpenAI client and helper — works with any OpenAI-compatible server
|
|
52
|
+
- `schema:` in `chat` — Structured Outputs on both providers, returned as a parsed Hash
|
|
53
|
+
- Examples in `examples/llm/`
|
|
53
54
|
|
|
54
55
|
### Changed
|
|
55
|
-
- `temperature` is
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
models that supported it (also from `Doc#openai_query` /
|
|
60
|
-
`Doc#openai_completion`); now the API default applies unless you pass
|
|
61
|
-
`temperature:` yourself
|
|
62
|
-
- Shared HTTP layer (`LLMClientBase`) extracted from `OpenAIClient`;
|
|
63
|
-
no behavior change for existing OpenAI usage
|
|
64
|
-
- Truncated responses (`finish_reason: length` / `stop_reason: max_tokens`)
|
|
65
|
-
now emit a warning; with `schema:`, unparseable (e.g. truncated) JSON
|
|
66
|
-
returns nil with a warning instead of raising `JSON::ParserError`
|
|
67
|
-
- LLM error messages are printed to stderr (`Kernel#warn`) instead of
|
|
68
|
-
stdout, keeping stdout clean for program output
|
|
69
|
-
- Default model names are now public constants:
|
|
70
|
-
`OpenAIClient::DEFAULT_MODEL`, `OpenAIClient::DEFAULT_EMBEDDINGS_MODEL`,
|
|
71
|
-
`AnthropicClient::DEFAULT_MODEL`
|
|
56
|
+
- `temperature` is sent only when given (previously 0.7 by default); a model that rejects it is retried without it
|
|
57
|
+
- Truncated responses emit a warning; with `schema:`, unparseable JSON returns nil instead of raising
|
|
58
|
+
- LLM error messages go to stderr instead of stdout
|
|
59
|
+
- Default model names are public constants: `OpenAIClient::DEFAULT_MODEL`, `OpenAIClient::DEFAULT_EMBEDDINGS_MODEL`, `AnthropicClient::DEFAULT_MODEL`
|
|
72
60
|
|
|
73
61
|
## 0.4.1 - 2026-07-19
|
|
74
62
|
### Fixed
|
|
75
|
-
-
|
|
76
|
-
are world-readable (0644 / 0755). Version 0.4.0 shipped 11 owner-only
|
|
77
|
-
(0600) files, which made the gem unusable after `sudo gem install`
|
|
63
|
+
- Packaged files are world-readable, so the gem works after `sudo gem install`
|
|
78
64
|
|
|
79
65
|
## 0.4.0 - 2026-02-23
|
|
80
66
|
### Added
|
|
81
|
-
- `Language#with_openai` block API with `OpenAIHelper`
|
|
82
|
-
- `Doc#linguistic_summary`
|
|
83
|
-
- `Token#idx`, `Span#text
|
|
84
|
-
|
|
85
|
-
### Fixed
|
|
86
|
-
- `Doc#ents` returns proper `Span` objects instead of raw Python objects
|
|
67
|
+
- `Language#with_openai` — block API with `OpenAIHelper`
|
|
68
|
+
- `Doc#linguistic_summary` — spaCy analysis as JSON
|
|
69
|
+
- `Token#idx`, `Span#text`, `Span#to_s`, and `Language#memory_zone` (spaCy 3.8+)
|
|
87
70
|
|
|
88
71
|
### Changed
|
|
89
|
-
-
|
|
90
|
-
- OpenAI client
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
-
|
|
94
|
-
|
|
95
|
-
|
|
72
|
+
- `Language.new` validates the model name
|
|
73
|
+
- OpenAI client retries on 429 and supports `dimensions:` and `response_format:`
|
|
74
|
+
- `openai_query` limits the depth of tool calls
|
|
75
|
+
- `respond_to?` reports the Python methods each wrapper forwards
|
|
76
|
+
- `base64` gem added as a dependency (required for Ruby 3.4+)
|
|
77
|
+
|
|
78
|
+
### Fixed
|
|
79
|
+
- `Doc#ents` returns `Span` objects instead of raw Python objects
|
|
96
80
|
|
|
97
81
|
## 0.3.0 - 2025-01-06
|
|
98
82
|
### Added
|
|
99
83
|
- Ruby 4.0 support
|
|
100
|
-
- `Doc#to_bytes`
|
|
101
|
-
- `
|
|
102
|
-
- `PhraseMatcher` class for efficient phrase matching
|
|
103
|
-
- `Language#phrase_matcher` helper method
|
|
84
|
+
- `Doc#to_bytes` and `Doc.from_bytes` — serialize and restore documents
|
|
85
|
+
- `PhraseMatcher` and `Language#phrase_matcher`
|
|
104
86
|
|
|
105
87
|
### Changed
|
|
106
|
-
-
|
|
107
|
-
-
|
|
108
|
-
-
|
|
109
|
-
-
|
|
110
|
-
- Added `fiddle` gem dependency (required for Ruby 4.0)
|
|
88
|
+
- The `ruby-openai` gem is replaced by a built-in OpenAI client
|
|
89
|
+
- Default OpenAI model is now `gpt-5-mini`, and the embeddings model `text-embedding-3-small`
|
|
90
|
+
- `max_tokens:` is renamed `max_completion_tokens:`; the old name still works
|
|
91
|
+
- `fiddle` gem added as a dependency (required for Ruby 4.0)
|
|
111
92
|
|
|
112
93
|
## 0.2.4 - 2024-12-11
|
|
113
94
|
### Changed
|
|
114
|
-
-
|
|
95
|
+
- `Language.new` retries when loading the model fails
|
|
115
96
|
|
|
116
97
|
## 0.2.3 - 2024-08-27
|
|
117
|
-
|
|
118
|
-
-
|
|
98
|
+
### Added
|
|
99
|
+
- `timeout:` in `Language.new`
|
|
100
|
+
|
|
101
|
+
### Changed
|
|
102
|
+
- Default OpenAI model is now `gpt-4o-mini`
|
|
119
103
|
|
|
120
104
|
## 0.2.0 - 2022-10-02
|
|
121
105
|
### Added
|
|
122
|
-
- spaCy 3.7.0
|
|
123
|
-
- `Doc#openai_query`
|
|
124
|
-
- `Doc#openai_completion`
|
|
125
|
-
- `Doc#openai_embeddings`
|
|
106
|
+
- spaCy 3.7.0 support
|
|
107
|
+
- `Doc#openai_query`, `Doc#openai_completion`, and `Doc#openai_embeddings`
|
|
126
108
|
|
|
127
109
|
## 0.1.4.1 - 2021-07-06
|
|
128
|
-
|
|
129
|
-
- `
|
|
110
|
+
### Changed
|
|
111
|
+
- `Language#most_similar` returns hash-based objects that accept method calls
|
|
130
112
|
|
|
131
113
|
## 0.1.4 - 2021-06-26
|
|
132
114
|
### Added
|
|
133
115
|
- `Spacy::Lexeme` class
|
|
134
116
|
|
|
135
|
-
- `Spacy::Token#morpheme` method
|
|
136
117
|
## 0.1.3 - 2021-06-26
|
|
137
|
-
|
|
118
|
+
### Changed
|
|
119
|
+
- No user-facing changes
|
|
138
120
|
|
|
139
121
|
## 0.1.2 - 2021-06-26
|
|
140
122
|
### Added
|
|
141
|
-
- `
|
|
123
|
+
- `Token#morpheme`
|
|
142
124
|
|
|
143
125
|
## 0.1.1 - 2021-06-26
|
|
144
|
-
|
|
126
|
+
### Changed
|
|
127
|
+
- Gem description corrected
|
|
145
128
|
|
|
146
129
|
## 0.1.0 - 2021-06-26
|
|
147
130
|
- Initial release
|
data/Gemfile
CHANGED
|
@@ -9,3 +9,10 @@ group :development do
|
|
|
9
9
|
gem "github-markup"
|
|
10
10
|
gem "redcarpet"
|
|
11
11
|
end
|
|
12
|
+
|
|
13
|
+
# Used only for rendering in Doc#syntax_tree / Span#syntax_tree tests.
|
|
14
|
+
# Kept in its own group so CI jobs without the pango/rsvg system libraries
|
|
15
|
+
# can exclude it with BUNDLE_WITHOUT=rendering.
|
|
16
|
+
group :rendering do
|
|
17
|
+
gem "rsyntaxtree", ">= 2.10.0", require: false
|
|
18
|
+
end
|
data/README.md
CHANGED
|
@@ -10,20 +10,22 @@
|
|
|
10
10
|
| ✅ | Part-of-speech tagging and dependency parsing |
|
|
11
11
|
| ✅ | Named entity recognition |
|
|
12
12
|
| ✅ | Syntactic dependency visualization |
|
|
13
|
+
| ✅ | Syntax tree visualization (via rsyntaxtree) |
|
|
13
14
|
| ✅ | Access to pre-trained word vectors |
|
|
14
15
|
| ✅ | LLM integration: OpenAI, Anthropic (Claude), and local models |
|
|
15
16
|
|
|
16
|
-
Current Version: `0.
|
|
17
|
+
Current Version: `0.8.0`
|
|
17
18
|
|
|
18
19
|
- Ruby 3.2 to 4.0 supported (PyCall 1.5.3 or later required)
|
|
19
20
|
- spaCy 3.8 supported
|
|
21
|
+
- Syntax trees drawn with rsyntaxtree, including right-to-left languages
|
|
20
22
|
- Multi-provider LLM API: OpenAI, Anthropic (Claude), and local models via Ollama or any OpenAI-compatible server
|
|
21
23
|
- Structured outputs (JSON Schema) support
|
|
22
24
|
- Block-based LLM API with linguistic analysis
|
|
23
25
|
|
|
24
26
|
## Installation of Prerequisites
|
|
25
27
|
|
|
26
|
-
**IMPORTANT**: Make sure that the `enable-shared` option is enabled in your Python installation. You can use [pyenv](https://github.com/pyenv/pyenv) to install any version of Python you like. spaCy 3.8 supports Python 3.10 and later (wheels are provided for Python 3.10–3.
|
|
28
|
+
**IMPORTANT**: Make sure that the `enable-shared` option is enabled in your Python installation. You can use [pyenv](https://github.com/pyenv/pyenv) to install any version of Python you like. spaCy 3.8 supports Python 3.10 and later (wheels are provided for Python 3.10–3.15), so we recommend using one of those versions. Install Python 3.13, for instance, using pyenv with `enable-shared` as follows:
|
|
27
29
|
|
|
28
30
|
```shell
|
|
29
31
|
$ env CONFIGURE_OPTS="--enable-shared" pyenv install 3.13
|
|
@@ -77,6 +79,18 @@ Or install it yourself as:
|
|
|
77
79
|
|
|
78
80
|
See [Examples](#examples) below.
|
|
79
81
|
|
|
82
|
+
### Using an External Pipeline
|
|
83
|
+
|
|
84
|
+
`Spacy::Language.new` normally loads an installed model by name. To use a language spaCy has no model for, or a pipeline you built yourself, pass an existing Python `Language` object instead:
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
py_nlp = PyCall.import_module("spacy_stanza").load_pipeline("ar")
|
|
88
|
+
nlp = Spacy::Language.new(py_nlp: py_nlp)
|
|
89
|
+
nlp.read("...").tokens # works like any other nlp
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Third-party packages such as [spacy-stanza](https://github.com/explosion/spacy-stanza) or [spacy-udpipe](https://github.com/TakeLab/spacy-udpipe) are not ruby-spacy dependencies; install them yourself. `model` and `py_nlp:` are mutually exclusive.
|
|
93
|
+
|
|
80
94
|
## Examples
|
|
81
95
|
|
|
82
96
|
Many of the following examples are Python-to-Ruby translations of code snippets in [spaCy 101](https://spacy.io/usage/spacy-101). For more examples, look inside the `examples` directory.
|
|
@@ -286,6 +300,43 @@ Output:
|
|
|
286
300
|
|
|
287
301
|

|
|
288
302
|
|
|
303
|
+
### Syntax Trees
|
|
304
|
+
|
|
305
|
+
→ [rsyntaxtree](https://github.com/yohasebe/rsyntaxtree)
|
|
306
|
+
|
|
307
|
+
`Doc#syntax_tree` (and `Span#syntax_tree`) converts the parse into rsyntaxtree bracket notation and can render it as an image. rsyntaxtree (>= 2.10.0) is an optional dependency: `gem install rsyntaxtree`.
|
|
308
|
+
|
|
309
|
+
```ruby
|
|
310
|
+
require "ruby-spacy"
|
|
311
|
+
|
|
312
|
+
nlp = Spacy::Language.new("en_core_web_sm")
|
|
313
|
+
doc = nlp.read("The quick brown fox jumped over the lazy dog near the river.")
|
|
314
|
+
|
|
315
|
+
doc.syntax_tree # => "[S [%NP [DET The] [ADJ quick] ...] ...]"
|
|
316
|
+
File.binwrite("tree.png", doc.syntax_tree(format: :png))
|
|
317
|
+
doc.syntax_tree(style: :chunks) # shallow tree with noun chunks
|
|
318
|
+
doc.syntax_tree(morphology: true) # attach morphology tables to the leaves
|
|
319
|
+
doc.syntax_tree(format: :svg, path: [doc[3], doc[11]]) # highlight the route from "fox" to "river"
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_en_projection.png" alt="English projection tree" width="560">
|
|
323
|
+
|
|
324
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_en_morphology.png" alt="English projection tree with morphology tables" width="760">
|
|
325
|
+
|
|
326
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_ja_projection.png" alt="Japanese projection tree with named entities" width="500">
|
|
327
|
+
|
|
328
|
+
Two styles are available: `:projection` (default; a phrase-structure-like tree projected from the head words) and `:chunks` (a shallow tree with noun chunks). In both, noun chunks are shaded grey, and a chunk that is also a named entity is shaded orange and labeled with the entity type (`entities: false` to disable). The blue and green are rsyntaxtree's default palette, not marking of any kind; only the shading carries meaning. Punctuation is omitted (`punctuation: true` to keep it). The words line up on the bottom row, each joined to its tag by a dashed line (`leaf_align: "off", leafstyle: "nothing"` to set each word directly under its tag). `path:` takes two tokens and draws the route between them through the tree in a heavier line; in a projection tree, that route is the dependency path between the two words. The `format:` option accepts `:bracket` (default), `:svg`, `:png`, `:pdf`, `:tikz`, and `:json`; any other keywords are passed through to rsyntaxtree (e.g. `fontsize: 12`).
|
|
329
|
+
|
|
330
|
+
The notation is rsyntaxtree-flavored: it can contain AVMs (`#(...#)`), region backgrounds (`%`), and in-word space joins (`<>`). A doc must hold a single sentence; for a multi-sentence doc, use `doc.sents.map { |s| s.syntax_tree }`.
|
|
331
|
+
|
|
332
|
+
**Annotation schemes differ between models.** spaCy's English models make the preposition the head of its phrase, so projection trees contain `PP` nodes. UD-style models (Japanese, Russian, Chinese, and most others) attach prepositions and particles to the noun instead, so a case-marked phrase is labeled `NP` and no `PP` appears. The labels are ruby-spacy's own, derived from the head's POS tag — spaCy supplies only tags and dependencies.
|
|
333
|
+
|
|
334
|
+
**Entity highlighting requires noun chunks.** An entity gets its colored background only where it coincides with a noun chunk. In languages whose models have no noun chunk iterator (Russian, Chinese, Korean, Polish, ...) `entities: true` highlights nothing and `style: :chunks` raises `ArgumentError`.
|
|
335
|
+
|
|
336
|
+
**Right-to-left languages.** spaCy ships no pipelines for Arabic, Hebrew, or other RTL languages, so one has to come from outside (see [Using an External Pipeline](#using-an-external-pipeline)). Their trees are drawn mirrored automatically; pass `mirror: "off"` to disable.
|
|
337
|
+
|
|
338
|
+
See the [syntax tree gallery](docs/syntax_trees.md) for rendered trees in six languages, and `examples/rsyntaxtree/` for the scripts that generate them.
|
|
339
|
+
|
|
289
340
|
### Named Entity Recognition
|
|
290
341
|
|
|
291
342
|
→ [spaCy: Named entities](https://spacy.io/usage/spacy-101#annotations-ner)
|
|
@@ -640,8 +691,8 @@ nlp = Spacy::Language.new("en_core_web_sm")
|
|
|
640
691
|
doc = nlp.read("The Beatles released 12 studio albums")
|
|
641
692
|
|
|
642
693
|
# default parameter values
|
|
643
|
-
# max_completion_tokens:
|
|
644
|
-
# model: "gpt-
|
|
694
|
+
# max_completion_tokens: 4000
|
|
695
|
+
# model: "gpt-6-luna"
|
|
645
696
|
res1 = doc.openai_query(
|
|
646
697
|
access_token: api_key,
|
|
647
698
|
prompt: "Translate the text to Japanese."
|
|
@@ -665,8 +716,8 @@ nlp = Spacy::Language.new("en_core_web_sm")
|
|
|
665
716
|
doc = nlp.read("The Beatles were an English rock band formed in Liverpool in 1960.")
|
|
666
717
|
|
|
667
718
|
# default parameter values
|
|
668
|
-
# max_completion_tokens:
|
|
669
|
-
# model: "gpt-
|
|
719
|
+
# max_completion_tokens: 4000
|
|
720
|
+
# model: "gpt-6-luna"
|
|
670
721
|
res = doc.openai_query(
|
|
671
722
|
access_token: api_key,
|
|
672
723
|
prompt: "Extract the topic of the document and list 10 entities (names, concepts, locations, etc.) that are relevant to the topic."
|
|
@@ -702,8 +753,8 @@ nlp = Spacy::Language.new("en_core_web_sm")
|
|
|
702
753
|
doc = nlp.read("The Beatles released 12 studio albums")
|
|
703
754
|
|
|
704
755
|
# default parameter values
|
|
705
|
-
# max_completion_tokens:
|
|
706
|
-
# model: "gpt-
|
|
756
|
+
# max_completion_tokens: 4000
|
|
757
|
+
# model: "gpt-6-luna"
|
|
707
758
|
res = doc.openai_query(
|
|
708
759
|
access_token: api_key,
|
|
709
760
|
prompt: "List token data of each of the words used in the sentence. Add 'meaning' property and value (brief semantic definition) to each token data. Output as a JSON object."
|
|
@@ -791,8 +842,8 @@ nlp = Spacy::Language.new("en_core_web_sm")
|
|
|
791
842
|
doc = nlp.read("The Beatles released 12 studio albums")
|
|
792
843
|
|
|
793
844
|
# default parameter values
|
|
794
|
-
# max_completion_tokens:
|
|
795
|
-
# model: "gpt-
|
|
845
|
+
# max_completion_tokens: 4000
|
|
846
|
+
# model: "gpt-6-luna"
|
|
796
847
|
res = doc.openai_query(
|
|
797
848
|
access_token: api_key,
|
|
798
849
|
prompt: "Generate a tree diagram from the text using given token data. Use the following bracketing style: [S [NP [Det the] [N cat]] [VP [V sat] [PP [P on] [NP the mat]]]"
|
|
@@ -833,8 +884,8 @@ nlp = Spacy::Language.new("en_core_web_sm")
|
|
|
833
884
|
doc = nlp.read("Vladimir Nabokov was a")
|
|
834
885
|
|
|
835
886
|
# default parameter values
|
|
836
|
-
# max_completion_tokens:
|
|
837
|
-
# model: "gpt-
|
|
887
|
+
# max_completion_tokens: 4000
|
|
888
|
+
# model: "gpt-6-luna"
|
|
838
889
|
res = doc.openai_completion(access_token: api_key)
|
|
839
890
|
puts res
|
|
840
891
|
```
|
|
@@ -883,7 +934,7 @@ require "ruby-spacy"
|
|
|
883
934
|
nlp = Spacy::Language.new("en_core_web_sm")
|
|
884
935
|
doc = nlp.read("Apple Inc. was founded by Steve Jobs in California.")
|
|
885
936
|
|
|
886
|
-
nlp.with_openai(model: "gpt-
|
|
937
|
+
nlp.with_openai(model: "gpt-6-luna") do |ai|
|
|
887
938
|
result = ai.chat(
|
|
888
939
|
system: "You are a linguistic analyst. Analyze the given linguistic data.",
|
|
889
940
|
user: doc.linguistic_summary
|
|
@@ -900,7 +951,7 @@ require "ruby-spacy"
|
|
|
900
951
|
nlp = Spacy::Language.new("en_core_web_sm")
|
|
901
952
|
texts = ["The bank approved the loan.", "I sat on the river bank."]
|
|
902
953
|
|
|
903
|
-
nlp.with_openai(model: "gpt-
|
|
954
|
+
nlp.with_openai(model: "gpt-6-luna") do |ai|
|
|
904
955
|
nlp.pipe(texts).each do |doc|
|
|
905
956
|
result = ai.chat(
|
|
906
957
|
system: "Identify the meaning of 'bank' in one word based on the linguistic context.",
|
|
@@ -938,7 +989,7 @@ The `Language#with_llm` block API generalizes `with_openai` to multiple provider
|
|
|
938
989
|
|
|
939
990
|
```ruby
|
|
940
991
|
# Requires the ANTHROPIC_API_KEY environment variable
|
|
941
|
-
# Default model: claude-sonnet-5 (override with model: "...")
|
|
992
|
+
# Default model: claude-sonnet-5-5 (override with model: "...")
|
|
942
993
|
nlp.with_llm(provider: :anthropic) do |ai|
|
|
943
994
|
result = ai.chat(
|
|
944
995
|
system: "You are a linguistic analyst.",
|
|
@@ -994,6 +1045,8 @@ result["entities"].each { |ent| puts "#{ent["text"]} (#{ent["label"]})" }
|
|
|
994
1045
|
|
|
995
1046
|
**Note on `temperature`:** for all providers, `temperature` is omitted from requests unless you pass it explicitly (`ai.chat(user: "...", temperature: 0.3)`). Models that reject the parameter (e.g., GPT-5 series, o-series, and current Claude models) are automatically retried once without it, so any model works without per-model configuration.
|
|
996
1047
|
|
|
1048
|
+
**Reasoning and token limits:** reasoning models count the tokens they spend reasoning toward the response limit, which defaults to 4000 (`max_tokens:`, or `max_completion_tokens:` for OpenAI). To set how much they reason, pass `reasoning_effort:` for OpenAI (`"none"`, `"low"`, `"medium"`, `"high"`, ...) or `effort:` for Anthropic (`"low"` to `"max"`), either when opening the block or per `chat` call. `Doc#openai_query` sends `reasoning_effort: "none"`, which models such as `gpt-6-luna` require to call its `get_tokens` tool through Chat Completions; pass another value, or `nil` to leave it out, for a model that rejects it.
|
|
1049
|
+
|
|
997
1050
|
See `examples/llm/` for complete scripts, including a spaCy-vs-LLM NER comparison.
|
|
998
1051
|
|
|
999
1052
|
## Advanced Usage
|
data/Rakefile
CHANGED
|
@@ -23,3 +23,9 @@ task :normalize_permissions do
|
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
Rake::Task["build"].enhance([:normalize_permissions])
|
|
26
|
+
|
|
27
|
+
# Inspect the built gem before it can be pushed: its payload must match
|
|
28
|
+
# spec.files in both directions, and must carry no secrets, machine-local
|
|
29
|
+
# paths, or owner-only modes. `release` depends on `build`, so this is the
|
|
30
|
+
# last thing that runs before a push.
|
|
31
|
+
Rake::Task["build"].enhance { sh "ruby", "scripts/verify_gem.rb" }
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Syntax Tree Gallery
|
|
2
|
+
|
|
3
|
+
Trees drawn by `Doc#syntax_tree` with the [rsyntaxtree](https://github.com/yohasebe/rsyntaxtree) gem. The scripts in `examples/rsyntaxtree/` generate every image here.
|
|
4
|
+
|
|
5
|
+
Noun chunks are shaded grey; a chunk that is also a named entity is shaded orange and labeled with the entity type. The words line up on the bottom row, each joined to its tag by a dashed line. The blue and green are rsyntaxtree's default palette, not marking of any kind. Phrase labels are ruby-spacy's own, derived from the head's POS tag, so they follow whatever annotation scheme the model uses. Both are explained in the [README](../README.md#syntax-trees).
|
|
6
|
+
|
|
7
|
+
## English (`en_core_web_sm`)
|
|
8
|
+
|
|
9
|
+
Projection, the default style:
|
|
10
|
+
|
|
11
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_en_projection.png" alt="English projection" width="769">
|
|
12
|
+
|
|
13
|
+
Chunks style:
|
|
14
|
+
|
|
15
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_en_chunks.png" alt="English chunks" width="739">
|
|
16
|
+
|
|
17
|
+
A highlighted path (`path: [doc[3], doc[11]]`), the dependency path from "fox" to "river":
|
|
18
|
+
|
|
19
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_en_path.png" alt="English path" width="769">
|
|
20
|
+
|
|
21
|
+
With morphology (a shorter sentence — the tables are tall):
|
|
22
|
+
|
|
23
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_en_morphology.png" alt="English morphology" width="900">
|
|
24
|
+
|
|
25
|
+
## Japanese (`ja_core_news_sm`)
|
|
26
|
+
|
|
27
|
+
Projection, with PERSON and GPE highlighted:
|
|
28
|
+
|
|
29
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_ja_projection.png" alt="Japanese projection" width="737">
|
|
30
|
+
|
|
31
|
+
Chunks style:
|
|
32
|
+
|
|
33
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_ja_chunks.png" alt="Japanese chunks" width="752">
|
|
34
|
+
|
|
35
|
+
## Russian (`ru_core_news_sm`)
|
|
36
|
+
|
|
37
|
+
No noun chunk iterator, so `style: :chunks` is unavailable.
|
|
38
|
+
|
|
39
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_ru_projection.png" alt="Russian projection" width="574">
|
|
40
|
+
|
|
41
|
+
With morphology — Russian marks case, gender, animacy, aspect, and voice:
|
|
42
|
+
|
|
43
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_ru_morphology.png" alt="Russian morphology" width="900">
|
|
44
|
+
|
|
45
|
+
## German (`de_core_news_sm`)
|
|
46
|
+
|
|
47
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_de_projection.png" alt="German projection" width="580">
|
|
48
|
+
|
|
49
|
+
## Chinese (`zh_core_web_sm`)
|
|
50
|
+
|
|
51
|
+
No noun chunk iterator, so `style: :chunks` is unavailable.
|
|
52
|
+
|
|
53
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_zh_projection.png" alt="Chinese projection" width="655">
|
|
54
|
+
|
|
55
|
+
## Arabic (right-to-left)
|
|
56
|
+
|
|
57
|
+
spaCy ships no Arabic pipeline, so this tree was parsed with [Stanza](https://stanfordnlp.github.io/stanza/) via [spacy-stanza](https://github.com/explosion/spacy-stanza) and wrapped with `Spacy::Language.new(py_nlp:)`. Right-to-left languages are drawn mirrored automatically. This pipeline has no noun chunks, so nothing is shaded. See `examples/rsyntaxtree/syntax_tree_ar.rb`.
|
|
58
|
+
|
|
59
|
+
<img src="https://github.com/yohasebe/ruby-spacy/blob/main/examples/rsyntaxtree/outputs/tree_ar_projection.png" alt="Arabic projection" width="438">
|
|
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
|
|
|
6
6
|
require "ruby-spacy"
|
|
7
7
|
|
|
8
8
|
# Requires the ANTHROPIC_API_KEY environment variable
|
|
9
|
-
# Default model: claude-sonnet-5 (override with model: "...")
|
|
9
|
+
# Default model: claude-sonnet-5-5 (override with model: "...")
|
|
10
10
|
|
|
11
11
|
nlp = Spacy::Language.new("en_core_web_sm")
|
|
12
12
|
doc = nlp.read("The cat sat on the mat while the dog slept under the table.")
|
|
@@ -10,9 +10,8 @@ nlp = Spacy::Language.new("en_core_web_sm")
|
|
|
10
10
|
doc = nlp.read("Vladimir Nabokov was a")
|
|
11
11
|
|
|
12
12
|
# default parameter values
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
# model: "gpt-4o-mini"
|
|
13
|
+
# max_completion_tokens: 4000
|
|
14
|
+
# model: "gpt-6-luna"
|
|
16
15
|
res = doc.openai_completion(access_token: api_key)
|
|
17
16
|
puts res
|
|
18
17
|
|
|
@@ -10,9 +10,8 @@ nlp = Spacy::Language.new("en_core_web_sm")
|
|
|
10
10
|
doc = nlp.read("The Beatles released 12 studio albums")
|
|
11
11
|
|
|
12
12
|
# default parameter values
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
# model: "gpt-4o-mini"
|
|
13
|
+
# max_completion_tokens: 4000
|
|
14
|
+
# model: "gpt-6-luna"
|
|
16
15
|
res = doc.openai_query(access_token: api_key, prompt: "Translate the text to Japanese.")
|
|
17
16
|
|
|
18
17
|
puts res
|
|
@@ -10,9 +10,8 @@ nlp = Spacy::Language.new("en_core_web_sm")
|
|
|
10
10
|
doc = nlp.read("The Beatles were an English rock band formed in Liverpool in 1960.")
|
|
11
11
|
|
|
12
12
|
# default parameter values
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
# model: "gpt-4o-mini"
|
|
13
|
+
# max_completion_tokens: 4000
|
|
14
|
+
# model: "gpt-6-luna"
|
|
16
15
|
res = doc.openai_query(access_token: api_key, prompt: "Extract the topic of the document and list 10 entities (names, concepts, locations, etc.) that are relevant to the topic.")
|
|
17
16
|
|
|
18
17
|
puts res
|
|
@@ -10,9 +10,8 @@ nlp = Spacy::Language.new("en_core_web_sm")
|
|
|
10
10
|
doc = nlp.read("The Beatles released 12 studio albums")
|
|
11
11
|
|
|
12
12
|
# default parameter values
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
# model: "gpt-4o-mini"
|
|
13
|
+
# max_completion_tokens: 4000
|
|
14
|
+
# model: "gpt-6-luna"
|
|
16
15
|
res = doc.openai_query(
|
|
17
16
|
access_token: api_key,
|
|
18
17
|
prompt: "List token data of each of the words used in the sentence. Add 'meaning' property and value (brief semantic definition) to each token data. Output as a JSON object.",
|
|
@@ -10,9 +10,8 @@ nlp = Spacy::Language.new("en_core_web_sm")
|
|
|
10
10
|
doc = nlp.read("The Beatles released 12 studio albums")
|
|
11
11
|
|
|
12
12
|
# default parameter values
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
# model: "gpt-4o-mini"
|
|
13
|
+
# max_completion_tokens: 4000
|
|
14
|
+
# model: "gpt-6-luna"
|
|
16
15
|
res = doc.openai_query(
|
|
17
16
|
access_token: api_key,
|
|
18
17
|
model: "gpt-4o",
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Arabic via an external pipeline. spaCy ships no trained pipeline for
|
|
4
|
+
# Arabic (or any other right-to-left language), so this example builds one
|
|
5
|
+
# with Stanza through spacy-stanza. These are NOT ruby-spacy dependencies;
|
|
6
|
+
# install them yourself first:
|
|
7
|
+
#
|
|
8
|
+
# pip install spacy-stanza
|
|
9
|
+
# pip install --upgrade "stanza>=1.10" # spacy-stanza's stanza pin is old and conflicts with current torch
|
|
10
|
+
# python -c "import stanza; stanza.download('ar')"
|
|
11
|
+
#
|
|
12
|
+
# Note: at exit, spacy-stanza under PyCall prints a harmless
|
|
13
|
+
# multiprocessing.resource_tracker LoadError to stderr (sys.executable
|
|
14
|
+
# points to ruby, not python); the results are unaffected.
|
|
15
|
+
|
|
16
|
+
# add path to ruby-spacy lib to load path
|
|
17
|
+
$LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
|
|
18
|
+
|
|
19
|
+
require "ruby-spacy"
|
|
20
|
+
require "fileutils"
|
|
21
|
+
|
|
22
|
+
# Requires the rsyntaxtree gem (>= 2.10.0): gem install rsyntaxtree
|
|
23
|
+
|
|
24
|
+
py_nlp = PyCall.import_module("spacy_stanza")
|
|
25
|
+
.load_pipeline("ar", processors: "tokenize,pos,lemma,depparse", verbose: false)
|
|
26
|
+
nlp = Spacy::Language.new(py_nlp: py_nlp)
|
|
27
|
+
doc = nlp.read("قرأ الطالب الجديد كتابا مثيرا في المكتبة أمس.")
|
|
28
|
+
|
|
29
|
+
puts doc.syntax_tree
|
|
30
|
+
|
|
31
|
+
# Right-to-left languages are drawn mirrored automatically (pass
|
|
32
|
+
# mirror: "off" to disable)
|
|
33
|
+
output_dir = File.join(File.dirname(__FILE__), "outputs")
|
|
34
|
+
FileUtils.mkdir_p(output_dir)
|
|
35
|
+
File.binwrite(File.join(output_dir, "tree_ar_projection.png"),
|
|
36
|
+
doc.syntax_tree(format: :png))
|