| 494 | PTB = PENN = "penn" |
| 495 | |
| 496 | class Parser(object): |
| 497 | |
| 498 | def __init__(self, lexicon={}, default=("NN", "NNP", "CD"), language=None): |
| 499 | """ A simple shallow parser using a Brill-based part-of-speech tagger. |
| 500 | The given lexicon is a dictionary of known words and their part-of-speech tag. |
| 501 | The given default tags are used for unknown words. |
| 502 | Unknown words that start with a capital letter are tagged NNP (except for German). |
| 503 | Unknown words that contain only digits and punctuation are tagged CD. |
| 504 | The given language can be used to discern between |
| 505 | Germanic and Romance languages for phrase chunking. |
| 506 | """ |
| 507 | self.lexicon = lexicon |
| 508 | self.default = default |
| 509 | self.language = language |
| 510 | |
| 511 | def find_tokens(self, string, **kwargs): |
| 512 | """ Returns a list of sentences from the given string. |
| 513 | Punctuation marks are separated from each word by a space. |
| 514 | """ |
| 515 | # "The cat purs." => ["The cat purs ."] |
| 516 | return find_tokens(string, |
| 517 | punctuation = kwargs.get( "punctuation", PUNCTUATION), |
| 518 | abbreviations = kwargs.get("abbreviations", ABBREVIATIONS), |
| 519 | replace = kwargs.get( "replace", replacements), |
| 520 | linebreak = r"\n{2,}") |
| 521 | |
| 522 | def find_tags(self, tokens, **kwargs): |
| 523 | """ Annotates the given list of tokens with part-of-speech tags. |
| 524 | Returns a list of tokens, where each token is now a [word, tag]-list. |
| 525 | """ |
| 526 | # ["The", "cat", "purs"] => [["The", "DT"], ["cat", "NN"], ["purs", "VB"]] |
| 527 | return find_tags(tokens, |
| 528 | language = kwargs.get("language", self.language), |
| 529 | lexicon = kwargs.get( "lexicon", self.lexicon), |
| 530 | default = kwargs.get( "default", self.default), |
| 531 | map = kwargs.get( "map", None)) |
| 532 | |
| 533 | def find_chunks(self, tokens, **kwargs): |
| 534 | """ Annotates the given list of tokens with chunk tags. |
| 535 | Several tags can be added, for example chunk + preposition tags. |
| 536 | """ |
| 537 | # [["The", "DT"], ["cat", "NN"], ["purs", "VB"]] => |
| 538 | # [["The", "DT", "B-NP"], ["cat", "NN", "I-NP"], ["purs", "VB", "B-VP"]] |
| 539 | return find_prepositions( |
| 540 | find_chunks(tokens, |
| 541 | language = kwargs.get("language", self.language))) |
| 542 | |
| 543 | def find_prepositions(self, tokens, **kwargs): |
| 544 | """ Annotates the given list of tokens with prepositional noun phrase tags. |
| 545 | """ |
| 546 | return find_prepositions(tokens) # See also Parser.find_chunks(). |
| 547 | |
| 548 | def find_labels(self, tokens, **kwargs): |
| 549 | """ Annotates the given list of tokens with verb/predicate tags. |
| 550 | """ |
| 551 | return find_relations(tokens) |
| 552 | |
| 553 | def find_lemmata(self, tokens, **kwargs): |
no outgoing calls
no test coverage detected
searching dependent graphs…