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_yelp_review.py 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Copyright 2021 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. DATA_POLARITY_DIR = '../data/dataset/testYelpReview/polarity'
  17. DATA_FULL_DIR = '../data/dataset/testYelpReview/full'
  18. def test_yelp_review_polarity_dataset_basic():
  19. """
  20. Feature: Test YelpReviewPolarity Dataset.
  21. Description: read data from a single file.
  22. Expectation: the data is processed successfully.
  23. """
  24. buffer = []
  25. data = ds.YelpReviewDataset(DATA_POLARITY_DIR, usage='test', shuffle=False)
  26. data = data.repeat(2)
  27. data = data.skip(2)
  28. for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  29. buffer.append(d)
  30. assert len(buffer) == 2
  31. def test_yelp_review_full_dataset_basic():
  32. """
  33. Feature: Test YelpReviewFull Dataset.
  34. Description: read data from a single file.
  35. Expectation: the data is processed successfully.
  36. """
  37. buffer = []
  38. data = ds.YelpReviewDataset(DATA_FULL_DIR, usage='test', shuffle=False)
  39. data = data.repeat(2)
  40. data = data.skip(2)
  41. for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  42. buffer.append(d)
  43. assert len(buffer) == 2
  44. def test_yelp_review_dataset_quoted():
  45. """
  46. Feature: Test get the YelpReview Dataset.
  47. Description: read YelpReviewPolarityDataset data and get data.
  48. Expectation: the data is processed successfully.
  49. """
  50. data = ds.YelpReviewDataset(DATA_POLARITY_DIR, usage='test', shuffle=False)
  51. buffer = []
  52. for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  53. buffer.extend([d['label'].item().decode("utf8"),
  54. d['text'].item().decode("utf8")])
  55. assert buffer == ["2", "\\\"Yelp\\\" service was very good.\\n",
  56. "1", "\\\"Yelp\\\" service was very bad.\\n"]
  57. def test_yelp_review_dataset_usage_all():
  58. """
  59. Feature: Test YelpReviewPolarity Dataset(usage=all).
  60. Description: read train data and test data.
  61. Expectation: the data is processed successfully.
  62. """
  63. buffer = []
  64. data = ds.YelpReviewDataset(DATA_POLARITY_DIR, usage='all', shuffle=False)
  65. for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  66. buffer.append(d)
  67. assert len(buffer) == 5
  68. def test_yelp_review_dataset_get_datasetsize():
  69. """
  70. Feature: Test Getters.
  71. Description: test get_dataset_size of YelpReview dataset.
  72. Expectation: the data is processed successfully.
  73. """
  74. data = ds.YelpReviewDataset(DATA_POLARITY_DIR, usage='test', shuffle=False)
  75. size = data.get_dataset_size()
  76. assert size == 2
  77. def test_yelp_review_dataset_distribution():
  78. """
  79. Feature: Test YelpReviewDataset in distribution.
  80. Description: test in a distributed state.
  81. Expectation: the data is processed successfully.
  82. """
  83. data = ds.YelpReviewDataset(DATA_POLARITY_DIR, usage='test', shuffle=False, num_shards=2, shard_id=0)
  84. count = 0
  85. for _ in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  86. count += 1
  87. assert count == 1
  88. def test_yelp_review_dataset_num_samples():
  89. """
  90. Feature: Test YelpReview Dataset(num_samples = 2).
  91. Description: test get num_samples.
  92. Expectation: the data is processed successfully.
  93. """
  94. data = ds.YelpReviewDataset(DATA_POLARITY_DIR, usage='test', shuffle=False, num_samples=2)
  95. count = 0
  96. for _ in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  97. count += 1
  98. assert count == 2
  99. def test_yelp_review_dataset_exception():
  100. """
  101. Feature: Error Test.
  102. Description: test the wrong input.
  103. Expectation: unable to read in data.
  104. """
  105. def exception_func(item):
  106. raise Exception("Error occur!")
  107. try:
  108. data = ds.YelpReviewDataset(DATA_POLARITY_DIR, usage='test', shuffle=False)
  109. data = data.map(operations=exception_func, input_columns=["label"], num_parallel_workers=1)
  110. for _ in data.create_dict_iterator():
  111. pass
  112. assert False
  113. except RuntimeError as e:
  114. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  115. try:
  116. data = ds.YelpReviewDataset(DATA_POLARITY_DIR, usage='test', shuffle=False)
  117. data = data.map(operations=exception_func, input_columns=["text"], num_parallel_workers=1)
  118. for _ in data.create_dict_iterator():
  119. pass
  120. assert False
  121. except RuntimeError as e:
  122. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  123. if __name__ == "__main__":
  124. test_yelp_review_polarity_dataset_basic()
  125. test_yelp_review_full_dataset_basic()
  126. test_yelp_review_dataset_quoted()
  127. test_yelp_review_dataset_usage_all()
  128. test_yelp_review_dataset_get_datasetsize()
  129. test_yelp_review_dataset_distribution()
  130. test_yelp_review_dataset_num_samples()
  131. test_yelp_review_dataset_exception()