MCPcopy Index your code
hub / github.com/clips/pattern / nearest

Method nearest

pattern/text/tree.py:402–415  ·  view source on GitHub ↗

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")

Source from the content-addressed store, hash-verified

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.

Callers 3

modifiersMethod · 0.80
guess_anchorMethod · 0.80
test_chunkMethod · 0.80

Calls 3

lenFunction · 0.85
absFunction · 0.50
indexMethod · 0.45

Tested by 1

test_chunkMethod · 0.64