容器

DocBin

class
将Doc对象打包用于二进制序列化

DocBin 类可高效序列化来自多个 Doc 对象的信息。您可以通过传递属性ID列表来控制序列化的信息内容,并可选择是否序列化用户数据。相比pickle,DocBin 速度更快且生成的数据体积更小,并允许您在反序列化时不执行任意Python代码。该格式的一个显著缺点是您无法轻松地从 DocBin 中提取单个文档。序列化格式采用gzip压缩的msgpack,其msgpack对象具有以下结构:

msgpack 对象结构

单词、标签、标注等字符串在标记数据中以64位哈希值表示,所有出现过的字符串都会通过字符串对象传递。这意味着如果将更多文档打包在一起,存储效率会更高,因为字符串的重复会更少。有关使用示例,请参阅序列化Doc对象的文档。

DocBin.__init__ 方法

创建一个DocBin对象来保存序列化的标注数据。

参数描述
attrsList of attributes to serialize. ORTH (hash of token text) and SPACY (whether the token is followed by whitespace) are always serialized, so they’re not required. Defaults to ("ORTH", "TAG", "HEAD", "DEP", "ENT_IOB", "ENT_TYPE", "ENT_KB_ID", "LEMMA", "MORPH", "POS"). Iterable[str]
store_user_dataWhether to write the Doc.user_data and the values of custom extension attributes to file/bytes. Defaults to False. bool
docsDoc objects to add on initialization. Iterable[Doc]

DocBin.__len__ 方法

获取已添加到DocBin中的Doc对象数量。

参数描述

DocBin.add 方法

Doc的注释添加到DocBin以便序列化。

参数描述
docThe Doc object to add. Doc

DocBin.get_docs 方法

从标注中恢复Doc对象,使用给定的词汇表。

参数描述
vocabThe shared vocab. Vocab

DocBin.merge 方法

使用另一个DocBin的注释来扩展当前DocBin的注释。如果两个DocBin预定义的attrs不匹配,将会引发错误。

参数描述
otherThe DocBin to merge into the current bin. DocBin

DocBin.to_bytes 方法

DocBin的注释序列化为字节字符串。

参数描述

DocBin.from_bytes 方法

从字节字符串反序列化DocBin的注释。

参数描述
bytes_dataThe data to load from. bytes

DocBin.to_disk 方法v3.0

将序列化的DocBin保存到文件。通常使用.spacy扩展名, 生成的结果可作为spacy train的输入数据。

参数描述
pathThe file path, typically with the .spacy extension. Union[str,Path]

DocBin.from_disk 方法v3.0

从文件加载序列化的DocBin。通常使用.spacy扩展名。

参数描述
pathThe file path, typically with the .spacy extension. Union[str,Path]