| """Tool for fixing for common typos.""" |
| |
| from __future__ import absolute_import |
| import collections |
| import logging |
| import os |
| import re |
| |
| from helpers import typos |
| |
| |
| def fix(line): |
| """Return a list of (typo, suggestion) for typos in |line|.""" |
| suggestions = [] |
| for typo in typos.TYPOS: |
| for match in re.finditer(typo.typo_pattern, line, re.IGNORECASE): |
| original = match.group(0) |
| suggestion = re.sub( |
| typo.typo_pattern, |
| typo.replacement_pattern, |
| original, |
| count=1, |
| flags=re.IGNORECASE) |
| |
| # The typo check was triggering too many false positives when words |
| # were mixed with CamelCase. Detect that case and ignore those suggestions |
| # to reduce the false positive case. |
| if re.search(r'[a-z][A-Z]', original): |
| continue |
| |
| if original.islower(): |
| suggestion = suggestion.lower() |
| elif original.isupper(): |
| suggestion = suggestion.upper() |
| elif original.istitle(): |
| suggestion = suggestion.title() |
| |
| if original != suggestion: |
| suggestions.append((original, suggestion)) |
| |
| return suggestions |