Skip to content

Instantly share code, notes, and snippets.

@bodqhrohro
Last active May 27, 2025 14:19
Show Gist options
  • Save bodqhrohro/bf94d725ed1034ec10bd739b8ac3b6f3 to your computer and use it in GitHub Desktop.
Save bodqhrohro/bf94d725ed1034ec10bd739b8ac3b6f3 to your computer and use it in GitHub Desktop.
# GPT-generated
import re
import readline # Add this import for better text navigation
from colorama import Fore, Style, init
# Initialize colorama
init(autoreset=True)
# Define regex rules with severity levels
rules = [
{
"pattern": r"(?:ч|щ)",
"severity": "high",
"message": "Ts-ch merger"
},
{
"pattern": r"(?:ол|ор|ро|ло|ел|ле|ер|ре)",
"severity": "middle",
"message": "Secondary pleophony"
},
{
"pattern": r"(?:вс|вес)",
"severity": "middle",
"message": "*vьs"
},
{
"pattern": r"(?:е|о|ё|ь)",
"severity": "middle",
"message": "Harmonization of reduced vowels"
},
{
"pattern": r"(?:вл|мл)",
"severity": "high",
"message": "vl'→l', ml'→n"
},
{
"pattern": r"ы\b",
"severity": "middle",
"message": "-ы→-ѣ in genitive and accusative"
},
]
def highlight_matches(text, pattern, severity):
"""Highlight matches in the text based on severity."""
if severity == "high":
return re.sub(pattern, lambda m: Fore.RED + m.group(0) + Style.RESET_ALL, text)
elif severity == "middle":
return re.sub(pattern, lambda m: Fore.YELLOW + m.group(0) + Style.RESET_ALL, text)
return text
def check_text(phrase):
"""Check the phrase against the rules and allow corrections or proceeding."""
for rule in rules:
pattern = re.compile(rule["pattern"], re.IGNORECASE)
severity = rule["severity"]
message = rule["message"]
# Check for matches
matches = re.findall(pattern, phrase)
if matches:
# Highlight matches
highlighted_text = highlight_matches(phrase, pattern, severity)
print(f"Rule Violation: {message}")
print(f"Highlighted Text: {highlighted_text}")
# Ask user if they want to correct or proceed
action = input("Would you like to correct this? (y/n): ").strip().lower()
if action == 'y':
phrase = input("Please correct the text: ")
print("Final text:", phrase)
if __name__ == "__main__":
input_phrase = input("Enter a phrase to check: ")
check_text(input_phrase)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment