Returns a list of Word and Chunk objects, where words have been grouped into their chunks whenever possible. Optionally, returns only chunks/words that match given constraint(s), or constraint index.
(self, constraint=None)
| 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: |
| 965 | i = self.pattern.sequence.index(constraint) |
| 966 | W = self._map2.get(i,[]) |
| 967 | W = [self.words[i-self.words[0].index] for i in W] |
| 968 | if isinstance(constraint, (list, tuple)): |
| 969 | W = []; [W.extend(self._map2.get(j<0 and j%n or j,[])) for j in constraint] |
| 970 | W = [self.words[i-self.words[0].index] for i in W] |
| 971 | W = unique(W) |
| 972 | a = [] |
| 973 | i = 0 |
| 974 | while i < len(W): |
| 975 | w = W[i] |
| 976 | if w.chunk and W[i:i+len(w.chunk)] == w.chunk.words: |
| 977 | i += len(w.chunk) - 1 |
| 978 | a.append(w.chunk) |
| 979 | else: |
| 980 | a.append(w) |
| 981 | i += 1 |
| 982 | return a |
| 983 | |
| 984 | def group(self, index, chunked=False): |
| 985 | """ Returns a list of Word objects that match the given group. |