Changes in embedding.py, phrase.py, symbol.py, word.py
Added from_dict class methods
This commit is contained in:
1 parent
ac2bb3ce29
commit
7126a66331
4 files changed
+45
-13
No files matched your search
@@ -21,4 +21,8 @@ class Embedding:
|
||||
# Fallback or error if not implemented for other types (e.g., torch.Tensor)
|
||||
raise NotImplementedError(
|
||||
"Comparison must be implemented for all supported data types."
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict) -> "Embedding":
|
||||
return cls(data=np.array(data["data"], dtype=np.float32))
|
||||
@@ -18,6 +18,14 @@ class Phrase(Lexeme):
|
||||
@property
|
||||
def embedding(self) -> Embedding:
|
||||
return self._embedding
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict) -> 'Phrase':
|
||||
return cls(
|
||||
id=data['id'],
|
||||
content=data['content'],
|
||||
embedding=Embedding.from_dict(data['embedding'])
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
|
||||
|
||||
@@ -12,6 +12,14 @@ class Symbol:
|
||||
label: str
|
||||
image: Tensor[np.ndarray[Any, Any], tuple, Literal["int32", "int64"]]
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict) -> 'Symbol':
|
||||
return cls(
|
||||
id=data['id'],
|
||||
label=data['label'],
|
||||
image=np.array(data['image'], dtype=np.int32)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
|
||||
from pydantic_core import core_schema
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from .lexeme import Lexeme
|
||||
from typing import Any
|
||||
from typing import Any
|
||||
from .embedding import Embedding
|
||||
from pydantic_core import core_schema
|
||||
|
||||
@@ -17,32 +17,44 @@ class Word(Lexeme):
|
||||
@property
|
||||
def embedding(self) -> Embedding:
|
||||
return self._embedding
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.content
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict) -> "Word":
|
||||
return cls(
|
||||
word_id=data["id"],
|
||||
content=data["content"],
|
||||
embedding=Embedding.from_dict(data["embedding"]),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
|
||||
from pydantic_core import core_schema
|
||||
|
||||
|
||||
return core_schema.no_info_after_validator_function(
|
||||
lambda x: x if isinstance(x, cls) else cls(**x),
|
||||
core_schema.typed_dict_schema(
|
||||
{
|
||||
'id': core_schema.typed_dict_field(handler.generate_schema(int)),
|
||||
'content': core_schema.typed_dict_field(handler.generate_schema(str)),
|
||||
'embedding': core_schema.typed_dict_field(handler.generate_schema(Embedding)),
|
||||
"id": core_schema.typed_dict_field(handler.generate_schema(int)),
|
||||
"content": core_schema.typed_dict_field(
|
||||
handler.generate_schema(str)
|
||||
),
|
||||
"embedding": core_schema.typed_dict_field(
|
||||
handler.generate_schema(Embedding)
|
||||
),
|
||||
}
|
||||
),
|
||||
serialization=core_schema.plain_serializer_function_ser_schema(
|
||||
lambda x: {
|
||||
'id': x.id,
|
||||
'content': x.content,
|
||||
'embedding': x.embedding # Let Pydantic serialize Embedding
|
||||
"id": x.id,
|
||||
"content": x.content,
|
||||
"embedding": x.embedding, # Let Pydantic serialize Embedding
|
||||
},
|
||||
when_used='json',
|
||||
when_used="json",
|
||||
),
|
||||
)
|
||||
Reference in new issue
Block a user