Spaces:
Running
Running
File size: 27,597 Bytes
a249916 42c888f a249916 42c888f f2c2a9e a249916 6deb98d a249916 f2c2a9e a249916 2c33aa3 a249916 2c33aa3 a249916 57111be a249916 57111be a249916 57111be a249916 57111be a249916 57111be a249916 57111be a249916 57111be a249916 57111be a249916 251bfda a249916 251bfda a249916 251bfda a249916 42c888f f2c2a9e 135117a f2c2a9e 4bc5186 f2c2a9e 135117a 4bc5186 135117a 4bc5186 135117a 4bc5186 135117a 4bc5186 135117a 4bc5186 135117a 4bc5186 135117a 4bc5186 135117a 4bc5186 135117a 4bc5186 42c888f 57111be 536d81c 57111be 536d81c 57111be 4bf9a66 57111be 42c888f f2c2a9e 42c888f f2c2a9e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
import statistics
import unittest
import numpy as np
import torch
from numpy.testing import assert_almost_equal
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
from unittest import TestLoader
from .encoder_models import SBertEncoder, get_encoder
from .semf1 import SemF1, _compute_cosine_similarity, _validate_input_format
from .utils import get_gpu, slice_embeddings, is_nested_list_of_type, flatten_list, compute_f1, Scores
class TestUtils(unittest.TestCase):
def runTest(self):
self.test_get_gpu()
self.test_slice_embeddings()
self.test_is_nested_list_of_type()
self.test_flatten_list()
self.test_compute_f1()
self.test_scores()
def test_get_gpu(self):
gpu_count = torch.cuda.device_count()
gpu_available = torch.cuda.is_available()
# Test single boolean input
self.assertEqual(get_gpu(True), 0 if gpu_available else "cpu")
self.assertEqual(get_gpu(False), "cpu")
# Test single string input
self.assertEqual(get_gpu("cpu"), "cpu")
self.assertEqual(get_gpu("gpu"), 0 if gpu_available else "cpu")
self.assertEqual(get_gpu("cuda"), 0 if gpu_available else "cpu")
# Test single integer input
self.assertEqual(get_gpu(0), 0 if gpu_available else "cpu")
self.assertEqual(get_gpu(1), 1 if gpu_available else "cpu")
# Test list input with unique elements
self.assertEqual(get_gpu([True, "cpu", 0]), [0, "cpu"] if gpu_available else ["cpu", "cpu", "cpu"])
# Test list input with duplicate elements
self.assertEqual(get_gpu([0, 0, "gpu"]), 0 if gpu_available else ["cpu", "cpu", "cpu"])
# Test list input with duplicate elements of different types
self.assertEqual(get_gpu([True, 0, "gpu"]), 0 if gpu_available else ["cpu", "cpu", "cpu"])
# Test list input but only one element
self.assertEqual(get_gpu([True]), 0 if gpu_available else "cpu")
# Test list input with all integers
self.assertEqual(get_gpu(list(range(gpu_count))),
list(range(gpu_count)) if gpu_available else gpu_count * ["cpu"])
with self.assertRaises(ValueError):
get_gpu("invalid")
with self.assertRaises(ValueError):
get_gpu(torch.cuda.device_count())
def test_slice_embeddings(self):
embeddings = np.random.rand(10, 5)
num_sentences = [3, 2, 5]
expected_output = [embeddings[:3], embeddings[3:5], embeddings[5:]]
self.assertTrue(
all(np.array_equal(a, b) for a, b in zip(slice_embeddings(embeddings, num_sentences),
expected_output))
)
num_sentences_nested = [[2, 1], [3, 4]]
expected_output_nested = [[embeddings[:2], embeddings[2:3]], [embeddings[3:6], embeddings[6:]]]
self.assertTrue(
slice_embeddings(embeddings, num_sentences_nested), expected_output_nested
)
with self.assertRaises(TypeError):
slice_embeddings(embeddings, "invalid")
def test_is_nested_list_of_type(self):
# Test case: Depth 0, single element matching element_type
self.assertEqual(is_nested_list_of_type("test", str, 0), (True, ""))
# Test case: Depth 0, single element not matching element_type
is_valid, err_msg = is_nested_list_of_type("test", int, 0)
self.assertEqual(is_valid, False)
# Test case: Depth 1, list of elements matching element_type
self.assertEqual(is_nested_list_of_type(["apple", "banana"], str, 1), (True, ""))
# Test case: Depth 1, list of elements not matching element_type
is_valid, err_msg = is_nested_list_of_type([1, 2, 3], str, 1)
self.assertEqual(is_valid, False)
# Test case: Depth 0 (Wrong), list of elements matching element_type
is_valid, err_msg = is_nested_list_of_type([1, 2, 3], str, 0)
self.assertEqual(is_valid, False)
# Depth 2
self.assertEqual(is_nested_list_of_type([[1, 2], [3, 4]], int, 2), (True, ""))
self.assertEqual(is_nested_list_of_type([['1', '2'], ['3', '4']], str, 2), (True, ""))
is_valid, err_msg = is_nested_list_of_type([[1, 2], ["a", "b"]], int, 2)
self.assertEqual(is_valid, False)
# Depth 3
is_valid, err_msg = is_nested_list_of_type([[[1], [2]], [[3], [4]]], list, 3)
self.assertEqual(is_valid, False)
self.assertEqual(is_nested_list_of_type([[[1], [2]], [[3], [4]]], int, 3), (True, ""))
# Test case: Depth is negative, expecting ValueError
with self.assertRaises(ValueError):
is_nested_list_of_type([1, 2], int, -1)
def test_flatten_list(self):
self.assertEqual(flatten_list([1, [2, 3], [[4], 5]]), [1, 2, 3, 4, 5])
self.assertEqual(flatten_list([]), [])
self.assertEqual(flatten_list([1, 2, 3]), [1, 2, 3])
self.assertEqual(flatten_list([[[[1]]]]), [1])
def test_compute_f1(self):
self.assertAlmostEqual(compute_f1(0.5, 0.5), 0.5)
self.assertAlmostEqual(compute_f1(1, 0), 0.0)
self.assertAlmostEqual(compute_f1(0, 1), 0.0)
self.assertAlmostEqual(compute_f1(1, 1), 1.0)
def test_scores(self):
scores = Scores(precision=0.8, recall=[0.7, 0.9])
self.assertAlmostEqual(scores.f1, compute_f1(0.8, statistics.fmean([0.7, 0.9])))
class TestSBertEncoder(unittest.TestCase):
def setUp(self, device=None):
if device is None:
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
else:
self.device = device
self.model_name = "stsb-roberta-large"
self.batch_size = 8
self.verbose = False
self.encoder = SBertEncoder(self.model_name, self.device, self.batch_size, self.verbose)
def test_initialization(self):
self.assertIsInstance(self.encoder.model, SentenceTransformer)
self.assertEqual(self.encoder.device, self.device)
self.assertEqual(self.encoder.batch_size, self.batch_size)
self.assertEqual(self.encoder.verbose, self.verbose)
def test_encode_single_device(self):
sentences = ["This is a test sentence.", "Here is another sentence."]
embeddings = self.encoder.encode(sentences)
self.assertIsInstance(embeddings, np.ndarray)
self.assertEqual(embeddings.shape[0], len(sentences))
self.assertEqual(embeddings.shape[1], self.encoder.model.get_sentence_embedding_dimension())
def test_encode_multi_device(self):
if torch.cuda.device_count() < 2:
self.skipTest("Multi-GPU test requires at least 2 GPUs.")
else:
devices = ["cuda:0", "cuda:1"]
self.setUp(devices)
sentences = ["This is a test sentence.", "Here is another sentence.", "This is a test sentence."]
embeddings = self.encoder.encode(sentences)
self.assertIsInstance(embeddings, np.ndarray)
self.assertEqual(embeddings.shape[0], 3)
self.assertEqual(embeddings.shape[1], self.encoder.model.get_sentence_embedding_dimension())
class TestGetEncoder(unittest.TestCase):
def setUp(self):
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.batch_size = 8
self.verbose = False
def _base_test(self, model_name):
encoder = get_encoder(model_name, self.device, self.batch_size, self.verbose)
# Assert
self.assertIsInstance(encoder, SBertEncoder)
self.assertEqual(encoder.device, self.device)
self.assertEqual(encoder.batch_size, self.batch_size)
self.assertEqual(encoder.verbose, self.verbose)
def test_get_sbert_encoder(self):
model_name = "stsb-roberta-large"
self._base_test(model_name)
def test_sbert_model(self):
model_name = "all-mpnet-base-v2"
self._base_test(model_name)
def test_huggingface_model(self):
"""Test Huggingface models which work with SBert library"""
model_name = "roberta-base"
self._base_test(model_name)
def test_get_encoder_environment_error(self): # This parameter is used when using patch decorator
model_name = "abc" # Wrong model_name
with self.assertRaises(EnvironmentError):
get_encoder(model_name, self.device, self.batch_size, self.verbose)
def test_get_encoder_other_exception(self):
model_name = "apple/OpenELM-270M" # This model is not supported by SentenceTransformer lib
with self.assertRaises(RuntimeError):
get_encoder(model_name, self.device, self.batch_size, self.verbose)
class TestSemF1(unittest.TestCase):
def setUp(self):
self.semf1_metric = SemF1() # semf1_metric
# Example cases, #Samples = 1
self.untokenized_single_reference_predictions = [
"This is a prediction sentence 1. This is a prediction sentence 2."]
self.untokenized_single_reference_references = [
"This is a reference sentence 1. This is a reference sentence 2."]
self.tokenized_single_reference_predictions = [
["This is a prediction sentence 1.", "This is a prediction sentence 2."],
]
self.tokenized_single_reference_references = [
["This is a reference sentence 1.", "This is a reference sentence 2."],
]
self.untokenized_multi_reference_predictions = [
"Prediction sentence 1. Prediction sentence 2."
]
self.untokenized_multi_reference_references = [
["Reference sentence 1. Reference sentence 2.", "Alternative reference 1. Alternative reference 2."],
]
self.tokenized_multi_reference_predictions = [
["Prediction sentence 1.", "Prediction sentence 2."],
]
self.tokenized_multi_reference_references = [
[
["Reference sentence 1.", "Reference sentence 2."],
["Alternative reference 1.", "Alternative reference 2."]
],
]
self.multi_sample_refs = [
'this is the first reference sample',
'this is the second reference sample',
]
self.multi_sample_preds = [
'this is the first prediction sample',
'this is the second prediction sample',
]
def test_aggregate_multi_sample(self):
"""
check if a `Scores` class is returned instead of a list of
`Scores`
"""
scores = self.semf1_metric.compute(
predictions=self.multi_sample_preds,
references=self.multi_sample_refs,
tokenize_sentences=True,
multi_references=False,
gpu=False,
batch_size=32,
verbose=False,
aggregate=True,
)
self.assertIsInstance(scores, Scores)
print(f'Score: {scores}')
def test_aggregate_untokenized_single_ref(self):
scores = self.semf1_metric.compute(
predictions=self.untokenized_single_reference_predictions,
references=self.untokenized_single_reference_references,
tokenize_sentences=True,
multi_references=False,
gpu=False,
batch_size=32,
verbose=False,
aggregate=True,
)
self.assertIsInstance(scores, Scores)
print(f'Score: {scores}')
def test_aggregate_tokenized_single_ref(self):
scores = self.semf1_metric.compute(
predictions=self.tokenized_single_reference_predictions,
references=self.tokenized_single_reference_references,
tokenize_sentences=False,
multi_references=False,
gpu=False,
batch_size=32,
verbose=False,
aggregate=True,
)
self.assertIsInstance(scores, Scores)
print(f'Score: {scores}')
def test_aggregate_untokenized_multi_ref(self):
scores = self.semf1_metric.compute(
predictions=self.untokenized_multi_reference_predictions,
references=self.untokenized_multi_reference_references,
tokenize_sentences=True,
multi_references=True,
gpu=False,
batch_size=32,
verbose=False,
aggregate=True,
)
self.assertIsInstance(scores, Scores)
print(f'Score: {scores}')
def test_aggregate_tokenized_multi_ref(self):
scores = self.semf1_metric.compute(
predictions=self.tokenized_multi_reference_predictions,
references=self.tokenized_multi_reference_references,
tokenize_sentences=False,
multi_references=True,
gpu=False,
batch_size=32,
verbose=False,
aggregate=True,
)
self.assertIsInstance(scores, Scores)
print(f'Score: {scores}')
def test_aggregate_same_pred_and_ref(self):
scores = self.semf1_metric.compute(
predictions=self.tokenized_single_reference_predictions,
references=self.tokenized_single_reference_predictions,
tokenize_sentences=False,
multi_references=False,
gpu=False,
batch_size=32,
verbose=False,
aggregate=True,
)
self.assertIsInstance(scores, Scores)
print(f'Score: {scores}')
def test_untokenized_single_reference(self):
scores = self.semf1_metric.compute(
predictions=self.untokenized_single_reference_predictions,
references=self.untokenized_single_reference_references,
tokenize_sentences=True,
multi_references=False,
gpu=False,
batch_size=32,
verbose=False
)
self.assertIsInstance(scores, list)
self.assertEqual(len(scores), len(self.untokenized_single_reference_predictions))
def test_tokenized_single_reference(self):
scores = self.semf1_metric.compute(
predictions=self.tokenized_single_reference_predictions,
references=self.tokenized_single_reference_references,
tokenize_sentences=False,
multi_references=False,
gpu=False,
batch_size=32,
verbose=False
)
self.assertIsInstance(scores, list)
self.assertEqual(len(scores), len(self.tokenized_single_reference_predictions))
for score in scores:
self.assertIsInstance(score, Scores)
self.assertTrue(0.0 <= score.precision <= 1.0)
self.assertTrue(all(0.0 <= recall <= 1.0 for recall in score.recall))
def test_untokenized_multi_reference(self):
scores = self.semf1_metric.compute(
predictions=self.untokenized_multi_reference_predictions,
references=self.untokenized_multi_reference_references,
tokenize_sentences=True,
multi_references=True,
gpu=False,
batch_size=32,
verbose=False
)
self.assertIsInstance(scores, list)
self.assertEqual(len(scores), len(self.untokenized_multi_reference_predictions))
def test_tokenized_multi_reference(self):
scores = self.semf1_metric.compute(
predictions=self.tokenized_multi_reference_predictions,
references=self.tokenized_multi_reference_references,
tokenize_sentences=False,
multi_references=True,
gpu=False,
batch_size=32,
verbose=False
)
self.assertIsInstance(scores, list)
self.assertEqual(len(scores), len(self.tokenized_multi_reference_predictions))
for score in scores:
self.assertIsInstance(score, Scores)
self.assertTrue(0.0 <= score.precision <= 1.0)
self.assertTrue(all(0.0 <= recall <= 1.0 for recall in score.recall))
def test_same_predictions_and_references(self):
scores = self.semf1_metric.compute(
predictions=self.tokenized_single_reference_predictions,
references=self.tokenized_single_reference_predictions,
tokenize_sentences=False,
multi_references=False,
gpu=False,
batch_size=32,
verbose=False
)
self.assertIsInstance(scores, list)
self.assertEqual(len(scores), len(self.tokenized_single_reference_predictions))
for score in scores:
self.assertIsInstance(score, Scores)
self.assertAlmostEqual(score.precision, 1.0, places=6)
assert_almost_equal(score.recall, 1, decimal=5, err_msg="Not all values are almost equal to 1")
def test_exact_output_scores(self):
predictions = [
["I go to School.", "You are stupid."],
["I love adventure sports."],
]
references = [
["I go to playground.", "You are genius.", "You need to be admired."],
["I love adventure sports."],
]
scores = self.semf1_metric.compute(
predictions=predictions,
references=references,
tokenize_sentences=False,
multi_references=False,
gpu=False,
batch_size=32,
verbose=False,
model_type="use",
)
self.assertIsInstance(scores, list)
self.assertEqual(len(scores), len(predictions))
score = scores[0]
self.assertIsInstance(score, Scores)
self.assertAlmostEqual(score.precision, 0.73, places=2)
self.assertAlmostEqual(score.recall[0], 0.63, places=2)
def test_none_input(self):
def _call_metric(preds, refs, tok, mul_ref):
with self.assertRaises(Exception) as ctx:
_ = self.semf1_metric.compute(
predictions=preds,
references=refs,
tokenize_sentences=tok,
multi_references=mul_ref,
gpu=False,
batch_size=32,
verbose=False,
model_type="use",
)
print(f"Raised Exception with message: {ctx.exception}")
return ""
# # Case 1: tokenize_sentences = True, multi_references = True
tokenize_sentences = True
multi_references = True
predictions = [
"I go to School. You are stupid.",
"I go to School. You are stupid.",
]
references = [
["I am", "I am"],
[None, "I am"],
]
print(f"Case I\n{_call_metric(predictions, references, tokenize_sentences, multi_references)}\n")
# Case 2: tokenize_sentences = False, multi_references = True
tokenize_sentences = False
multi_references = True
predictions = [
["I go to School.", "You are stupid."],
["I go to School.", "You are stupid."],
]
references = [
[["I am", "I am"], [None, "I am"]],
[[None, "I am"]],
]
print(f"Case II\n{_call_metric(predictions, references, tokenize_sentences, multi_references)}\n")
# Case 3: tokenize_sentences = True, multi_references = False
tokenize_sentences = True
multi_references = False
predictions = [
None,
"I go to School. You are stupid.",
]
references = [
"I am. I am.",
"I am. I am.",
]
print(f"Case III\n{_call_metric(predictions, references, tokenize_sentences, multi_references)}\n")
# Case 4: tokenize_sentences = False, multi_references = False
# This is taken care by the library itself
tokenize_sentences = False
multi_references = False
predictions = [
["I go to School.", None],
["I go to School.", "You are stupid."],
]
references = [
["I am.", "I am."],
["I am.", "I am."],
]
print(f"Case IV\n{_call_metric(predictions, references, tokenize_sentences, multi_references)}\n")
def test_empty_input(self):
predictions = ["", ""]
references = ["I go to School. You are stupid.", "I am"]
scores = self.semf1_metric.compute(
predictions=predictions,
references=references,
)
print(scores)
# # Test with Gibberish Cases
# predictions = ["lth cgezawrxretxdr", "dsfgsdfhsdfh"]
# references = ["dzfgzeWfnAfse", "dtjsrtzerZJSEWr"]
# scores = self.semf1_metric.compute(
# predictions=predictions,
# references=references,
# )
# print(scores)
class TestCosineSimilarity(unittest.TestCase):
def setUp(self):
# Sample embeddings for testing
self.pred_embeds = np.array([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
])
self.ref_embeds = np.array([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
])
self.pred_embeds_random = np.random.rand(3, 3)
self.ref_embeds_random = np.random.rand(3, 3)
def test_cosine_similarity_perfect_match(self):
precision, recall = _compute_cosine_similarity(self.pred_embeds, self.ref_embeds)
# Expected values are 1.0 for both precision and recall since embeddings are identical
self.assertAlmostEqual(precision, 1.0, places=5)
self.assertAlmostEqual(recall, 1.0, places=5)
def _test_cosine_similarity_base(self, pred_embeds, ref_embeds):
precision, recall = _compute_cosine_similarity(pred_embeds, ref_embeds)
# Calculate expected precision and recall using sklearn's cosine similarity function
cosine_scores = cosine_similarity(pred_embeds, ref_embeds)
expected_precision = np.mean(np.max(cosine_scores, axis=-1)).item()
expected_recall = np.mean(np.max(cosine_scores, axis=0)).item()
self.assertAlmostEqual(precision, expected_precision, places=5)
self.assertAlmostEqual(recall, expected_recall, places=5)
def test_cosine_similarity_random(self):
self._test_cosine_similarity_base(self.pred_embeds_random, self.ref_embeds_random)
def test_cosine_similarity_different_shapes(self):
pred_embeds_diff = np.random.rand(5, 3)
ref_embeds_diff = np.random.rand(3, 3)
self._test_cosine_similarity_base(pred_embeds_diff, ref_embeds_diff)
class TestValidateInputFormat(unittest.TestCase):
def setUp(self):
# Sample predictions and references for different scenarios where number of samples = 1
# Note: Naming Convention: # When tokenize_sentences = True (i.e. input is untokenized) and vice-versa
# When tokenize_sentences = True (untokenized input) and multi_references = False
self.untokenized_single_reference_predictions = [
"This is a prediction sentence 1. This is a prediction sentence 2."
]
self.untokenized_single_reference_references = [
"This is a reference sentence 1. This is a reference sentence 2."
]
# When tokenize_sentences = False (tokenized input) and multi_references = False
self.tokenized_single_reference_predictions = [
["This is a prediction sentence 1.", "This is a prediction sentence 2."]
]
self.tokenized_single_reference_references = [
["This is a reference sentence 1.", "This is a reference sentence 2."]
]
# When tokenize_sentences = True (untokenized input) and multi_references = True
self.untokenized_multi_reference_predictions = [
"This is a prediction sentence 1. This is a prediction sentence 2."
]
self.untokenized_multi_reference_references = [
[
"This is a reference sentence 1. This is a reference sentence 2.",
"Another reference sentence."
]
]
# When tokenize_sentences = False (tokenized input) and multi_references = True
self.tokenized_multi_reference_predictions = [
["This is a prediction sentence 1.", "This is a prediction sentence 2."]
]
self.tokenized_multi_reference_references = [
[
["This is a reference sentence 1.", "This is a reference sentence 2."],
["Another reference sentence."]
]
]
def test_tokenized_sentences_true_multi_references_true(self):
# Invalid format should raise an error
with self.assertRaises(ValueError):
_validate_input_format(
True,
True,
self.tokenized_single_reference_predictions,
self.tokenized_single_reference_references,
)
# Valid format should pass without error
_validate_input_format(
True,
True,
self.untokenized_multi_reference_predictions,
self.untokenized_multi_reference_references,
)
def test_tokenized_sentences_false_multi_references_true(self):
# Invalid format should raise an error
with self.assertRaises(ValueError):
_validate_input_format(
False,
True,
self.untokenized_single_reference_predictions,
self.untokenized_multi_reference_references,
)
# Valid format should pass without error
_validate_input_format(
False,
True,
self.tokenized_multi_reference_predictions,
self.tokenized_multi_reference_references,
)
def test_tokenized_sentences_true_multi_references_false(self):
# Invalid format should raise an error
with self.assertRaises(ValueError):
_validate_input_format(
True,
False,
self.tokenized_single_reference_predictions,
self.tokenized_single_reference_references,
)
# Valid format should pass without error
_validate_input_format(
True,
False,
self.untokenized_single_reference_predictions,
self.untokenized_single_reference_references,
)
def test_tokenized_sentences_false_multi_references_false(self):
# Invalid format should raise an error
with self.assertRaises(ValueError):
_validate_input_format(
False,
False,
self.untokenized_single_reference_predictions,
self.untokenized_single_reference_references,
)
# Valid format should pass without error
_validate_input_format(
False,
False,
self.tokenized_single_reference_predictions,
self.tokenized_single_reference_references,
)
def test_mismatched_lengths(self):
# Length mismatch should raise an error
with self.assertRaises(ValueError):
_validate_input_format(
True,
True,
self.untokenized_single_reference_predictions,
[self.untokenized_single_reference_predictions[0], self.untokenized_single_reference_predictions[0]],
)
def run_tests():
unittest.main(verbosity=2)
if __name__ == '__main__':
run_tests()
|