| 448 | print "pattern.en.parser.entities.apply()" |
| 449 | |
| 450 | def test_parse(self): |
| 451 | # Assert parsed output with Penn Treebank II tags (slash-formatted). |
| 452 | # 1) "the black cat" is a noun phrase, "on the mat" is a prepositional noun phrase. |
| 453 | v = en.parser.parse("The black cat sat on the mat.") |
| 454 | self.assertEqual(v, |
| 455 | "The/DT/B-NP/O black/JJ/I-NP/O cat/NN/I-NP/O " + \ |
| 456 | "sat/VBD/B-VP/O " + \ |
| 457 | "on/IN/B-PP/B-PNP the/DT/B-NP/I-PNP mat/NN/I-NP/I-PNP ././O/O" |
| 458 | ) |
| 459 | # 2) "the black cat" is the subject, "a fish" is the object. |
| 460 | v = en.parser.parse("The black cat is eating a fish.", relations=True) |
| 461 | self.assertEqual(v, |
| 462 | "The/DT/B-NP/O/NP-SBJ-1 black/JJ/I-NP/O/NP-SBJ-1 cat/NN/I-NP/O/NP-SBJ-1 " + \ |
| 463 | "is/VBZ/B-VP/O/VP-1 eating/VBG/I-VP/O/VP-1 " + \ |
| 464 | "a/DT/B-NP/O/NP-OBJ-1 fish/NN/I-NP/O/NP-OBJ-1 ././O/O/O" |
| 465 | ) |
| 466 | # 3) "chasing" and "mice" lemmata are "chase" and "mouse". |
| 467 | v = en.parser.parse("The black cat is chasing mice.", lemmata=True) |
| 468 | self.assertEqual(v, |
| 469 | "The/DT/B-NP/O/the black/JJ/I-NP/O/black cat/NN/I-NP/O/cat " + \ |
| 470 | "is/VBZ/B-VP/O/be chasing/VBG/I-VP/O/chase " + \ |
| 471 | "mice/NNS/B-NP/O/mouse ././O/O/." |
| 472 | ) |
| 473 | # 4) Assert unicode. |
| 474 | self.assertTrue(isinstance(v, unicode)) |
| 475 | # 5) Assert unicode for faulty input (bytestring with unicode characters). |
| 476 | self.assertTrue(isinstance(en.parse("ø ü"), unicode)) |
| 477 | self.assertTrue(isinstance(en.parse("ø ü", tokenize=True, tags=False, chunks=False), unicode)) |
| 478 | self.assertTrue(isinstance(en.parse("ø ü", tokenize=False, tags=False, chunks=False), unicode)) |
| 479 | self.assertTrue(isinstance(en.parse("o u", encoding="ascii"), unicode)) |
| 480 | # 6) Assert optional parameters (i.e., setting all to False). |
| 481 | self.assertEqual(en.parse("ø ü.", tokenize=True, tags=False, chunks=False), u"ø ü .") |
| 482 | self.assertEqual(en.parse("ø ü.", tokenize=False, tags=False, chunks=False), u"ø ü.") |
| 483 | # 7) Assert the accuracy of the English tagger. |
| 484 | i, n = 0, 0 |
| 485 | for sentence in open(os.path.join(PATH, "corpora", "tagged-en-penntreebank.txt")).readlines(): |
| 486 | sentence = sentence.decode("utf-8").strip() |
| 487 | s1 = [w.split("/") for w in sentence.split(" ")] |
| 488 | s2 = [[w for w, pos in s1]] |
| 489 | s2 = en.parse(s2, tokenize=False) |
| 490 | s2 = [w.split("/") for w in s2.split(" ")] |
| 491 | for j in range(len(s1)): |
| 492 | if s1[j][1] == s2[j][1].split("-")[0]: |
| 493 | i += 1 |
| 494 | n += 1 |
| 495 | #print float(i) / n |
| 496 | self.assertTrue(float(i) / n > 0.945) |
| 497 | print "pattern.en.parse()" |
| 498 | |
| 499 | def test_tagged_string(self): |
| 500 | # Assert splitable TaggedString with language and tags properties. |