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_basic_tokenizer.py 3.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 BasicTokenizer 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. BASIC_TOKENIZER_FILE = "../data/dataset/testTokenizerData/basic_tokenizer.txt"
  23. test_paras = [
  24. dict(
  25. first=1,
  26. last=6,
  27. expected_tokens=
  28. [['Welcome', 'to', 'Beijing', '北', '京', '欢', '迎', '您'],
  29. ['長', '風', '破', '浪', '會', '有', '時', ',', '直', '掛', '雲', '帆', '濟', '滄', '海'],
  30. ['😀', '嘿', '嘿', '😃', '哈', '哈', '😄', '大', '笑', '😁', '嘻', '嘻'],
  31. ['明', '朝', '(', '1368', '—', '1644', '年', ')', '和', '清', '朝',
  32. '(', '1644', '—', '1911', '年', ')', ',', '是', '中', '国', '封',
  33. '建', '王', '朝', '史', '上', '最', '后', '两', '个', '朝', '代'],
  34. ['明', '代', '(', '1368', '-', '1644', ')', 'と', '清', '代',
  35. '(', '1644', '-', '1911', ')', 'は', '、', '中', '国', 'の', '封',
  36. '建', '王', '朝', 'の', '歴', '史', 'における', '最', '後', 'の2つの', '王', '朝', 'でした'],
  37. ['명나라', '(', '1368', '-', '1644', ')', '와', '청나라', '(', '1644', '-', '1911', ')', '는',
  38. '중국', '봉건', '왕조의', '역사에서', '마지막', '두', '왕조였다']]
  39. ),
  40. dict(
  41. first=7,
  42. last=7,
  43. expected_tokens=[['this', 'is', 'a', 'funky', 'string']],
  44. lower_case=True
  45. ),
  46. ]
  47. def check_basic_tokenizer(first, last, expected_tokens, lower_case=False, keep_whitespace=False,
  48. normalization_form=nlp.utils.NormalizeForm.NONE, preserve_unused_token=False):
  49. dataset = ds.TextFileDataset(BASIC_TOKENIZER_FILE, shuffle=False)
  50. if first > 1:
  51. dataset = dataset.skip(first - 1)
  52. if last >= first:
  53. dataset = dataset.take(last - first + 1)
  54. basic_tokenizer = nlp.BasicTokenizer(lower_case=lower_case,
  55. keep_whitespace=keep_whitespace,
  56. normalization_form=normalization_form,
  57. preserve_unused_token=preserve_unused_token)
  58. dataset = dataset.map(operations=basic_tokenizer)
  59. count = 0
  60. for i in dataset.create_dict_iterator():
  61. text = nlp.to_str(i['text'])
  62. logger.info("Out:", text)
  63. logger.info("Exp:", expected_tokens[count])
  64. np.testing.assert_array_equal(text, expected_tokens[count])
  65. count = count + 1
  66. def test_basic_tokenizer():
  67. """
  68. Test BasicTokenizer
  69. """
  70. for paras in test_paras:
  71. check_basic_tokenizer(**paras)
  72. if __name__ == '__main__':
  73. test_basic_tokenizer()