| 530 | return isinstance(string, unicode) and hasattr(string, "tags") |
| 531 | |
| 532 | class Sentence(object): |
| 533 | |
| 534 | def __init__(self, string="", token=[WORD, POS, CHUNK, PNP, REL, ANCHOR, LEMMA], language="en"): |
| 535 | """ A nested tree of sentence words, chunks and prepositions. |
| 536 | The input is a tagged string from parse(). |
| 537 | The order in which token tags appear can be specified. |
| 538 | """ |
| 539 | # Extract token format from TokenString or TaggedString if possible. |
| 540 | if _is_tokenstring(string): |
| 541 | token, language = string.tags, getattr(string, "language", language) |
| 542 | # Convert to Unicode. |
| 543 | if isinstance(string, str): |
| 544 | for encoding in (("utf-8",), ("windows-1252",), ("utf-8", "ignore")): |
| 545 | try: string = string.decode(*encoding) |
| 546 | except: |
| 547 | pass |
| 548 | self.parent = None # A Slice refers to the Sentence it is part of. |
| 549 | self.text = None # A Sentence refers to the Text it is part of. |
| 550 | self.language = language |
| 551 | self.id = _uid() |
| 552 | self.token = list(token) |
| 553 | self.words = [] |
| 554 | self.chunks = [] # Words grouped into chunks. |
| 555 | self.pnp = [] # Words grouped into PNP chunks. |
| 556 | self._anchors = {} # Anchor tags related to anchor chunks or attached PNP's. |
| 557 | self._relation = None # Helper variable: the last chunk's relation and role. |
| 558 | self._attachment = None # Helper variable: the last attachment tag (e.g., "P1") parsed in _do_pnp(). |
| 559 | self._previous = None # Helper variable: the last token parsed in parse_token(). |
| 560 | self.relations = { "SBJ":{}, "OBJ":{}, "VP":{} } |
| 561 | # Split the slash-formatted token into the separate tags in the given order. |
| 562 | # Append Word and Chunk objects according to the token's tags. |
| 563 | for chars in string.split(" "): |
| 564 | if len(chars) > 0: |
| 565 | self.append(*self.parse_token(chars, token)) |
| 566 | |
| 567 | @property |
| 568 | def word(self): |
| 569 | return self.words |
| 570 | |
| 571 | @property |
| 572 | def lemmata(self): |
| 573 | return Map(lambda w: w.lemma, self.words) |
| 574 | #return [word.lemma for word in self.words] |
| 575 | |
| 576 | lemma = lemmata |
| 577 | |
| 578 | @property |
| 579 | def parts_of_speech(self): |
| 580 | return Map(lambda w: w.type, self.words) |
| 581 | #return [word.type for word in self.words] |
| 582 | |
| 583 | pos = parts_of_speech |
| 584 | |
| 585 | @property |
| 586 | def tagged(self): |
| 587 | return [(word.string, word.type) for word in self] |
| 588 | |
| 589 | @property |
no outgoing calls
searching dependent graphs…