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

Class Match

pattern/text/search.py:907–1005  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

905#--- PATTERN MATCH ---------------------------------------------------------------------------------
906
907class Match(object):
908
909 def __init__(self, pattern, words=[], map={}):
910 """ Search result returned from Pattern.match(sentence),
911 containing a sequence of Word objects.
912 """
913 self.pattern = pattern
914 self.words = words
915 self._map1 = dict() # Word index to Constraint.
916 self._map2 = dict() # Constraint index to list of Word indices.
917 for w in self.words:
918 self._map1[w.index] = map[w.index]
919 for k,v in self._map1.items():
920 self._map2.setdefault(self.pattern.sequence.index(v),[]).append(k)
921 for k,v in self._map2.items():
922 v.sort()
923
924 def __len__(self):
925 return len(self.words)
926 def __iter__(self):
927 return iter(self.words)
928 def __getitem__(self, i):
929 return self.words.__getitem__(i)
930
931 @property
932 def start(self):
933 return self.words and self.words[0].index or None
934 @property
935 def stop(self):
936 return self.words and self.words[-1].index+1 or None
937
938 def constraint(self, word):
939 """ Returns the constraint that matches the given Word, or None.
940 """
941 if word.index in self._map1:
942 return self._map1[word.index]
943
944 def constraints(self, chunk):
945 """ Returns a list of constraints that match the given Chunk.
946 """
947 a = [self._map1[w.index] for w in chunk.words if w.index in self._map1]
948 b = []; [b.append(constraint) for constraint in a if constraint not in b]
949 return b
950
951 def constituents(self, constraint=None):
952 """ Returns a list of Word and Chunk objects,
953 where words have been grouped into their chunks whenever possible.
954 Optionally, returns only chunks/words that match given constraint(s), or constraint index.
955 """
956 # Select only words that match the given constraint.
957 # Note: this will only work with constraints from Match.pattern.sequence.
958 W = self.words
959 n = len(self.pattern.sequence)
960 if isinstance(constraint, (int, Constraint)):
961 if isinstance(constraint, int):
962 i = constraint
963 i = i<0 and i%n or i
964 else:

Callers 1

_matchMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…