Returns the nearest chunk in the sentence with the given type. This can be used (for example) to find adverbs and adjectives related to verbs, as in: "the cat is ravenous" => is what? => "ravenous".
(self, type="VP")
| 400 | return self._modifiers |
| 401 | |
| 402 | def nearest(self, type="VP"): |
| 403 | """ Returns the nearest chunk in the sentence with the given type. |
| 404 | This can be used (for example) to find adverbs and adjectives related to verbs, |
| 405 | as in: "the cat is ravenous" => is what? => "ravenous". |
| 406 | """ |
| 407 | candidate, d = None, len(self.sentence.chunks) |
| 408 | if isinstance(self, PNPChunk): |
| 409 | i = self.sentence.chunks.index(self.chunks[0]) |
| 410 | else: |
| 411 | i = self.sentence.chunks.index(self) |
| 412 | for j, chunk in enumerate(self.sentence.chunks): |
| 413 | if chunk.type.startswith(type) and abs(i-j) < d: |
| 414 | candidate, d = chunk, abs(i-j) |
| 415 | return candidate |
| 416 | |
| 417 | def next(self, type=None): |
| 418 | """ Returns the next chunk in the sentence with the given type. |