You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_bert_tokenizer.py 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """
  16. Testing BertTokenizer op in DE
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. from mindspore import log as logger
  21. import mindspore.dataset.text as nlp
  22. BERT_TOKENIZER_FILE = "../data/dataset/testTokenizerData/bert_tokenizer.txt"
  23. vocab_bert = [
  24. "床", "前", "明", "月", "光", "疑", "是", "地", "上", "霜", "举", "头", "望", "低", "思", "故", "乡",
  25. "繁", "體", "字", "嘿", "哈", "大", "笑", "嘻",
  26. "i", "am", "mak", "make", "small", "mistake", "##s", "during", "work", "##ing", "hour",
  27. "😀", "😃", "😄", "😁", "+", "/", "-", "=", "12", "28", "40", "16", " ", "I",
  28. "[CLS]", "[SEP]", "[UNK]", "[PAD]", "[MASK]"
  29. ]
  30. pad = '<pad>'
  31. test_paras = [
  32. # test chinese text
  33. dict(
  34. first=1,
  35. last=4,
  36. expect_str=[['床', '前', '明', '月', '光'],
  37. ['疑', '是', '地', '上', '霜'],
  38. ['举', '头', '望', '明', '月'],
  39. ['低', '头', '思', '故', '乡']],
  40. vocab_list=vocab_bert
  41. ),
  42. # test english text
  43. dict(
  44. first=5,
  45. last=5,
  46. expect_str=[['i', 'am', 'mak', '##ing', 'small', 'mistake', '##s', 'during', 'work', '##ing', 'hour', '##s']],
  47. lower_case=True,
  48. vocab_list=vocab_bert
  49. ),
  50. dict(
  51. first=5,
  52. last=5,
  53. expect_str=[['I', "am", 'mak', '##ing', 'small', 'mistake', '##s', 'during', 'work', '##ing', 'hour', '##s']],
  54. lower_case=False,
  55. vocab_list=vocab_bert
  56. ),
  57. # test emoji tokens
  58. dict(
  59. first=6,
  60. last=7,
  61. expect_str=[
  62. ['😀', '嘿', '嘿', '😃', '哈', '哈', '😄', '大', '笑', '😁', '嘻', '嘻'],
  63. ['繁', '體', '字']],
  64. normalization_form=nlp.utils.NormalizeForm.NFKC,
  65. vocab_list=vocab_bert
  66. ),
  67. # test preserved tokens
  68. dict(
  69. first=8,
  70. last=12,
  71. expect_str=[
  72. ['[UNK]', '[CLS]'],
  73. ['[UNK]', '[SEP]'],
  74. ['[UNK]', '[UNK]'],
  75. ['[UNK]', '[PAD]'],
  76. ['[UNK]', '[MASK]'],
  77. ],
  78. lower_case=False,
  79. vocab_list=vocab_bert,
  80. preserve_unused_token=True,
  81. ),
  82. # test special symbol
  83. dict(
  84. first=13,
  85. last=13,
  86. expect_str=[['12', '+', '/', '-', '28', '=', '40', '/', '-', '16']],
  87. preserve_unused_token=True,
  88. vocab_list=vocab_bert
  89. ),
  90. # test non-default parms
  91. dict(
  92. first=8,
  93. last=8,
  94. expect_str=[['[UNK]', ' ', '[CLS]']],
  95. lower_case=False,
  96. vocab_list=vocab_bert,
  97. preserve_unused_token=True,
  98. keep_whitespace=True
  99. ),
  100. dict(
  101. first=8,
  102. last=8,
  103. expect_str=[['unused', ' ', '[CLS]']],
  104. lower_case=False,
  105. vocab_list=vocab_bert,
  106. preserve_unused_token=True,
  107. keep_whitespace=True,
  108. unknown_token=''
  109. ),
  110. dict(
  111. first=8,
  112. last=8,
  113. expect_str=[['unused', ' ', '[', 'CLS', ']']],
  114. lower_case=False,
  115. vocab_list=vocab_bert,
  116. preserve_unused_token=False,
  117. keep_whitespace=True,
  118. unknown_token=''
  119. ),
  120. ]
  121. def check_bert_tokenizer(first, last, expect_str,
  122. vocab_list,
  123. suffix_indicator='##',
  124. max_bytes_per_token=100, unknown_token='[UNK]',
  125. lower_case=False, keep_whitespace=False,
  126. normalization_form=nlp.utils.NormalizeForm.NONE,
  127. preserve_unused_token=False):
  128. dataset = ds.TextFileDataset(BERT_TOKENIZER_FILE, shuffle=False)
  129. if first > 1:
  130. dataset = dataset.skip(first - 1)
  131. if last >= first:
  132. dataset = dataset.take(last - first + 1)
  133. vocab = nlp.Vocab.from_list(vocab_list)
  134. tokenizer_op = nlp.BertTokenizer(
  135. vocab=vocab, suffix_indicator=suffix_indicator,
  136. max_bytes_per_token=max_bytes_per_token, unknown_token=unknown_token,
  137. lower_case=lower_case, keep_whitespace=keep_whitespace,
  138. normalization_form=normalization_form,
  139. preserve_unused_token=preserve_unused_token)
  140. dataset = dataset.map(operations=tokenizer_op)
  141. count = 0
  142. for i in dataset.create_dict_iterator():
  143. text = nlp.to_str(i['text'])
  144. logger.info("Out:", text)
  145. logger.info("Exp:", expect_str[count])
  146. np.testing.assert_array_equal(text, expect_str[count])
  147. count = count + 1
  148. def test_bert_tokenizer():
  149. """
  150. Test WordpieceTokenizer
  151. """
  152. for paras in test_paras:
  153. check_bert_tokenizer(**paras)
  154. if __name__ == '__main__':
  155. test_bert_tokenizer()