Yield position Spans for each tagged required phrase found in ``text``.
(text)
| 180 | |
| 181 | |
| 182 | def get_phrase_spans(text): |
| 183 | """ |
| 184 | Yield position Spans for each tagged required phrase found in ``text``. |
| 185 | """ |
| 186 | ipos = 0 |
| 187 | in_required_phrase = False |
| 188 | current_phrase_positions = [] |
| 189 | for token in required_phrase_tokenizer(text): |
| 190 | if token == REQUIRED_PHRASE_OPEN: |
| 191 | if in_required_phrase: |
| 192 | raise InvalidRuleRequiredPhrase('Invalid rule with nested required phrase {{ {{ braces', text) |
| 193 | in_required_phrase = True |
| 194 | |
| 195 | elif token == REQUIRED_PHRASE_CLOSE: |
| 196 | if in_required_phrase: |
| 197 | if current_phrase_positions: |
| 198 | yield Span(current_phrase_positions) |
| 199 | current_phrase_positions = [] |
| 200 | else: |
| 201 | raise InvalidRuleRequiredPhrase('Invalid rule with empty required phrase {{}} braces', text) |
| 202 | in_required_phrase = False |
| 203 | else: |
| 204 | raise InvalidRuleRequiredPhrase(f'Invalid rule with dangling required phrase missing closing braces', text) |
| 205 | continue |
| 206 | else: |
| 207 | if in_required_phrase: |
| 208 | current_phrase_positions.append(ipos) |
| 209 | ipos += 1 |
| 210 | |
| 211 | if current_phrase_positions or in_required_phrase: |
| 212 | raise InvalidRuleRequiredPhrase(f'Invalid rule with dangling required phrase missing final closing braces', text) |
| 213 | |
| 214 | |
| 215 |
no test coverage detected