Skip to content

Problems converting Doc object to/from json #13397

Discussion options

You must be logged in to vote

So this error message is confusing/misleading, because there's really just one thing you'd need to change in your code for this to work, and that is to call from_json on a Doc object instead of on the class directly:

spacy.tokens.doc.Doc(nlp.vocab).from_json(j)

and then your snippet should work. Or, similarly, look at the example from the docs:

from spacy.tokens import Doc
doc = nlp("All we have to decide is what to do with the time that is given us.")
doc_json = doc.to_json()
deserialized_doc = Doc(nlp.vocab).from_json(doc_json)
assert deserialized_doc.text == doc.text == doc_json["text"]

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by svlandeg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat / serialize Feature: Serialization, saving and loading feat / doc Feature: Doc, Span and Token objects
2 participants
Converted from issue

This discussion was converted from issue #13374 on March 26, 2024 13:29.