Version 5 (Leep UpperCase, remove default text, retrieve data from dictionar.txt and dictionar-2.txt)
import tkinter as tk import re punctuation = re.compile("[:;,\.\"'”“\?!]") def get_words(text, lower=False): words = [re.sub(punctuation, "", word) for word in text.split()] if lower: return [word.lower() for word in words] return words def replace(text, old, new): # Replace old with new in text, keeping the case of old intact # Check if old starts with an uppercase letter if old[0].isupper(): new = new.capitalize() return re.sub(r'\b' + re.escape(old) + r'\b', new, text, flags=re.IGNORECASE) def adauga_diacritice(): text = text_input.get("1.0", tk.END) words = get_words(text) for word in words: replacement = plain_2_diacritic.get(word.lower(), None) if replacement is not None: text = replace(text, word, replacement) text_input.delete("1.0", tk.END) text_input.insert("1.0", text) def make_diacritic_word_dictionary(): with open("dictionar.txt", "r", encoding="utf-8") as f1, open("dictionar-2.txt", "r", encoding="utf-8") as f2: d = get_words(f1.read(), lower=True) d2 = get_words(f2.read(), lower=True) return {w2: w for w, w2 in zip(d, d2) if w != w2} plain_2_diacritic = make_diacritic_word_dictionary() root = tk.Tk() text_input = tk.Text(root, height=20, width=50) text_input.pack(pady=20) btn_diacritice = tk.Button(root, text="Diacritice", command=adauga_diacritice) btn_diacritice.pack(side=tk.LEFT, padx=10) root.mainloop()