class_docstr: 用于构建, 存储和使用 `str` 到 `int` 的一一映射::
  
      from fastNLP.core import Vocabulary
      vocab = Vocabulary()
      word_list = "this is a word list".split()
      # vocab更新自己的字典，输入为list列表
      vocab.update(word_list)
      vocab["word"] # str to int
      vocab.to_word(5) # int to str
  
  :param max_size: `Vocabulary` 的最大大小, 即能存储词的最大数量
      若为 ``None`` , 则不限制大小。
  :param min_freq: 能被记录下的词在文本中的最小出现频率, 应大于或等于 1。
      若小于该频率, 词语将被视为 `unknown`. 若为 ``None`` , 所有文本中的词都被记录。
  :param padding: padding的字符. 如果设置为 ``None`` ,
      则vocabulary中不考虑padding, 也不计入词表大小，为 ``None`` 的情况多在为 label 建立 Vocabulary 的情况。
  :param unknown: unknown的字符，所有未被记录的词在转为 :class:`int` 时将被视为 `unknown` 。
      如果设置为 ``None`` ,则 vocabulary 中不考虑 `unknown`, 也不计入词表大小。
      为 ``None`` 的情况多在为 labe l建立 Vocabulary 的情况
class_docstr_tok: ["\u7528\u4e8e\u6784\u5efa",",","\u5b58\u50a8\u548c\u4f7f\u7528","`","str","`","\u5230","`","int","`","\u7684\u4e00\u4e00\u6620\u5c04",":",":","from","fastNLP.core","import","Vocabulary","vocab","=","Vocabulary","(",")","word_list","=","``","this","is","a","word","list","''",".split","(",")","#","vocab\u66f4\u65b0\u81ea\u5df1\u7684\u5b57\u5178\uff0c\u8f93\u5165\u4e3alist\u5217\u8868","vocab.update","(","word_list",")","vocab","[","``","word","''","]","#","str","to","int","vocab.to_word","(","5",")","#","int","to","str",":","param","max_size",":","`","Vocabulary","`","\u7684\u6700\u5927\u5927\u5c0f",",","\u5373\u80fd\u5b58\u50a8\u8bcd\u7684\u6700\u5927\u6570\u91cf","\u82e5\u4e3a","``","None","``",",","\u5219\u4e0d\u9650\u5236\u5927\u5c0f\u3002",":","param","min_freq",":","\u80fd\u88ab\u8bb0\u5f55\u4e0b\u7684\u8bcd\u5728\u6587\u672c\u4e2d\u7684\u6700\u5c0f\u51fa\u73b0\u9891\u7387",",","\u5e94\u5927\u4e8e\u6216\u7b49\u4e8e","1\u3002","\u82e5\u5c0f\u4e8e\u8be5\u9891\u7387",",","\u8bcd\u8bed\u5c06\u88ab\u89c6\u4e3a","`","unknown","`",".","\u82e5\u4e3a","``","None","``",",","\u6240\u6709\u6587\u672c\u4e2d\u7684\u8bcd\u90fd\u88ab\u8bb0\u5f55\u3002",":","param","padding",":","padding\u7684\u5b57\u7b26",".","\u5982\u679c\u8bbe\u7f6e\u4e3a","``","None","``",",","\u5219vocabulary\u4e2d\u4e0d\u8003\u8651padding",",","\u4e5f\u4e0d\u8ba1\u5165\u8bcd\u8868\u5927\u5c0f\uff0c\u4e3a","``","None","``","\u7684\u60c5\u51b5\u591a\u5728\u4e3a","label","\u5efa\u7acb","Vocabulary","\u7684\u60c5\u51b5\u3002",":","param","unknown",":","unknown\u7684\u5b57\u7b26\uff0c\u6240\u6709\u672a\u88ab\u8bb0\u5f55\u7684\u8bcd\u5728\u8f6c\u4e3a",":","class",":","`","int","`","\u65f6\u5c06\u88ab\u89c6\u4e3a","`","unknown","`","\u3002","\u5982\u679c\u8bbe\u7f6e\u4e3a","``","None","``",",","\u5219","vocabulary","\u4e2d\u4e0d\u8003\u8651","`","unknown","`",",","\u4e5f\u4e0d\u8ba1\u5165\u8bcd\u8868\u5927\u5c0f\u3002","\u4e3a","``","None","``","\u7684\u60c5\u51b5\u591a\u5728\u4e3a","labe","l\u5efa\u7acb","Vocabulary","\u7684\u60c5\u51b5"]
docstr: 使用dataset的对应field中词构建词典::
  
      # remember to use `field_name`
      vocab.from_dataset(train_data1, train_data2, field_name='words', no_create_entry_dataset=[test_data1, test_data2])
  
  :param datasets: 其类型为 :class:`~fastNLP.core.dataset.DataSet` 或者 List[:class:`~fastNLP.core.dataset.DataSet`]。
  :param field_name: 构建词典所使用的 field(s), 支持一个或多个 field，若有多个 DataSet, 每个 DataSet 都必须有这些 field.
      目前支持的field结构: ``str`` , ``List[str]``
  :param no_create_entry_dataset: 可以传入 :class:`~fastNLP.core.dataset.DataSet`, :class:`List` [ :class:`~fastNLP.core.dataset.DataSet` ] 或者 
      ``None`` （默认），建议直接将非训练数据都传入到这个参数。该选项用于接下来的模型会使用预训练的 embedding （包括 ``glove``, ``word2vec`` ,
      ``elmo`` 与 ``bert`` ）且会 finetune 的情况。如果仅使用来自于训练集的数据建立词表，会导致测试集与验证集中的数据无法充分利用到来自于预训练
      embedding 的信息，所以在建立词表的时候将测试集与验证集考虑进来会使得最终的结果更好。
      如果一个词出现在了训练集中，但是没在预训练模型中， embedding 会为它用 ``<UNK>`` 初始化；但如果它是单独的一个 vector ，并且 finetune embedding
      的话，这个词在更新之后可能会有更好的表示；而如果这个词仅出现在了验证集或者测试集中，那么就不能为它们单独建立 vector，而应该让它指向 ``<UNK>`` 这个
      vector 的值。所以只位于 ``no_create_entry_dataset`` 中的 token 将首先从预训练的词表中寻找它的表示，如果找到了，就使用该表示; 如果没有找到，则认
      为该词的表示应该为 ``<UNK>`` 的表示。
  :return: Vocabulary 自身
docstr_tok: ["\u4f7f\u7528dataset\u7684\u5bf9\u5e94field\u4e2d\u8bcd\u6784\u5efa\u8bcd\u5178",":",":","#","remember","to","use","`","field_name","`","vocab.from_dataset","(","train_data1",",","train_data2",",","field_name='words","'",",","no_create_entry_dataset=","[","test_data1",",","test_data2","]",")",":","param","datasets",":","\u5176\u7c7b\u578b\u4e3a",":","class",":","`","~fastNLP.core.dataset.DataSet","`","\u6216\u8005","List","[",":","class",":","`","~fastNLP.core.dataset.DataSet","`","]","\u3002",":","param","field_name",":","\u6784\u5efa\u8bcd\u5178\u6240\u4f7f\u7528\u7684","field","(","s",")",",","\u652f\u6301\u4e00\u4e2a\u6216\u591a\u4e2a","field\uff0c\u82e5\u6709\u591a\u4e2a","DataSet",",","\u6bcf\u4e2a","DataSet","\u90fd\u5fc5\u987b\u6709\u8fd9\u4e9b","field",".","\u76ee\u524d\u652f\u6301\u7684field\u7ed3\u6784",":","``","str","``",",","``","List","[","str","]","``",":","param","no_create_entry_dataset",":","\u53ef\u4ee5\u4f20\u5165",":","class",":","`","~fastNLP.core.dataset.DataSet","`",",",":","class",":","`","List","`","[",":","class",":","`","~fastNLP.core.dataset.DataSet","`","]","\u6216\u8005","``","None","``","\uff08\u9ed8\u8ba4\uff09\uff0c\u5efa\u8bae\u76f4\u63a5\u5c06\u975e\u8bad\u7ec3\u6570\u636e\u90fd\u4f20\u5165\u5230\u8fd9\u4e2a\u53c2\u6570\u3002\u8be5\u9009\u9879\u7528\u4e8e\u63a5\u4e0b\u6765\u7684\u6a21\u578b\u4f1a\u4f7f\u7528\u9884\u8bad\u7ec3\u7684","embedding","\uff08\u5305\u62ec","``","glove","``",",","``","word2vec","``",",","``","elmo","``","\u4e0e","``","bert","``","\uff09\u4e14\u4f1a","finetune","\u7684\u60c5\u51b5\u3002\u5982\u679c\u4ec5\u4f7f\u7528\u6765\u81ea\u4e8e\u8bad\u7ec3\u96c6\u7684\u6570\u636e\u5efa\u7acb\u8bcd\u8868\uff0c\u4f1a\u5bfc\u81f4\u6d4b\u8bd5\u96c6\u4e0e\u9a8c\u8bc1\u96c6\u4e2d\u7684\u6570\u636e\u65e0\u6cd5\u5145\u5206\u5229\u7528\u5230\u6765\u81ea\u4e8e\u9884\u8bad\u7ec3","embedding","\u7684\u4fe1\u606f\uff0c\u6240\u4ee5\u5728\u5efa\u7acb\u8bcd\u8868\u7684\u65f6\u5019\u5c06\u6d4b\u8bd5\u96c6\u4e0e\u9a8c\u8bc1\u96c6\u8003\u8651\u8fdb\u6765\u4f1a\u4f7f\u5f97\u6700\u7ec8\u7684\u7ed3\u679c\u66f4\u597d\u3002","\u5982\u679c\u4e00\u4e2a\u8bcd\u51fa\u73b0\u5728\u4e86\u8bad\u7ec3\u96c6\u4e2d\uff0c\u4f46\u662f\u6ca1\u5728\u9884\u8bad\u7ec3\u6a21\u578b\u4e2d\uff0c","embedding","\u4f1a\u4e3a\u5b83\u7528","``","<","UNK",">","``","\u521d\u59cb\u5316\uff1b\u4f46\u5982\u679c\u5b83\u662f\u5355\u72ec\u7684\u4e00\u4e2a","vector","\uff0c\u5e76\u4e14","finetune","embedding","\u7684\u8bdd\uff0c\u8fd9\u4e2a\u8bcd\u5728\u66f4\u65b0\u4e4b\u540e\u53ef\u80fd\u4f1a\u6709\u66f4\u597d\u7684\u8868\u793a\uff1b\u800c\u5982\u679c\u8fd9\u4e2a\u8bcd\u4ec5\u51fa\u73b0\u5728\u4e86\u9a8c\u8bc1\u96c6\u6216\u8005\u6d4b\u8bd5\u96c6\u4e2d\uff0c\u90a3\u4e48\u5c31\u4e0d\u80fd\u4e3a\u5b83\u4eec\u5355\u72ec\u5efa\u7acb","vector\uff0c\u800c\u5e94\u8be5\u8ba9\u5b83\u6307\u5411","``","<","UNK",">","``","\u8fd9\u4e2a","vector","\u7684\u503c\u3002\u6240\u4ee5\u53ea\u4f4d\u4e8e","``","no_create_entry_dataset","``","\u4e2d\u7684","token","\u5c06\u9996\u5148\u4ece\u9884\u8bad\u7ec3\u7684\u8bcd\u8868\u4e2d\u5bfb\u627e\u5b83\u7684\u8868\u793a\uff0c\u5982\u679c\u627e\u5230\u4e86\uff0c\u5c31\u4f7f\u7528\u8be5\u8868\u793a",";","\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5219\u8ba4","\u4e3a\u8be5\u8bcd\u7684\u8868\u793a\u5e94\u8be5\u4e3a","``","<","UNK",">","``","\u7684\u8868\u793a\u3002",":","return",":","Vocabulary","\u81ea\u8eab"]
code: def from_dataset(
      self,
      *datasets,
      field_name: Union[str, List[str]],
      no_create_entry_dataset=None,
  ):
      r"""
      使用dataset的对应field中词构建词典::
  
          # remember to use `field_name`
          vocab.from_dataset(train_data1, train_data2, field_name='words', no_create_entry_dataset=[test_data1, test_data2])
  
      :param datasets: 其类型为 :class:`~fastNLP.core.dataset.DataSet` 或者 List[:class:`~fastNLP.core.dataset.DataSet`]。
      :param field_name: 构建词典所使用的 field(s), 支持一个或多个 field，若有多个 DataSet, 每个 DataSet 都必须有这些 field.
          目前支持的field结构: ``str`` , ``List[str]``
      :param no_create_entry_dataset: 可以传入 :class:`~fastNLP.core.dataset.DataSet`, :class:`List` [ :class:`~fastNLP.core.dataset.DataSet` ] 或者
          ``None`` （默认），建议直接将非训练数据都传入到这个参数。该选项用于接下来的模型会使用预训练的 embedding （包括 ``glove``, ``word2vec`` ,
          ``elmo`` 与 ``bert`` ）且会 finetune 的情况。如果仅使用来自于训练集的数据建立词表，会导致测试集与验证集中的数据无法充分利用到来自于预训练
          embedding 的信息，所以在建立词表的时候将测试集与验证集考虑进来会使得最终的结果更好。
          如果一个词出现在了训练集中，但是没在预训练模型中， embedding 会为它用 ``<UNK>`` 初始化；但如果它是单独的一个 vector ，并且 finetune embedding
          的话，这个词在更新之后可能会有更好的表示；而如果这个词仅出现在了验证集或者测试集中，那么就不能为它们单独建立 vector，而应该让它指向 ``<UNK>`` 这个
          vector 的值。所以只位于 ``no_create_entry_dataset`` 中的 token 将首先从预训练的词表中寻找它的表示，如果找到了，就使用该表示; 如果没有找到，则认
          为该词的表示应该为 ``<UNK>`` 的表示。
      :return: Vocabulary 自身
  
      """
      if isinstance(field_name, str):
          field_name = [field_name]
      elif not isinstance(field_name, list):
          raise TypeError(
              "invalid argument field_name: {}".format(field_name)
          )
  
      def construct_vocab(ins, no_create_entry=False):
          for fn in field_name:
              field = ins[fn]
              if isinstance(field, str) or not _is_iterable(field):
                  self.add_word(field, no_create_entry=no_create_entry)
              else:
                  if isinstance(field[0], str) or not _is_iterable(
                      field[0]
                  ):
                      for word in field:
                          self.add_word(
                              word, no_create_entry=no_create_entry
                          )
                  else:
                      if not isinstance(
                          field[0][0], str
                      ) and _is_iterable(field[0][0]):
                          raise RuntimeError(
                              "Only support field with 2 dimensions."
                          )
                      for words in field:
                          for word in words:
                              self.add_word(
                                  word, no_create_entry=no_create_entry
                              )
  
      for idx, dataset in enumerate(datasets):
          if isinstance(dataset, DataSet):
              ds_lst = [dataset]
          elif _is_iterable(dataset):
              ds_lst = list(dataset)
          else:
              raise TypeError(
                  f"Only DataSet type is allowed, instead of {type(dataset)}."
              )
  
          try:
              for ds in ds_lst:
                  ds.apply(construct_vocab, progress_bar=None)
          except BaseException as e:
              logger.error(
                  "When processing the `{}` dataset, the following error occurred:".format(
                      idx
                  )
              )
              raise e
  
      if no_create_entry_dataset is not None:
          partial_construct_vocab = partial(
              construct_vocab, no_create_entry=True
          )
          if isinstance(no_create_entry_dataset, DataSet):
              no_create_entry_dataset.apply(
                  partial_construct_vocab, progress_bar=None
              )
          elif isinstance(no_create_entry_dataset, list):
              for dataset in no_create_entry_dataset:
                  if not isinstance(dataset, DataSet):
                      raise TypeError("Only DataSet type is allowed.")
                  dataset.apply(
                      partial_construct_vocab, progress_bar=None
                  )
      return self
  
imports: from collections import Counter
  from functools import partial
  from functools import wraps
  from typing import List, Callable, Union
  from fastNLP.core.dataset import DataSet
  from fastNLP.core.utils.utils import Option
  from fastNLP.core.utils.utils import _is_iterable
  from .log import logger
  import io
label_desc: Use image node_id 19 for calling the Vocabulary obj's underlying member method code with example usage: obj.from_dataset() and returns: self