Return True if the given Word is part of the constraint: - the word (or lemma) occurs in Constraint.words, OR - the word (or lemma) occurs in Constraint.taxa taxonomy tree, AND - the word and/or chunk tags match those defined in the constraint. Indivi
(self, word)
| 545 | self.words.append(v.lower()) |
| 546 | |
| 547 | def match(self, word): |
| 548 | """ Return True if the given Word is part of the constraint: |
| 549 | - the word (or lemma) occurs in Constraint.words, OR |
| 550 | - the word (or lemma) occurs in Constraint.taxa taxonomy tree, AND |
| 551 | - the word and/or chunk tags match those defined in the constraint. |
| 552 | Individual terms in Constraint.words or the taxonomy can contain wildcards (*). |
| 553 | Some part-of-speech-tags can also contain wildcards: NN*, VB*, JJ*, RB* |
| 554 | If the given word contains spaces (e.g., proper noun), |
| 555 | the entire chunk will also be compared. |
| 556 | For example: Constraint(words=["Mac OS X*"]) |
| 557 | matches the word "Mac" if the word occurs in a Chunk("Mac OS X 10.5"). |
| 558 | """ |
| 559 | # If the constraint can only match the first word, Word.index must be 0. |
| 560 | if self.first and word.index > 0: |
| 561 | return False |
| 562 | # If the constraint defines excluded options, Word can not match any of these. |
| 563 | if self.exclude and self.exclude.match(word): |
| 564 | return False |
| 565 | # If the constraint defines allowed tags, Word.tag needs to match one of these. |
| 566 | if self.tags: |
| 567 | if find(lambda w: _match(word.tag, w), self.tags) is None: |
| 568 | return False |
| 569 | # If the constraint defines allowed chunks, Word.chunk.tag needs to match one of these. |
| 570 | if self.chunks: |
| 571 | ch = word.chunk and word.chunk.tag or None |
| 572 | if find(lambda w: _match(ch, w), self.chunks) is None: |
| 573 | return False |
| 574 | # If the constraint defines allowed role, Word.chunk.tag needs to match one of these. |
| 575 | if self.roles: |
| 576 | R = word.chunk and [r2 for r1, r2 in word.chunk.relations] or [] |
| 577 | if find(lambda w: w in R, self.roles) is None: |
| 578 | return False |
| 579 | # If the constraint defines allowed words, |
| 580 | # Word.string.lower() OR Word.lemma needs to match one of these. |
| 581 | b = True # b==True when word in constraint (or Constraints.words=[]). |
| 582 | if len(self.words) + len(self.taxa) > 0: |
| 583 | s1 = word.string.lower() |
| 584 | s2 = word.lemma |
| 585 | b = False |
| 586 | for w in itertools.chain(self.words, self.taxa): |
| 587 | # If the constraint has a word with spaces (e.g., a proper noun), |
| 588 | # compare it to the entire chunk. |
| 589 | try: |
| 590 | if " " in w and (s1 in w or s2 and s2 in w or "*" in w): |
| 591 | s1 = word.chunk and word.chunk.string.lower() or s1 |
| 592 | s2 = word.chunk and " ".join([x or "" for x in word.chunk.lemmata]) or s2 |
| 593 | except: |
| 594 | s1 = s1 |
| 595 | s2 = None |
| 596 | # Compare the word to the allowed words (which can contain wildcards). |
| 597 | if _match(s1, w): |
| 598 | b=True; break |
| 599 | # Compare the word lemma to the allowed words, e.g., |
| 600 | # if "was" is not in the constraint, perhaps "be" is, which is a good match. |
| 601 | if s2 and _match(s2, w): |
| 602 | b=True; break |
| 603 | # If the constraint defines allowed taxonomy terms, |
| 604 | # and the given word did not match an allowed word, traverse the taxonomy. |