Returns the first match found in the given sentence, or None.
(self, sentence, start=0, _v=None, _u=None)
| 754 | return a |
| 755 | |
| 756 | def match(self, sentence, start=0, _v=None, _u=None): |
| 757 | """ Returns the first match found in the given sentence, or None. |
| 758 | """ |
| 759 | if sentence.__class__.__name__ == "Text": |
| 760 | return find(lambda (m,s): m!=None, ((self.match(s, start, _v), s) for s in sentence))[0] |
| 761 | if isinstance(sentence, basestring): |
| 762 | sentence = Sentence(sentence) |
| 763 | # Variations (_v) further down the list may match words more to the front. |
| 764 | # We need to check all of them. Unmatched variations are blacklisted (_u). |
| 765 | # Pattern.search() calls Pattern.match() with a persistent blacklist (1.5x faster). |
| 766 | a = [] |
| 767 | for sequence in (_v is not None and _v or self._variations()): |
| 768 | if _u is not None and id(sequence) in _u: |
| 769 | continue |
| 770 | m = self._match(sequence, sentence, start) |
| 771 | if m is not None: |
| 772 | a.append((m.words[0].index, len(m.words), m)) |
| 773 | if m is not None and m.words[0].index == start: |
| 774 | return m |
| 775 | if m is None and _u is not None: |
| 776 | _u[id(sequence)] = False |
| 777 | # Return the leftmost-longest. |
| 778 | if len(a) > 0: |
| 779 | return sorted(a)[0][-1] |
| 780 | |
| 781 | def _variations(self): |
| 782 | v = variations(self.sequence, optional=lambda constraint: constraint.optional) |