mirror of https://github.com/explosion/spaCy.git
Add `path.mkdir` to custom component examples of `to_disk` (#10348)
* add `path.mkdir` to examples * add ensure_path + mkdir * update highlights
This commit is contained in:
parent
191e8b31fa
commit
01ec6349ea
|
@ -1081,13 +1081,17 @@ on [serialization methods](/usage/saving-loading/#serialization-methods).
|
|||
> directory.
|
||||
|
||||
```python
|
||||
### Custom serialization methods {highlight="6-7,9-11"}
|
||||
### Custom serialization methods {highlight="7-11,13-15"}
|
||||
import srsly
|
||||
from spacy.util import ensure_path
|
||||
|
||||
class AcronymComponent:
|
||||
# other methods here...
|
||||
|
||||
def to_disk(self, path, exclude=tuple()):
|
||||
path = ensure_path(path)
|
||||
if not path.exists():
|
||||
path.mkdir()
|
||||
srsly.write_json(path / "data.json", self.data)
|
||||
|
||||
def from_disk(self, path, exclude=tuple()):
|
||||
|
|
|
@ -202,7 +202,9 @@ the data to and from a JSON file.
|
|||
> rules _with_ the component data.
|
||||
|
||||
```python
|
||||
### {highlight="14-18,20-25"}
|
||||
### {highlight="16-23,25-30"}
|
||||
from spacy.util import ensure_path
|
||||
|
||||
@Language.factory("my_component")
|
||||
class CustomComponent:
|
||||
def __init__(self):
|
||||
|
@ -218,6 +220,9 @@ class CustomComponent:
|
|||
|
||||
def to_disk(self, path, exclude=tuple()):
|
||||
# This will receive the directory path + /my_component
|
||||
path = ensure_path(path)
|
||||
if not path.exists():
|
||||
path.mkdir()
|
||||
data_path = path / "data.json"
|
||||
with data_path.open("w", encoding="utf8") as f:
|
||||
f.write(json.dumps(self.data))
|
||||
|
@ -467,7 +472,12 @@ pipeline package. When you save out a pipeline using `nlp.to_disk` and the
|
|||
component exposes a `to_disk` method, it will be called with the disk path.
|
||||
|
||||
```python
|
||||
from spacy.util import ensure_path
|
||||
|
||||
def to_disk(self, path, exclude=tuple()):
|
||||
path = ensure_path(path)
|
||||
if not path.exists():
|
||||
path.mkdir()
|
||||
snek_path = path / "snek.txt"
|
||||
with snek_path.open("w", encoding="utf8") as snek_file:
|
||||
snek_file.write(self.snek)
|
||||
|
|
Loading…
Reference in New Issue