File size: 30,921 Bytes
0f7793d |
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 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 |
"""
Unit tests for nltk.tokenize.
See also nltk/test/tokenize.doctest
"""
from typing import List, Tuple
import pytest
from nltk.tokenize import (
LegalitySyllableTokenizer,
StanfordSegmenter,
SyllableTokenizer,
TreebankWordTokenizer,
TweetTokenizer,
punkt,
sent_tokenize,
word_tokenize,
)
def load_stanford_segmenter():
try:
seg = StanfordSegmenter()
seg.default_config("ar")
seg.default_config("zh")
return True
except LookupError:
return False
check_stanford_segmenter = pytest.mark.skipif(
not load_stanford_segmenter(),
reason="NLTK was unable to find stanford-segmenter.jar.",
)
class TestTokenize:
def test_tweet_tokenizer(self):
"""
Test TweetTokenizer using words with special and accented characters.
"""
tokenizer = TweetTokenizer(strip_handles=True, reduce_len=True)
s9 = "@myke: Let's test these words: resumé España München français"
tokens = tokenizer.tokenize(s9)
expected = [
":",
"Let's",
"test",
"these",
"words",
":",
"resumé",
"España",
"München",
"français",
]
assert tokens == expected
@pytest.mark.parametrize(
"test_input, expecteds",
[
(
"My text 0106404243030 is great text",
(
["My", "text", "01064042430", "30", "is", "great", "text"],
["My", "text", "0106404243030", "is", "great", "text"],
),
),
(
"My ticket id is 1234543124123",
(
["My", "ticket", "id", "is", "12345431241", "23"],
["My", "ticket", "id", "is", "1234543124123"],
),
),
(
"@remy: This is waaaaayyyy too much for you!!!!!! 01064042430",
(
[
":",
"This",
"is",
"waaayyy",
"too",
"much",
"for",
"you",
"!",
"!",
"!",
"01064042430",
],
[
":",
"This",
"is",
"waaayyy",
"too",
"much",
"for",
"you",
"!",
"!",
"!",
"01064042430",
],
),
),
# Further tests from https://github.com/nltk/nltk/pull/2798#issuecomment-922533085,
# showing the TweetTokenizer performance for `match_phone_numbers=True` and
# `match_phone_numbers=False`.
(
# Some phone numbers are always tokenized, even with `match_phone_numbers=`False`
"My number is 06-46124080, except it's not.",
(
[
"My",
"number",
"is",
"06-46124080",
",",
"except",
"it's",
"not",
".",
],
[
"My",
"number",
"is",
"06-46124080",
",",
"except",
"it's",
"not",
".",
],
),
),
(
# Phone number here is only tokenized correctly if `match_phone_numbers=True`
"My number is 601-984-4813, except it's not.",
(
[
"My",
"number",
"is",
"601-984-4813",
",",
"except",
"it's",
"not",
".",
],
[
"My",
"number",
"is",
"601-984-",
"4813",
",",
"except",
"it's",
"not",
".",
],
),
),
(
# Phone number here is only tokenized correctly if `match_phone_numbers=True`
"My number is (393) 928 -3010, except it's not.",
(
[
"My",
"number",
"is",
"(393) 928 -3010",
",",
"except",
"it's",
"not",
".",
],
[
"My",
"number",
"is",
"(",
"393",
")",
"928",
"-",
"3010",
",",
"except",
"it's",
"not",
".",
],
),
),
(
# A long number is tokenized correctly only if `match_phone_numbers=False`
"The product identification number is 48103284512.",
(
[
"The",
"product",
"identification",
"number",
"is",
"4810328451",
"2",
".",
],
[
"The",
"product",
"identification",
"number",
"is",
"48103284512",
".",
],
),
),
(
# `match_phone_numbers=True` can have some unforeseen
"My favourite substraction is 240 - 1353.",
(
["My", "favourite", "substraction", "is", "240 - 1353", "."],
["My", "favourite", "substraction", "is", "240", "-", "1353", "."],
),
),
],
)
def test_tweet_tokenizer_expanded(
self, test_input: str, expecteds: Tuple[List[str], List[str]]
):
"""
Test `match_phone_numbers` in TweetTokenizer.
Note that TweetTokenizer is also passed the following for these tests:
* strip_handles=True
* reduce_len=True
:param test_input: The input string to tokenize using TweetTokenizer.
:type test_input: str
:param expecteds: A 2-tuple of tokenized sentences. The first of the two
tokenized is the expected output of tokenization with `match_phone_numbers=True`.
The second of the two tokenized lists is the expected output of tokenization
with `match_phone_numbers=False`.
:type expecteds: Tuple[List[str], List[str]]
"""
for match_phone_numbers, expected in zip([True, False], expecteds):
tokenizer = TweetTokenizer(
strip_handles=True,
reduce_len=True,
match_phone_numbers=match_phone_numbers,
)
predicted = tokenizer.tokenize(test_input)
assert predicted == expected
def test_sonority_sequencing_syllable_tokenizer(self):
"""
Test SyllableTokenizer tokenizer.
"""
tokenizer = SyllableTokenizer()
tokens = tokenizer.tokenize("justification")
assert tokens == ["jus", "ti", "fi", "ca", "tion"]
def test_syllable_tokenizer_numbers(self):
"""
Test SyllableTokenizer tokenizer.
"""
tokenizer = SyllableTokenizer()
text = "9" * 10000
tokens = tokenizer.tokenize(text)
assert tokens == [text]
def test_legality_principle_syllable_tokenizer(self):
"""
Test LegalitySyllableTokenizer tokenizer.
"""
from nltk.corpus import words
test_word = "wonderful"
tokenizer = LegalitySyllableTokenizer(words.words())
tokens = tokenizer.tokenize(test_word)
assert tokens == ["won", "der", "ful"]
@check_stanford_segmenter
def test_stanford_segmenter_arabic(self):
"""
Test the Stanford Word Segmenter for Arabic (default config)
"""
seg = StanfordSegmenter()
seg.default_config("ar")
sent = "يبحث علم الحاسوب استخدام الحوسبة بجميع اشكالها لحل المشكلات"
segmented_sent = seg.segment(sent.split())
assert segmented_sent.split() == [
"يبحث",
"علم",
"الحاسوب",
"استخدام",
"الحوسبة",
"ب",
"جميع",
"اشكال",
"ها",
"ل",
"حل",
"المشكلات",
]
@check_stanford_segmenter
def test_stanford_segmenter_chinese(self):
"""
Test the Stanford Word Segmenter for Chinese (default config)
"""
seg = StanfordSegmenter()
seg.default_config("zh")
sent = "这是斯坦福中文分词器测试"
segmented_sent = seg.segment(sent.split())
assert segmented_sent.split() == ["这", "是", "斯坦福", "中文", "分词器", "测试"]
def test_phone_tokenizer(self):
"""
Test a string that resembles a phone number but contains a newline
"""
# Should be recognized as a phone number, albeit one with multiple spaces
tokenizer = TweetTokenizer()
test1 = "(393) 928 -3010"
expected = ["(393) 928 -3010"]
result = tokenizer.tokenize(test1)
assert result == expected
# Due to newline, first three elements aren't part of a phone number;
# fourth is
test2 = "(393)\n928 -3010"
expected = ["(", "393", ")", "928 -3010"]
result = tokenizer.tokenize(test2)
assert result == expected
def test_emoji_tokenizer(self):
"""
Test a string that contains Emoji ZWJ Sequences and skin tone modifier
"""
tokenizer = TweetTokenizer()
# A Emoji ZWJ Sequences, they together build as a single emoji, should not be split.
test1 = "👨👩👧👧"
expected = ["👨👩👧👧"]
result = tokenizer.tokenize(test1)
assert result == expected
# A Emoji with skin tone modifier, the two characters build a single emoji, should not be split.
test2 = "👨🏿"
expected = ["👨🏿"]
result = tokenizer.tokenize(test2)
assert result == expected
# A string containing both skin tone modifier and ZWJ Sequences
test3 = "🤔 🙈 me así, se😌 ds 💕👭👙 hello 👩🏾🎓 emoji hello 👨👩👦👦 how are 😊 you today🙅🏽🙅🏽"
expected = [
"🤔",
"🙈",
"me",
"así",
",",
"se",
"😌",
"ds",
"💕",
"👭",
"👙",
"hello",
"👩🏾\u200d🎓",
"emoji",
"hello",
"👨\u200d👩\u200d👦\u200d👦",
"how",
"are",
"😊",
"you",
"today",
"🙅🏽",
"🙅🏽",
]
result = tokenizer.tokenize(test3)
assert result == expected
# emoji flag sequences, including enclosed letter pairs
# Expected behavior from #3034
test4 = "🇦🇵🇵🇱🇪"
expected = ["🇦🇵", "🇵🇱", "🇪"]
result = tokenizer.tokenize(test4)
assert result == expected
test5 = "Hi 🇨🇦, 😍!!"
expected = ["Hi", "🇨🇦", ",", "😍", "!", "!"]
result = tokenizer.tokenize(test5)
assert result == expected
test6 = "<3 🇨🇦 🤝 🇵🇱 <3"
expected = ["<3", "🇨🇦", "🤝", "🇵🇱", "<3"]
result = tokenizer.tokenize(test6)
assert result == expected
def test_pad_asterisk(self):
"""
Test padding of asterisk for word tokenization.
"""
text = "This is a, *weird sentence with *asterisks in it."
expected = [
"This",
"is",
"a",
",",
"*",
"weird",
"sentence",
"with",
"*",
"asterisks",
"in",
"it",
".",
]
assert word_tokenize(text) == expected
def test_pad_dotdot(self):
"""
Test padding of dotdot* for word tokenization.
"""
text = "Why did dotdot.. not get tokenized but dotdotdot... did? How about manydots....."
expected = [
"Why",
"did",
"dotdot",
"..",
"not",
"get",
"tokenized",
"but",
"dotdotdot",
"...",
"did",
"?",
"How",
"about",
"manydots",
".....",
]
assert word_tokenize(text) == expected
def test_remove_handle(self):
"""
Test remove_handle() from casual.py with specially crafted edge cases
"""
tokenizer = TweetTokenizer(strip_handles=True)
# Simple example. Handles with just numbers should be allowed
test1 = "@twitter hello @twi_tter_. hi @12345 @123news"
expected = ["hello", ".", "hi"]
result = tokenizer.tokenize(test1)
assert result == expected
# Handles are allowed to follow any of the following characters
test2 = "@n`@n~@n(@n)@n-@n=@n+@n\\@n|@n[@n]@n{@n}@n;@n:@n'@n\"@n/@n?@n.@n,@n<@n>@n @n\n@n ñ@n.ü@n.ç@n."
expected = [
"`",
"~",
"(",
")",
"-",
"=",
"+",
"\\",
"|",
"[",
"]",
"{",
"}",
";",
":",
"'",
'"',
"/",
"?",
".",
",",
"<",
">",
"ñ",
".",
"ü",
".",
"ç",
".",
]
result = tokenizer.tokenize(test2)
assert result == expected
# Handles are NOT allowed to follow any of the following characters
test3 = "a@n j@n z@n A@n L@n Z@n 1@n 4@n 7@n 9@n 0@n _@n !@n @@n #@n $@n %@n &@n *@n"
expected = [
"a",
"@n",
"j",
"@n",
"z",
"@n",
"A",
"@n",
"L",
"@n",
"Z",
"@n",
"1",
"@n",
"4",
"@n",
"7",
"@n",
"9",
"@n",
"0",
"@n",
"_",
"@n",
"!",
"@n",
"@",
"@n",
"#",
"@n",
"$",
"@n",
"%",
"@n",
"&",
"@n",
"*",
"@n",
]
result = tokenizer.tokenize(test3)
assert result == expected
# Handles are allowed to precede the following characters
test4 = "@n!a @n#a @n$a @n%a @n&a @n*a"
expected = ["!", "a", "#", "a", "$", "a", "%", "a", "&", "a", "*", "a"]
result = tokenizer.tokenize(test4)
assert result == expected
# Tests interactions with special symbols and multiple @
test5 = "@n!@n @n#@n @n$@n @n%@n @n&@n @n*@n @n@n @@n @n@@n @n_@n @n7@n @nj@n"
expected = [
"!",
"@n",
"#",
"@n",
"$",
"@n",
"%",
"@n",
"&",
"@n",
"*",
"@n",
"@n",
"@n",
"@",
"@n",
"@n",
"@",
"@n",
"@n_",
"@n",
"@n7",
"@n",
"@nj",
"@n",
]
result = tokenizer.tokenize(test5)
assert result == expected
# Tests that handles can have a max length of 15
test6 = "@abcdefghijklmnopqrstuvwxyz @abcdefghijklmno1234 @abcdefghijklmno_ @abcdefghijklmnoendofhandle"
expected = ["pqrstuvwxyz", "1234", "_", "endofhandle"]
result = tokenizer.tokenize(test6)
assert result == expected
# Edge case where an @ comes directly after a long handle
test7 = "@abcdefghijklmnop@abcde @abcdefghijklmno@abcde @abcdefghijklmno_@abcde @abcdefghijklmno5@abcde"
expected = [
"p",
"@abcde",
"@abcdefghijklmno",
"@abcde",
"_",
"@abcde",
"5",
"@abcde",
]
result = tokenizer.tokenize(test7)
assert result == expected
def test_treebank_span_tokenizer(self):
"""
Test TreebankWordTokenizer.span_tokenize function
"""
tokenizer = TreebankWordTokenizer()
# Test case in the docstring
test1 = "Good muffins cost $3.88\nin New (York). Please (buy) me\ntwo of them.\n(Thanks)."
expected = [
(0, 4),
(5, 12),
(13, 17),
(18, 19),
(19, 23),
(24, 26),
(27, 30),
(31, 32),
(32, 36),
(36, 37),
(37, 38),
(40, 46),
(47, 48),
(48, 51),
(51, 52),
(53, 55),
(56, 59),
(60, 62),
(63, 68),
(69, 70),
(70, 76),
(76, 77),
(77, 78),
]
result = list(tokenizer.span_tokenize(test1))
assert result == expected
# Test case with double quotation
test2 = 'The DUP is similar to the "religious right" in the United States and takes a hardline stance on social issues'
expected = [
(0, 3),
(4, 7),
(8, 10),
(11, 18),
(19, 21),
(22, 25),
(26, 27),
(27, 36),
(37, 42),
(42, 43),
(44, 46),
(47, 50),
(51, 57),
(58, 64),
(65, 68),
(69, 74),
(75, 76),
(77, 85),
(86, 92),
(93, 95),
(96, 102),
(103, 109),
]
result = list(tokenizer.span_tokenize(test2))
assert result == expected
# Test case with double qoutation as well as converted quotations
test3 = "The DUP is similar to the \"religious right\" in the United States and takes a ``hardline'' stance on social issues"
expected = [
(0, 3),
(4, 7),
(8, 10),
(11, 18),
(19, 21),
(22, 25),
(26, 27),
(27, 36),
(37, 42),
(42, 43),
(44, 46),
(47, 50),
(51, 57),
(58, 64),
(65, 68),
(69, 74),
(75, 76),
(77, 79),
(79, 87),
(87, 89),
(90, 96),
(97, 99),
(100, 106),
(107, 113),
]
result = list(tokenizer.span_tokenize(test3))
assert result == expected
def test_word_tokenize(self):
"""
Test word_tokenize function
"""
sentence = "The 'v', I've been fooled but I'll seek revenge."
expected = [
"The",
"'",
"v",
"'",
",",
"I",
"'ve",
"been",
"fooled",
"but",
"I",
"'ll",
"seek",
"revenge",
".",
]
assert word_tokenize(sentence) == expected
sentence = "'v' 're'"
expected = ["'", "v", "'", "'re", "'"]
assert word_tokenize(sentence) == expected
def test_punkt_pair_iter(self):
test_cases = [
("12", [("1", "2"), ("2", None)]),
("123", [("1", "2"), ("2", "3"), ("3", None)]),
("1234", [("1", "2"), ("2", "3"), ("3", "4"), ("4", None)]),
]
for (test_input, expected_output) in test_cases:
actual_output = [x for x in punkt._pair_iter(test_input)]
assert actual_output == expected_output
def test_punkt_pair_iter_handles_stop_iteration_exception(self):
# test input to trigger StopIteration from next()
it = iter([])
# call method under test and produce a generator
gen = punkt._pair_iter(it)
# unpack generator, ensure that no error is raised
list(gen)
def test_punkt_tokenize_words_handles_stop_iteration_exception(self):
obj = punkt.PunktBaseClass()
class TestPunktTokenizeWordsMock:
def word_tokenize(self, s):
return iter([])
obj._lang_vars = TestPunktTokenizeWordsMock()
# unpack generator, ensure that no error is raised
list(obj._tokenize_words("test"))
def test_punkt_tokenize_custom_lang_vars(self):
# Create LangVars including a full stop end character as used in Bengali
class BengaliLanguageVars(punkt.PunktLanguageVars):
sent_end_chars = (".", "?", "!", "\u0964")
obj = punkt.PunktSentenceTokenizer(lang_vars=BengaliLanguageVars())
# We now expect these sentences to be split up into the individual sentences
sentences = "উপরাষ্ট্রপতি শ্রী এম ভেঙ্কাইয়া নাইডু সোমবার আই আই টি দিল্লির হীরক জয়ন্তী উদযাপনের উদ্বোধন করেছেন। অনলাইনের মাধ্যমে এই অনুষ্ঠানে কেন্দ্রীয় মানব সম্পদ উন্নয়নমন্ত্রী শ্রী রমেশ পোখরিয়াল ‘নিশাঙ্ক’ উপস্থিত ছিলেন। এই উপলক্ষ্যে উপরাষ্ট্রপতি হীরকজয়ন্তীর লোগো এবং ২০৩০-এর জন্য প্রতিষ্ঠানের লক্ষ্য ও পরিকল্পনার নথি প্রকাশ করেছেন।"
expected = [
"উপরাষ্ট্রপতি শ্রী এম ভেঙ্কাইয়া নাইডু সোমবার আই আই টি দিল্লির হীরক জয়ন্তী উদযাপনের উদ্বোধন করেছেন।",
"অনলাইনের মাধ্যমে এই অনুষ্ঠানে কেন্দ্রীয় মানব সম্পদ উন্নয়নমন্ত্রী শ্রী রমেশ পোখরিয়াল ‘নিশাঙ্ক’ উপস্থিত ছিলেন।",
"এই উপলক্ষ্যে উপরাষ্ট্রপতি হীরকজয়ন্তীর লোগো এবং ২০৩০-এর জন্য প্রতিষ্ঠানের লক্ষ্য ও পরিকল্পনার নথি প্রকাশ করেছেন।",
]
assert obj.tokenize(sentences) == expected
def test_punkt_tokenize_no_custom_lang_vars(self):
obj = punkt.PunktSentenceTokenizer()
# We expect these sentences to not be split properly, as the Bengali full stop '।' is not included in the default language vars
sentences = "উপরাষ্ট্রপতি শ্রী এম ভেঙ্কাইয়া নাইডু সোমবার আই আই টি দিল্লির হীরক জয়ন্তী উদযাপনের উদ্বোধন করেছেন। অনলাইনের মাধ্যমে এই অনুষ্ঠানে কেন্দ্রীয় মানব সম্পদ উন্নয়নমন্ত্রী শ্রী রমেশ পোখরিয়াল ‘নিশাঙ্ক’ উপস্থিত ছিলেন। এই উপলক্ষ্যে উপরাষ্ট্রপতি হীরকজয়ন্তীর লোগো এবং ২০৩০-এর জন্য প্রতিষ্ঠানের লক্ষ্য ও পরিকল্পনার নথি প্রকাশ করেছেন।"
expected = [
"উপরাষ্ট্রপতি শ্রী এম ভেঙ্কাইয়া নাইডু সোমবার আই আই টি দিল্লির হীরক জয়ন্তী উদযাপনের উদ্বোধন করেছেন। অনলাইনের মাধ্যমে এই অনুষ্ঠানে কেন্দ্রীয় মানব সম্পদ উন্নয়নমন্ত্রী শ্রী রমেশ পোখরিয়াল ‘নিশাঙ্ক’ উপস্থিত ছিলেন। এই উপলক্ষ্যে উপরাষ্ট্রপতি হীরকজয়ন্তীর লোগো এবং ২০৩০-এর জন্য প্রতিষ্ঠানের লক্ষ্য ও পরিকল্পনার নথি প্রকাশ করেছেন।"
]
assert obj.tokenize(sentences) == expected
@pytest.mark.parametrize(
"input_text,n_sents,n_splits,lang_vars",
[
# Test debug_decisions on a text with two sentences, split by a dot.
("Subject: Some subject. Attachments: Some attachments", 2, 1),
# The sentence should be split into two sections,
# with one split and hence one decision.
# Test debug_decisions on a text with two sentences, split by an exclamation mark.
("Subject: Some subject! Attachments: Some attachments", 2, 1),
# The sentence should be split into two sections,
# with one split and hence one decision.
# Test debug_decisions on a text with one sentences,
# which is not split.
("This is just a normal sentence, just like any other.", 1, 0)
# Hence just 1
],
)
def punkt_debug_decisions(self, input_text, n_sents, n_splits, lang_vars=None):
tokenizer = punkt.PunktSentenceTokenizer()
if lang_vars != None:
tokenizer._lang_vars = lang_vars
assert len(tokenizer.tokenize(input_text)) == n_sents
assert len(list(tokenizer.debug_decisions(input_text))) == n_splits
def test_punkt_debug_decisions_custom_end(self):
# Test debug_decisions on a text with two sentences,
# split by a custom end character, based on Issue #2519
class ExtLangVars(punkt.PunktLanguageVars):
sent_end_chars = (".", "?", "!", "^")
self.punkt_debug_decisions(
"Subject: Some subject^ Attachments: Some attachments",
n_sents=2,
n_splits=1,
lang_vars=ExtLangVars(),
)
# The sentence should be split into two sections,
# with one split and hence one decision.
@pytest.mark.parametrize(
"sentences, expected",
[
(
"this is a test. . new sentence.",
["this is a test.", ".", "new sentence."],
),
("This. . . That", ["This.", ".", ".", "That"]),
("This..... That", ["This..... That"]),
("This... That", ["This... That"]),
("This.. . That", ["This.. .", "That"]),
("This. .. That", ["This.", ".. That"]),
("This. ,. That", ["This.", ",.", "That"]),
("This!!! That", ["This!!!", "That"]),
("This! That", ["This!", "That"]),
(
"1. This is R .\n2. This is A .\n3. That's all",
["1.", "This is R .", "2.", "This is A .", "3.", "That's all"],
),
(
"1. This is R .\t2. This is A .\t3. That's all",
["1.", "This is R .", "2.", "This is A .", "3.", "That's all"],
),
("Hello.\tThere", ["Hello.", "There"]),
],
)
def test_sent_tokenize(self, sentences: str, expected: List[str]):
assert sent_tokenize(sentences) == expected
|