mirror of
				https://github.com/explosion/spaCy.git
				synced 2025-11-04 01:48:04 +03:00 
			
		
		
		
	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
							
								
									7cd2228703
								
							
						
					
					
						commit
						489336171a
					
				| 
						 | 
					@ -1081,13 +1081,17 @@ on [serialization methods](/usage/saving-loading/#serialization-methods).
 | 
				
			||||||
> directory.
 | 
					> directory.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```python
 | 
					```python
 | 
				
			||||||
### Custom serialization methods {highlight="6-7,9-11"}
 | 
					### Custom serialization methods {highlight="7-11,13-15"}
 | 
				
			||||||
import srsly
 | 
					import srsly
 | 
				
			||||||
 | 
					from spacy.util import ensure_path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AcronymComponent:
 | 
					class AcronymComponent:
 | 
				
			||||||
    # other methods here...
 | 
					    # other methods here...
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def to_disk(self, path, exclude=tuple()):
 | 
					    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)
 | 
					        srsly.write_json(path / "data.json", self.data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def from_disk(self, path, exclude=tuple()):
 | 
					    def from_disk(self, path, exclude=tuple()):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -202,7 +202,9 @@ the data to and from a JSON file.
 | 
				
			||||||
> rules _with_ the component data.
 | 
					> rules _with_ the component data.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```python
 | 
					```python
 | 
				
			||||||
### {highlight="14-18,20-25"}
 | 
					### {highlight="16-23,25-30"}
 | 
				
			||||||
 | 
					from spacy.util import ensure_path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Language.factory("my_component")
 | 
					@Language.factory("my_component")
 | 
				
			||||||
class CustomComponent:
 | 
					class CustomComponent:
 | 
				
			||||||
    def __init__(self):
 | 
					    def __init__(self):
 | 
				
			||||||
| 
						 | 
					@ -218,6 +220,9 @@ class CustomComponent:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def to_disk(self, path, exclude=tuple()):
 | 
					    def to_disk(self, path, exclude=tuple()):
 | 
				
			||||||
        # This will receive the directory path + /my_component
 | 
					        # This will receive the directory path + /my_component
 | 
				
			||||||
 | 
					        path = ensure_path(path)
 | 
				
			||||||
 | 
					        if not path.exists():
 | 
				
			||||||
 | 
					            path.mkdir()
 | 
				
			||||||
        data_path = path / "data.json"
 | 
					        data_path = path / "data.json"
 | 
				
			||||||
        with data_path.open("w", encoding="utf8") as f:
 | 
					        with data_path.open("w", encoding="utf8") as f:
 | 
				
			||||||
            f.write(json.dumps(self.data))
 | 
					            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.
 | 
					component exposes a `to_disk` method, it will be called with the disk path.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```python
 | 
					```python
 | 
				
			||||||
 | 
					from spacy.util import ensure_path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def to_disk(self, path, exclude=tuple()):
 | 
					def to_disk(self, path, exclude=tuple()):
 | 
				
			||||||
 | 
					    path = ensure_path(path)
 | 
				
			||||||
 | 
					    if not path.exists():
 | 
				
			||||||
 | 
					        path.mkdir()
 | 
				
			||||||
    snek_path = path / "snek.txt"
 | 
					    snek_path = path / "snek.txt"
 | 
				
			||||||
    with snek_path.open("w", encoding="utf8") as snek_file:
 | 
					    with snek_path.open("w", encoding="utf8") as snek_file:
 | 
				
			||||||
        snek_file.write(self.snek)
 | 
					        snek_file.write(self.snek)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user