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_classification.py 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import unittest
  2. import os
  3. from fastNLP.io import DataBundle
  4. from fastNLP.io.pipe.classification import SSTPipe, SST2Pipe, IMDBPipe, YelpFullPipe, YelpPolarityPipe
  5. from fastNLP.io.pipe.classification import ChnSentiCorpPipe, THUCNewsPipe, WeiboSenti100kPipe
  6. @unittest.skipIf('TRAVIS' in os.environ, "Skip in travis")
  7. class TestClassificationPipe(unittest.TestCase):
  8. def test_process_from_file(self):
  9. for pipe in [YelpPolarityPipe, SST2Pipe, IMDBPipe, YelpFullPipe, SSTPipe]:
  10. with self.subTest(pipe=pipe):
  11. print(pipe)
  12. data_bundle = pipe(tokenizer='raw').process_from_file()
  13. print(data_bundle)
  14. class TestRunPipe(unittest.TestCase):
  15. def test_load(self):
  16. for pipe in [IMDBPipe]:
  17. data_bundle = pipe(tokenizer='raw').process_from_file('test/data_for_tests/io/imdb')
  18. print(data_bundle)
  19. @unittest.skipIf('TRAVIS' in os.environ, "Skip in travis")
  20. class TestCNClassificationPipe(unittest.TestCase):
  21. def test_process_from_file(self):
  22. for pipe in [ChnSentiCorpPipe]:
  23. with self.subTest(pipe=pipe):
  24. data_bundle = pipe(bigrams=True, trigrams=True).process_from_file()
  25. print(data_bundle)
  26. class TestRunClassificationPipe(unittest.TestCase):
  27. def test_process_from_file(self):
  28. data_set_dict = {
  29. 'yelp.p': ('test/data_for_tests/io/yelp_review_polarity', YelpPolarityPipe, (6, 6, 6), (1176, 2), False),
  30. 'yelp.f': ('test/data_for_tests/io/yelp_review_full', YelpFullPipe, (6, 6, 6), (1023, 5), False),
  31. 'sst-2': ('test/data_for_tests/io/SST-2', SST2Pipe, (5, 5, 5), (139, 2), True),
  32. 'sst': ('test/data_for_tests/io/SST', SSTPipe, (6, 354, 6), (232, 5), False),
  33. 'imdb': ('test/data_for_tests/io/imdb', IMDBPipe, (6, 6, 6), (1670, 2), False),
  34. 'ChnSentiCorp': ('test/data_for_tests/io/ChnSentiCorp', ChnSentiCorpPipe, (6, 6, 6), (529, 1296, 1483, 2), False),
  35. 'Chn-THUCNews': ('test/data_for_tests/io/THUCNews', THUCNewsPipe, (9, 9, 9), (1864, 9), False),
  36. 'Chn-WeiboSenti100k': ('test/data_for_tests/io/WeiboSenti100k', WeiboSenti100kPipe, (7, 6, 6), (452, 2), False),
  37. }
  38. for k, v in data_set_dict.items():
  39. path, pipe, data_set, vocab, warns = v
  40. with self.subTest(pipe=pipe):
  41. if 'Chn' not in k:
  42. if warns:
  43. with self.assertWarns(Warning):
  44. data_bundle = pipe(tokenizer='raw').process_from_file(path)
  45. else:
  46. data_bundle = pipe(tokenizer='raw').process_from_file(path)
  47. else:
  48. data_bundle = pipe(bigrams=True, trigrams=True).process_from_file(path)
  49. self.assertTrue(isinstance(data_bundle, DataBundle))
  50. self.assertEqual(len(data_set), data_bundle.num_dataset)
  51. for x, y in zip(data_set, data_bundle.iter_datasets()):
  52. name, dataset = y
  53. self.assertEqual(x, len(dataset))
  54. self.assertEqual(len(vocab), data_bundle.num_vocab)
  55. for x, y in zip(vocab, data_bundle.iter_vocabs()):
  56. name, vocabs = y
  57. self.assertEqual(x, len(vocabs))