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_vocab.py 5.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. import numpy as np
  16. import mindspore.dataset as ds
  17. import mindspore.dataset.text as text
  18. # this file contains "home is behind the world head" each word is 1 line
  19. DATA_FILE = "../data/dataset/testVocab/words.txt"
  20. VOCAB_FILE = "../data/dataset/testVocab/vocab_list.txt"
  21. SIMPLE_VOCAB_FILE = "../data/dataset/testVocab/simple_vocab_list.txt"
  22. def test_from_list_tutorial():
  23. vocab = text.Vocab.from_list("home IS behind the world ahead !".split(" "), ["<pad>", "<unk>"], True)
  24. lookup = text.Lookup(vocab)
  25. data = ds.TextFileDataset(DATA_FILE, shuffle=False)
  26. data = data.map(input_columns=["text"], operations=lookup)
  27. ind = 0
  28. res = [2, 1, 4, 5, 6, 7]
  29. for d in data.create_dict_iterator():
  30. assert d["text"] == res[ind], ind
  31. ind += 1
  32. def test_from_file_tutorial():
  33. vocab = text.Vocab.from_file(VOCAB_FILE, ",", None, ["<pad>", "<unk>"], True)
  34. lookup = text.Lookup(vocab)
  35. data = ds.TextFileDataset(DATA_FILE, shuffle=False)
  36. data = data.map(input_columns=["text"], operations=lookup)
  37. ind = 0
  38. res = [10, 11, 12, 15, 13, 14]
  39. for d in data.create_dict_iterator():
  40. assert d["text"] == res[ind], ind
  41. ind += 1
  42. def test_from_dict_tutorial():
  43. vocab = text.Vocab.from_dict({"home": 3, "behind": 2, "the": 4, "world": 5, "<unk>": 6})
  44. lookup = text.Lookup(vocab, 6) # default value is -1
  45. data = ds.TextFileDataset(DATA_FILE, shuffle=False)
  46. data = data.map(input_columns=["text"], operations=lookup)
  47. res = [3, 6, 2, 4, 5, 6]
  48. ind = 0
  49. for d in data.create_dict_iterator():
  50. assert d["text"] == res[ind], ind
  51. ind += 1
  52. def test_from_list():
  53. def gen(texts):
  54. for word in texts.split(" "):
  55. yield (np.array(word, dtype='S'),)
  56. def test_config(lookup_str, vocab_input, special_tokens, special_first):
  57. try:
  58. vocab = text.Vocab.from_list(vocab_input, special_tokens, special_first)
  59. data = ds.GeneratorDataset(gen(lookup_str), column_names=["text"])
  60. data = data.map(input_columns=["text"], operations=text.Lookup(vocab))
  61. res = []
  62. for d in data.create_dict_iterator():
  63. res.append(d["text"].item())
  64. return res
  65. except ValueError as e:
  66. return str(e)
  67. # test normal operations
  68. assert test_config("w1 w2 w3 s1 s2", ["w1", "w2", "w3"], ["s1", "s2"], True) == [2, 3, 4, 0, 1]
  69. assert test_config("w1 w2 w3 s1 s2", ["w1", "w2", "w3"], ["s1", "s2"], False) == [0, 1, 2, 3, 4]
  70. assert test_config("w3 w2 w1", ["w1", "w2", "w3"], None, True) == [2, 1, 0]
  71. assert test_config("w3 w2 w1", ["w1", "w2", "w3"], None, False) == [2, 1, 0]
  72. # test exceptions
  73. assert "word_list contains duplicate" in test_config("w1", ["w1", "w1"], [], True)
  74. assert "special_tokens contains duplicate" in test_config("w1", ["w1", "w2"], ["s1", "s1"], True)
  75. assert "special_tokens and word_list contain duplicate" in test_config("w1", ["w1", "w2"], ["s1", "w1"], True)
  76. def test_from_file():
  77. def gen(texts):
  78. for word in texts.split(" "):
  79. yield (np.array(word, dtype='S'),)
  80. def test_config(lookup_str, vocab_size, special_tokens, special_first):
  81. try:
  82. vocab = text.Vocab.from_file(SIMPLE_VOCAB_FILE, vocab_size=vocab_size, special_tokens=special_tokens,
  83. special_first=special_first)
  84. data = ds.GeneratorDataset(gen(lookup_str), column_names=["text"])
  85. data = data.map(input_columns=["text"], operations=text.Lookup(vocab))
  86. res = []
  87. for d in data.create_dict_iterator():
  88. res.append(d["text"].item())
  89. return res
  90. except ValueError as e:
  91. return str(e)
  92. # test special tokens are prepended
  93. assert test_config("w1 w2 w3 s1 s2 s3", None, ["s1", "s2", "s3"], True) == [3, 4, 5, 0, 1, 2]
  94. # test special tokens are appended
  95. assert test_config("w1 w2 w3 s1 s2 s3", None, ["s1", "s2", "s3"], False) == [0, 1, 2, 8, 9, 10]
  96. # test special tokens are prepended when not all words in file are used
  97. assert test_config("w1 w2 w3 s1 s2 s3", 3, ["s1", "s2", "s3"], False) == [0, 1, 2, 3, 4, 5]
  98. # text exception special_words contains duplicate words
  99. assert "special_tokens contains duplicate" in test_config("w1", None, ["s1", "s1"], True)
  100. if __name__ == '__main__':
  101. test_from_list_tutorial()
  102. test_from_file_tutorial()
  103. test_from_dict_tutorial()
  104. test_from_list()
  105. test_from_file()