///
# CRITICALLY IMPORTANT RULES
1. Completeness: Generate full code, no placeholders. If unable, explain in comments.
2. Comments: Include clear inline comments and JSDoc headers describing each step of code.
3. Error Checking: Implement error checking and type validation.
4. Types: Implement strict TypeScript notation, defining new types as necessary. Additionally:
   - Do not use the 'any' type.
   - Do not use the non-null assertion operator (!).
   - Do not cast to unknown (e.g. as unknown as T).
5. Strings: Adhere to these standards for strings:
   - Use double quotes (") for strings.
   - Use string templates or .join() instead of operational concatenation.
It is critically important that you adhere to the above five rules. ///







Your aim is to develop a robust and feature-rich text editor tailored for poets and lyricists working in both English and Russian. This editor should highlight vowels and similar word combinations with consistent and unique colors, display vowel counts at the end of each line, and offer a user-friendly interface with customizable settings.

Below is a comprehensive, step-by-step plan to rewrite and enhance the text editor, ensuring it meets all your specified requirements and provides a smooth user experience.

I. Project Overview
Objective:
Create a Python-based text editor using Tkinter that:

Highlights vowels (English and Russian) with consistent colors.
Highlights similar word combinations (e.g., common substrings) with unique colors.
Displays vowel counts at the end of each line.
Offers user controls for customization, such as toggling highlights and changing background colors.
Ensures a dark-themed user interface for better readability.
Key Features:
Vowel Highlighting: Both English and Russian vowels are highlighted consistently throughout the text.
Word Combination Highlighting: Similar substrings within words are identified and highlighted with unique colors.
Vowel Counts: Each line displays the count of vowels present.
User Controls: Buttons to highlight text, clear highlights, toggle highlighting features, and change background colors.
Robust Tag Management: Efficient handling of Tkinter tags to avoid errors and ensure performance.
Error Handling: Gracefully manages potential errors, especially related to tag configurations.
Responsive UI: Ensures the editor remains responsive even with large texts.
II. Detailed Step-by-Step Plan
1. Set Up the Project Environment
Python Installation: Ensure Python 3.x is installed on your machine.
Tkinter Verification: Tkinter typically comes bundled with Python. Verify by running:

python -c "import tkinter"
If no errors appear, Tkinter is installed.
Dependencies: The project primarily relies on Tkinter and Python's standard libraries (re, collections, etc.), so no additional installations are required.
2. Define Constants and Configuration
Vowels Definition:
English Vowels: AEIOUYaeiouy
Russian Vowels: АЕЁИОУЫЭЮЯаеёиоуыэюя
Combined Vowels: Concatenate both for unified processing.
Color Palettes:
Vowel Colors: Utilize a light color palette to distinguish vowels without overshadowing the main text.
Word Combination Colors: Use a more vibrant palette to make similar parts stand out distinctly.
Ensure Enough Colors: Both palettes should have sufficient colors to handle multiple unique patterns without repetition.
3. Design the User Interface (UI) Layout
Main Components:
Text Area: Central area for text input and editing.
Control Panel: Contains buttons and checkboxes for user interactions.
Counters Display: Shows vowel counts at the end of each line.
UI Structure:
Tkinter Frames: Use frames to organize the layout logically.
ScrolledText Widget: Utilize ScrolledText for the main text area to provide scrolling functionality.
Buttons and Checkboxes: Place control elements in a dedicated frame below or beside the text area.
Background Theme: Apply a dark background to the text area and control panels for reduced eye strain.
User Controls:
Highlight Button: Triggers the highlighting of vowels and word combinations.
Clear Button: Removes all highlights and counts.
Checkboxes: Allow users to toggle the highlighting of vowels and word combinations independently.
Background Color Selector: Dropdown menu to let users choose different background colors.
4. Implement the Text Area with Scrollbars
Widget Selection: Use ScrolledText.ScrolledText for the text area to inherently provide vertical scrolling.
Configuration:
Fonts: Choose a monospaced font like "Consolas" for better alignment and readability.
Background and Foreground Colors: Set the text area's background to a dark color (e.g., #1e1e1e) and text color to a light shade (e.g., #d4d4d4).
Insert Cursor: Configure the cursor's color to be prominent (e.g., white).
5. Manage Tag Configurations for Highlighting
Understanding Tags:
Tkinter Tags: Used to apply styles (like foreground color) to specific portions of text.
Dynamic Tag Creation: Tags must be created dynamically based on the patterns detected (vowels and word combinations).
Tag Naming Strategy:
Unique Identifiers: Use a combination of a prefix and a hashed representation of the pattern to create unique and safe tag names.
Avoid Special Characters: This prevents issues with tags containing non-ASCII or special characters.
Example Tag Name Generation:

import hashlib

def generate_tag_name(prefix, content):
    content_hash = hashlib.md5(content.encode('utf-8')).hexdigest()
    return f"{prefix}_{content_hash}"
Tag Configuration:
Vowels:
Assign a unique color from the vowel color palette.
Tags are named using the vowel character and its hash.
Word Combinations:
Assign a unique color from the word combination color palette.
Tags are named using the substring and its hash.
Error Handling:
Tag Existence Check: Before retrieving or configuring a tag, ensure it exists to prevent TclError.
Graceful Failures: Wrap tag operations in try-except blocks or perform existence checks.
6. Implement Vowel Highlighting
Regex Pattern:
Use a regex pattern that matches any vowel (both English and Russian).
Example:

VOWEL_PATTERN = '[' + re.escape(ALL_VOWELS) + ']+'
Highlighting Process:
Retrieve Text: Get the entire text from the text area.
Find Matches: Use re.finditer with the vowel pattern to locate all vowels and their sequences.
Assign Colors:
For each unique vowel sequence, assign a color from the vowel color palette.
Use the generate_tag_name function to create a unique tag.
Apply Tags: Use tag_add to apply the tag to the matched text range.
7. Implement Word Combination Highlighting
Objective: Identify and highlight similar substrings within words to recognize patterns like rhymes or common prefixes/suffixes.
Approach:
Word Extraction: Use regex to extract all words from the text.

words = re.findall(r'\b\w+\b', text, re.UNICODE)
Identify Common Substrings:
Compare words pairwise to find common substrings of a minimum length (e.g., 2 characters).
Store found substrings in a collection ensuring uniqueness and relevance.
Filter Overlapping Substrings:
Prioritize longer substrings to maximize pattern recognition.
Avoid overlapping highlights to maintain clarity.
Assign Colors:
Assign a unique color from the word combination color palette to each unique substring.
Use the generate_tag_name function to create unique and safe tag names.
Apply Tags: Use tag_add to apply the tag to the matched substring ranges.
Optimization:
Avoid Redundancy: Ensure that the same substring isn't processed multiple times.
Performance: Implement efficient algorithms to handle large texts without significant delays.
8. Display Vowel Counts per Line
Objective: Count the number of vowels in each line and display the count at the end.
Implementation Steps:
Retrieve Text: Get the entire text and split it into lines.
Count Vowels: For each line, count the number of vowels using a generator expression.

count = sum(1 for char in line if char in ALL_VOWELS)
Display Counts:
Insert the count at the end of each line in a specific format (e.g., [5]).
Apply a distinct tag to the count text to style it differently (e.g., darker gray color).
Manage Counts:
Ensure counts are updated dynamically as the text changes.
Prevent duplicate counts by removing previous count tags before updating.
Edge Cases:
Empty Lines: Handle lines with no content gracefully.
Line Endings: Ensure counts are correctly inserted without disrupting the text structure.
9. Implement Control Panel with User Interaction Elements
Features:
Highlight Button: Initiates the highlighting process based on selected options.
Clear Button: Removes all highlights and counts, resetting the text area.
Checkboxes:
Highlight Vowels: Toggle vowel highlighting on or off.
Highlight Similar Parts: Toggle word combination highlighting on or off.
Background Color Selector:
Dropdown menu allowing users to choose different background colors for the text area.
Layout:
Place the control panel below or beside the text area for easy access.
Use padding and spacing to ensure buttons and checkboxes are neatly arranged.
Styling:
Buttons: Use distinct colors to indicate actions (e.g., green for highlight, red for clear).
Checkboxes: Clearly label to indicate their functionality.
10. Bind Events to Enable Dynamic Highlighting and Counting
Text Modification Event:
Bind the <<Modified>> event of the ScrolledText widget to a callback function that triggers highlighting and counting.
Example:

self.text_area.bind('<<Modified>>', self.on_text_modified)
Callback Function (on_text_modified):
Reset Modified Flag: Prevent recursive calls by resetting the modified flag.

self.text_area.edit_modified(False)
Trigger Highlighting:
Check the state of the checkboxes to determine which highlighting features to apply.
Call the respective highlighting functions (highlight_vowels, highlight_similar_parts).
Update Vowel Counts:
After highlighting, update the vowel counts per line.
Performance Considerations:
Debouncing: Implement debouncing to prevent excessive processing during rapid text changes.
Efficient Algorithms: Use optimized regex patterns and substring matching algorithms to maintain responsiveness.
11. Implement Robust Tag Management
Tag Storage:
Maintain dictionaries to map patterns (vowels and substrings) to their assigned colors.
Example:

self.vowel_colors = {}
self.similar_part_tags = set()
Tag Clearing:
Before applying new highlights, remove existing tags to prevent overlapping or outdated highlights.
Example:

self.text_area.tag_remove(tag, "1.0", tk.END)
Avoid Tag Duplication:
Use unique and consistent tag names to ensure the same patterns receive the same tags and colors.
12. Ensure Error Handling and Stability
Potential Errors:
Undefined Tags: Accessing or modifying undefined tags can raise TclError.
Invalid Regex Patterns: Malformed patterns can cause regex operations to fail.
Solutions:
Tag Existence Checks: Before configuring or accessing a tag, verify its existence.
Try-Except Blocks: Wrap tag operations in try-except blocks to catch and handle exceptions gracefully.
Input Validation: Ensure that user inputs (if any) for patterns or colors are valid.
Example Error Handling:

if tag_name not in self.text_area.tag_names():
    self.text_area.tag_configure(tag_name, foreground=assigned_color)
13. Optimize Performance for Large Texts
Efficient Processing:
Limit the scope of regex searches to recently modified parts of the text.
Use compiled regex patterns to speed up repeated searches.
Resource Management:
Avoid excessive creation of tags by reusing existing ones where possible.
Implement lazy loading or incremental processing for very large documents.
User Feedback:
Provide visual indicators (like a loading spinner) if processing is time-consuming.
Allow users to pause or cancel intensive operations.
14. Testing and Validation
Unit Testing:
Test individual functions like highlight_vowels and highlight_similar_parts with various input scenarios.
Integration Testing:
Ensure that components like event bindings, tag management, and UI elements work seamlessly together.
User Testing:
Test the editor with actual poetry and lyrics to validate the highlighting and counting functionalities.
Gather feedback on usability and adjust the UI accordingly.
Edge Cases:
Test with empty lines, lines with no vowels, lines with repetitive vowels, and lines with complex word combinations.
Test with mixed-language texts (English and Russian) to ensure accurate highlighting.
15. Documentation and User Guidance
Code Documentation:
Comment key sections of the code to explain functionality and logic.
Provide docstrings for classes and functions.
User Instructions:
Create a help menu or tooltip explanations for buttons and checkboxes.
Include guidance on how to use the editor's features effectively.
16. Future Enhancements (Optional)
File Operations:
Implement functionality to open and save text files.
Custom Pattern Highlighting:
Allow users to define and highlight their own regex patterns.
Theme Customization:
Provide multiple color themes beyond the default dark background.
Exporting Highlights:
Enable exporting the highlighted text with preserved formatting to formats like HTML or PDF.
Real-Time Collaboration:
Explore integrating real-time collaboration features for multiple users to edit simultaneously.
III. Implementation Guidelines
To facilitate the development process, here's a breakdown of how to implement some of the key components based on the above plan.