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.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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', pad],
  47. ["am", pad],
  48. ['mak', '##ing'],
  49. ['small', pad],
  50. ['mistake', '##s'],
  51. ['during', pad],
  52. ['work', '##ing'],
  53. ['hour', '##s']]],
  54. lower_case=True,
  55. vocab_list=vocab_bert
  56. ),
  57. dict(
  58. first=5,
  59. last=5,
  60. expect_str=[[['I', pad],
  61. ["am", pad],
  62. ['mak', '##ing'],
  63. ['small', pad],
  64. ['mistake', '##s'],
  65. ['during', pad],
  66. ['work', '##ing'],
  67. ['hour', '##s']]],
  68. lower_case=False,
  69. vocab_list=vocab_bert
  70. ),
  71. # test emoji tokens
  72. dict(
  73. first=6,
  74. last=7,
  75. expect_str=[
  76. [['😀'], ['嘿'], ['嘿'], ['😃'], ['哈'], ['哈'], ['😄'], ['大'], ['笑'], ['😁'], ['嘻'], ['嘻']],
  77. [['繁'], ['體'], ['字']]],
  78. normalization_form=nlp.utils.NormalizeForm.NFKC,
  79. vocab_list=vocab_bert
  80. ),
  81. # test preserved tokens
  82. dict(
  83. first=8,
  84. last=12,
  85. expect_str=[
  86. [['[UNK]'], ['[CLS]']],
  87. [['[UNK]'], ['[SEP]']],
  88. [['[UNK]'], ['[UNK]']],
  89. [['[UNK]'], ['[PAD]']],
  90. [['[UNK]'], ['[MASK]']],
  91. ],
  92. lower_case=False,
  93. vocab_list=vocab_bert,
  94. preserve_unused_token=True,
  95. ),
  96. # test special symbol
  97. dict(
  98. first=13,
  99. last=13,
  100. expect_str=[[['12'], ['+'], ['/'], ['-'], ['28'], ['='], ['40'], ['/'], ['-'], ['16']]],
  101. preserve_unused_token=True,
  102. vocab_list=vocab_bert
  103. ),
  104. # test non-default parms
  105. dict(
  106. first=8,
  107. last=8,
  108. expect_str=[
  109. [['[UNK]'], [' '], ['[CLS]']],
  110. ],
  111. lower_case=False,
  112. vocab_list=vocab_bert,
  113. preserve_unused_token=True,
  114. keep_whitespace=True
  115. ),
  116. dict(
  117. first=8,
  118. last=8,
  119. expect_str=[
  120. [['unused'], [' '], ['[CLS]']],
  121. ],
  122. lower_case=False,
  123. vocab_list=vocab_bert,
  124. preserve_unused_token=True,
  125. keep_whitespace=True,
  126. unknown_token=''
  127. ),
  128. dict(
  129. first=8,
  130. last=8,
  131. expect_str=[
  132. [['unused'], [' '], ['['], ['CLS'], [']']],
  133. ],
  134. lower_case=False,
  135. vocab_list=vocab_bert,
  136. preserve_unused_token=False,
  137. keep_whitespace=True,
  138. unknown_token=''
  139. ),
  140. ]
  141. def check_bert_tokenizer(first, last, expect_str,
  142. vocab_list,
  143. suffix_indicator='##',
  144. max_bytes_per_token=100, unknown_token='[UNK]',
  145. lower_case=False, keep_whitespace=False,
  146. normalization_form=nlp.utils.NormalizeForm.NONE,
  147. preserve_unused_token=False):
  148. dataset = ds.TextFileDataset(BERT_TOKENIZER_FILE, shuffle=False)
  149. if first > 1:
  150. dataset = dataset.skip(first - 1)
  151. if last >= first:
  152. dataset = dataset.take(last - first + 1)
  153. vocab = nlp.Vocab.from_list(vocab_list)
  154. tokenizer_op = nlp.BertTokenizer(
  155. vocab=vocab, suffix_indicator=suffix_indicator,
  156. max_bytes_per_token=max_bytes_per_token, unknown_token=unknown_token,
  157. lower_case=lower_case, keep_whitespace=keep_whitespace,
  158. normalization_form=normalization_form,
  159. preserve_unused_token=preserve_unused_token)
  160. dataset = dataset.map(operations=tokenizer_op)
  161. count = 0
  162. for i in dataset.create_dict_iterator():
  163. text = nlp.to_str(i['text'])
  164. logger.info("Out:", text)
  165. logger.info("Exp:", expect_str[count])
  166. np.testing.assert_array_equal(text, expect_str[count])
  167. count = count + 1
  168. def test_bert_tokenizer():
  169. """
  170. Test WordpieceTokenizer
  171. """
  172. for paras in test_paras:
  173. check_bert_tokenizer(**paras)
  174. if __name__ == '__main__':
  175. test_bert_tokenizer()