|
6 | 6 | import json |
7 | 7 | import logging |
8 | 8 | import random |
| 9 | +import re |
9 | 10 | import socket |
10 | 11 | import subprocess |
11 | 12 | import time |
@@ -610,6 +611,41 @@ def check(self, text: str) -> List[Match]: |
610 | 611 | matches = response["matches"] |
611 | 612 | return [Match(match, text) for match in matches] |
612 | 613 |
|
| 614 | + def check_matching_regions( |
| 615 | + self, text: str, pattern: str, flags: int = 0 |
| 616 | + ) -> List[Match]: |
| 617 | + """ |
| 618 | + Check only the parts of the text that match a regex pattern. |
| 619 | + The returned Match objects can be applied to the original text with |
| 620 | + :func:`language_tool_python.utils.correct`. |
| 621 | +
|
| 622 | + :param text: The full text. |
| 623 | + :param pattern: Regular expression defining the regions to check |
| 624 | + :param flags: Regex flags (re.IGNORECASE, re.MULTILINE, etc.) |
| 625 | + :return: List of Match with offsets adjusted to the original text |
| 626 | + :rtype: List[Match] |
| 627 | + """ |
| 628 | + |
| 629 | + # Find all matching regions |
| 630 | + matches_iter = re.finditer(pattern, text, flags) |
| 631 | + regions = [(m.start(), m.group()) for m in matches_iter] |
| 632 | + |
| 633 | + if not regions: |
| 634 | + return [] # No regions to check |
| 635 | + |
| 636 | + all_matches: List[Match] = [] |
| 637 | + |
| 638 | + for start_offset, region_text in regions: |
| 639 | + region_matches = self.check(region_text) |
| 640 | + |
| 641 | + # Adjust offsets for the original text |
| 642 | + for match in region_matches: |
| 643 | + match.offset += start_offset |
| 644 | + |
| 645 | + all_matches.extend(region_matches) |
| 646 | + |
| 647 | + return sorted(all_matches, key=lambda m: m.offset) |
| 648 | + |
613 | 649 | def _create_params(self, text: str) -> Dict[str, str]: |
614 | 650 | """ |
615 | 651 | Create a dictionary of parameters for the language tool server request. |
|
0 commit comments