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_wordpiece_tokenizer.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 WordpieceTokenizer 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. WORDPIECE_TOKENIZER_FILE = "../data/dataset/testTokenizerData/wordpiece_tokenizer.txt"
  23. vocab_english = [
  24. "book", "cholera", "era", "favor", "##ite", "my", "is", "love", "dur", "##ing", "the"
  25. ]
  26. vocab_chinese = [
  27. "我", '最', '喜', '欢', '的', '书', '是', '霍', '乱', '时', '期', '爱', '情'
  28. ]
  29. vocab_mix = vocab_chinese + vocab_english
  30. test_paras = [
  31. dict(
  32. first=1,
  33. last=10,
  34. expect_str=[['my'], ['favor', '##ite'], ['book'], ['is'], ['love'], ['dur', '##ing'], ['the'], ['cholera'],
  35. ['era'], ['[UNK]']],
  36. vocab_list=vocab_english
  37. ),
  38. dict(
  39. first=1,
  40. last=10,
  41. expect_str=[['my'], ['favor', '##ite'], ['book'], ['is'], ['love'], ['dur', '##ing'], ['the'], ['cholera'],
  42. ['era'], ['what']],
  43. vocab_list=vocab_english,
  44. unknown_token=""
  45. ),
  46. dict(
  47. first=1,
  48. last=10,
  49. expect_str=[['my'], ['[UNK]'], ['book'], ['is'], ['love'], ['[UNK]'], ['the'], ['[UNK]'], ['era'], ['[UNK]']],
  50. vocab_list=vocab_english,
  51. max_bytes_per_token=4
  52. ),
  53. dict(
  54. first=11,
  55. last=25,
  56. expect_str=[['我'], ['最'], ['喜'], ['欢'], ['的'], ['书'], ['是'], ['霍'], ['乱'], ['时'], ['期'], ['的'], ['爱'], ['情'],
  57. ['[UNK]']],
  58. vocab_list=vocab_chinese,
  59. ),
  60. dict(
  61. first=25,
  62. last=25,
  63. expect_str=[['您']],
  64. vocab_list=vocab_chinese,
  65. unknown_token=""
  66. ),
  67. dict(
  68. first=1,
  69. last=25,
  70. expect_str=[
  71. ['my'], ['favor', '##ite'], ['book'], ['is'], ['love'], ['dur', '##ing'], ['the'], ['cholera'], ['era'],
  72. ['[UNK]'],
  73. ['我'], ['最'], ['喜'], ['欢'], ['的'], ['书'], ['是'], ['霍'], ['乱'], ['时'], ['期'], ['的'], ['爱'], ['情'],
  74. ['[UNK]']],
  75. vocab_list=vocab_mix,
  76. ),
  77. ]
  78. def check_wordpiece_tokenizer(first, last, expect_str, vocab_list, unknown_token='[UNK]', max_bytes_per_token=100):
  79. dataset = ds.TextFileDataset(WORDPIECE_TOKENIZER_FILE, shuffle=False)
  80. if first > 1:
  81. dataset = dataset.skip(first - 1)
  82. if last >= first:
  83. dataset = dataset.take(last - first + 1)
  84. vocab = nlp.Vocab.from_list(vocab_list)
  85. tokenizer_op = nlp.WordpieceTokenizer(vocab=vocab, unknown_token=unknown_token,
  86. max_bytes_per_token=max_bytes_per_token)
  87. dataset = dataset.map(operations=tokenizer_op)
  88. count = 0
  89. for i in dataset.create_dict_iterator():
  90. text = nlp.to_str(i['text'])
  91. logger.info("Out:", text)
  92. logger.info("Exp:", expect_str[count])
  93. np.testing.assert_array_equal(text, expect_str[count])
  94. count = count + 1
  95. def test_wordpiece_tokenizer():
  96. """
  97. Test WordpieceTokenizer
  98. """
  99. for paras in test_paras:
  100. check_wordpiece_tokenizer(**paras)
  101. if __name__ == '__main__':
  102. test_wordpiece_tokenizer()