You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.4 KiB

5 years ago
  1. import io
  2. from django.test import TestCase
  3. from seqeval.metrics.sequence_labeling import get_entities
  4. from ..exceptions import FileParseException
  5. from ..models import Label, Document
  6. from ..utils import BaseStorage, ClassificationStorage, SequenceLabelingStorage, Seq2seqStorage, CoNLLParser
  7. from ..utils import AudioParser, iterable_to_io
  8. class TestBaseStorage(TestCase):
  9. def test_extract_label(self):
  10. data = [{'labels': ['positive']}, {'labels': ['negative']}]
  11. actual = BaseStorage.extract_label(data)
  12. self.assertEqual(actual, [['positive'], ['negative']])
  13. def test_exclude_created_labels(self):
  14. labels = ['positive', 'negative']
  15. created = {'positive': Label(text='positive')}
  16. actual = BaseStorage.exclude_created_labels(labels, created)
  17. self.assertEqual(actual, ['negative'])
  18. def test_to_serializer_format(self):
  19. labels = ['positive']
  20. created = {}
  21. actual = BaseStorage.to_serializer_format(labels, created)
  22. self.assertEqual(len(actual), 1)
  23. self.assertEqual(actual[0]['text'], 'positive')
  24. self.assertIsNone(actual[0]['prefix_key'])
  25. self.assertEqual(actual[0]['suffix_key'], 'p')
  26. self.assertIsNotNone(actual[0]['background_color'])
  27. self.assertIsNotNone(actual[0]['text_color'])
  28. def test_get_shortkey_without_existing_shortkey(self):
  29. label = 'positive'
  30. created = {}
  31. actual = BaseStorage.get_shortkey(label, created)
  32. self.assertEqual(actual, ('p', None))
  33. def test_get_shortkey_with_existing_shortkey(self):
  34. label = 'positive'
  35. created = {('p', None)}
  36. actual = BaseStorage.get_shortkey(label, created)
  37. self.assertEqual(actual, ('p', 'ctrl'))
  38. def test_update_saved_labels(self):
  39. saved = {'positive': Label(text='positive', text_color='#000000')}
  40. new = [Label(text='positive', text_color='#ffffff')]
  41. actual = BaseStorage.update_saved_labels(saved, new)
  42. self.assertEqual(actual['positive'].text_color, '#ffffff')
  43. class TestClassificationStorage(TestCase):
  44. def test_extract_unique_labels(self):
  45. labels = [['positive'], ['positive', 'negative'], ['negative']]
  46. actual = ClassificationStorage.extract_unique_labels(labels)
  47. self.assertCountEqual(actual, ['positive', 'negative'])
  48. def test_make_annotations(self):
  49. docs = [Document(text='a', id=1), Document(text='b', id=2), Document(text='c', id=3)]
  50. labels = [['positive'], ['positive', 'negative'], ['negative']]
  51. saved_labels = {'positive': Label(text='positive', id=1), 'negative': Label(text='negative', id=2)}
  52. actual = ClassificationStorage.make_annotations(docs, labels, saved_labels)
  53. self.assertCountEqual(actual, [
  54. {'document': 1, 'label': 1},
  55. {'document': 2, 'label': 1},
  56. {'document': 2, 'label': 2},
  57. {'document': 3, 'label': 2},
  58. ])
  59. class TestSequenceLabelingStorage(TestCase):
  60. def test_extract_unique_labels(self):
  61. labels = [[[0, 1, 'LOC']], [[3, 4, 'ORG']]]
  62. actual = SequenceLabelingStorage.extract_unique_labels(labels)
  63. self.assertCountEqual(actual, ['LOC', 'ORG'])
  64. def test_make_annotations(self):
  65. docs = [Document(text='a', id=1), Document(text='b', id=2)]
  66. labels = [[[0, 1, 'LOC']], [[3, 4, 'ORG']]]
  67. saved_labels = {'LOC': Label(text='LOC', id=1), 'ORG': Label(text='ORG', id=2)}
  68. actual = SequenceLabelingStorage.make_annotations(docs, labels, saved_labels)
  69. self.assertEqual(actual, [
  70. {'document': 1, 'label': 1, 'start_offset': 0, 'end_offset': 1},
  71. {'document': 2, 'label': 2, 'start_offset': 3, 'end_offset': 4},
  72. ])
  73. class TestSeq2seqStorage(TestCase):
  74. def test_make_annotations(self):
  75. docs = [Document(text='a', id=1), Document(text='b', id=2)]
  76. labels = [['Hello!'], ['How are you?', "What's up?"]]
  77. actual = Seq2seqStorage.make_annotations(docs, labels)
  78. self.assertEqual(actual, [
  79. {'document': 1, 'text': 'Hello!'},
  80. {'document': 2, 'text': 'How are you?'},
  81. {'document': 2, 'text': "What's up?"},
  82. ])
  83. class TestCoNLLParser(TestCase):
  84. def test_calc_char_offset(self):
  85. f = io.BytesIO(
  86. b"EU\tORG\n"
  87. b"rejects\t_\n"
  88. b"German\tMISC\n"
  89. b"call\t_\n"
  90. )
  91. actual = next(CoNLLParser().parse(f))[0]
  92. self.assertEqual(actual, {
  93. 'text': 'EU rejects German call',
  94. 'labels': [[0, 2, 'ORG'], [11, 17, 'MISC']]
  95. })
  96. class TestAudioParser(TestCase):
  97. def test_parse_mp3(self):
  98. f = io.BytesIO(b'...')
  99. f.name = 'test.mp3'
  100. actual = next(AudioParser().parse(f))
  101. self.assertEqual(actual, [{
  102. 'audio': 'data:audio/mpeg;base64,Li4u',
  103. 'meta': '{"filename": "test.mp3"}',
  104. }])
  105. def test_parse_unknown(self):
  106. f = io.BytesIO(b'...')
  107. f.name = 'unknown.unknown'
  108. with self.assertRaises(FileParseException):
  109. next(AudioParser().parse(f))
  110. class TestIterableToIO(TestCase):
  111. def test(self):
  112. def iterable():
  113. yield b'fo'
  114. yield b'o\nbar\n'
  115. yield b'baz\nrest'
  116. stream = iterable_to_io(iterable())
  117. stream = io.TextIOWrapper(stream)
  118. self.assertEqual(stream.readlines(), ['foo\n', 'bar\n', 'baz\n', 'rest'])