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_datasets_textfileop.py 3.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 mindspore.dataset as ds
  16. from mindspore import log as logger
  17. import mindspore.dataset.transforms.nlp.utils as nlp
  18. DATA_FILE = "../data/dataset/testTextFileDataset/1.txt"
  19. DATA_ALL_FILE = "../data/dataset/testTextFileDataset/*"
  20. def test_textline_dataset_one_file():
  21. data = ds.TextFileDataset(DATA_FILE)
  22. count = 0
  23. for i in data.create_dict_iterator():
  24. logger.info("{}".format(i["text"]))
  25. count += 1
  26. assert(count == 3)
  27. def test_textline_dataset_all_file():
  28. data = ds.TextFileDataset(DATA_ALL_FILE)
  29. count = 0
  30. for i in data.create_dict_iterator():
  31. logger.info("{}".format(i["text"]))
  32. count += 1
  33. assert(count == 5)
  34. def test_textline_dataset_totext():
  35. data = ds.TextFileDataset(DATA_ALL_FILE, shuffle=False)
  36. count = 0
  37. line = ["This is a text file.", "Another file.", "Be happy every day.", "End of file.", "Good luck to everyone."]
  38. for i in data.create_dict_iterator():
  39. str = nlp.as_text(i["text"])
  40. assert(str == line[count])
  41. count += 1
  42. assert(count == 5)
  43. def test_textline_dataset_num_samples():
  44. data = ds.TextFileDataset(DATA_FILE, num_samples=2)
  45. count = 0
  46. for i in data.create_dict_iterator():
  47. count += 1
  48. assert(count == 2)
  49. def test_textline_dataset_distribution():
  50. data = ds.TextFileDataset(DATA_ALL_FILE, num_shards=2, shard_id=1)
  51. count = 0
  52. for i in data.create_dict_iterator():
  53. count += 1
  54. assert(count == 3)
  55. def test_textline_dataset_repeat():
  56. data = ds.TextFileDataset(DATA_FILE, shuffle=False)
  57. data = data.repeat(3)
  58. count = 0
  59. line = ["This is a text file.", "Be happy every day.", "Good luck to everyone.",
  60. "This is a text file.", "Be happy every day.", "Good luck to everyone.",
  61. "This is a text file.", "Be happy every day.", "Good luck to everyone."]
  62. for i in data.create_dict_iterator():
  63. str = nlp.as_text(i["text"])
  64. assert(str == line[count])
  65. count += 1
  66. assert(count == 9)
  67. def test_textline_dataset_get_datasetsize():
  68. data = ds.TextFileDataset(DATA_FILE)
  69. size = data.get_dataset_size()
  70. assert(size == 3)
  71. if __name__ == "__main__":
  72. test_textline_dataset_one_file()
  73. test_textline_dataset_all_file()
  74. test_textline_dataset_totext()
  75. test_textline_dataset_num_samples()
  76. test_textline_dataset_distribution()
  77. test_textline_dataset_repeat()
  78. test_textline_dataset_get_datasetsize()