如何使用spaCy的新DocBin()类反序列化标签数据



我正在尝试使用spacy 中的新DocBin((类将文档数据和属性保存为二进制文件

我以前使用pickle保存过数据,但正在寻找一种更有效的方法。

def serialize_tok((:

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-destrialize_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

只有当依赖解析的属性(HEAD和/或DEP(包含在属性列表中时,它才会被标记为已解析。is_parsed只是用于相关性解析,而不是整个分析。如果你想要的是标签,还有is_tagged

最新更新