我正在尝试使用spacy中的新DocBin()类将文档数据和属性保存为二进制
我以前使用pickle保存过数据,但正在寻找一种更有效的方法。
def serialize_to_disk():
doc_bin = DocBin(attrs=["LEMMA", "ENT_IOB", "ENT_TYPE", "POS", "TAG"], store_user_data=True)
for doc in nlp.pipe(ff):
# print(doc.is_parsed) this DOES produce parsed docs
doc_bin.add(doc)
bytes_data = doc_bin.to_bytes()
print(type(bytes_data))
with open("bytes/test", "wb") as binary_file:
binary_file.write(bytes_data)
def deserialize_from_disk():
nlp = spacy.blank("en")
with open("bytes/test", "rb") as f:
data = f.read()
doc_bin = DocBin().from_bytes(data)
docs = list(doc_bin.get_docs(nlp.vocab))
# this list does not have the tag data. Why?
return docs
当我在反序列化的列表上调用doc.is_parsed时,它返回False。在序列化之前,这将返回True
发布于 2019-10-31 22:53:15
只有在属性列表中包含依赖项解析的属性(HEAD
和/或DEP
)时,才会将其标记为已解析。is_parsed
仅用于依赖项解析,而不是整个分析。如果您正在寻找标记器,也可以使用is_tagged
。
https://stackoverflow.com/questions/58645777
复制相似问题