Aug-18-2023, 07:40 PM
(This post was last modified: Aug-18-2023, 08:09 PM by deanhystad.)
It works for me. In the code below I extracted the adauga_diacritice() function and supporting players so I could focus on one thing at a time. Press the button and the test text (gibberish to test case and punctuation) is translated.
import tkinter as tk import re def get_words(text, lower=False): """Return words from file. Optionally set to lowercase.""" words = re.split(r"[:;,\.\"'”“\?! \n]+", text) if lower: return [word.lower() for word in words] return words def adauga_diacritice(): text = text_input.get("1.0", tk.END) # If word in dictionary, replace, else keep word. words = [plain_2_diacritic.get(word, word) for word in get_words(text, lower=True)] text = " ".join(words) text_input.delete("1.0", tk.END) text_input.insert("1.0", text) def make_diacritic_word_dictionary(): """Create dictionary to convert plain text words to words with diacritics.""" d = get_words( "Compatibilitatea sufletească nu este direct proporțională cu valoarea intensităţii sentimentelor.", lower=True, ) d2 = get_words( "Compatibilitatea sufleteasca nu este direct proportionala cu valoarea intensitatii sentimentelor.", lower=True, ) # Only add words containing diacritics. 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.insert( 1.0, "Sufleteasca nu este direct proportionala, cu valoarea intensitatii." ) 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()There are some obvious problems though. All punctuation gets removed from the text, and all words are lower case. More care must be taken when modifying the original text. Only words with diacritics should be modified.
import tkinter as tk import re def get_words(text, lower=False): """Return words from file. Optionally set to lowercase.""" words = re.split(r"[:;,\.\"'”“\?! \n]+", text) if lower: return [word.lower() for word in words] return words def replace(text, old, new): """In text replace old with new. Adjust case in new to match case in old.""" # Magic happens here def adauga_diacritice(): text = text_input.get("1.0", tk.END) words = get_words(text) for old in words: new= plain_2_diacritic.get(old.lower(), None) if replacement is not None: text = replace(text, old, new) text_input.delete("1.0", tk.END) text_input.insert("1.0", text) def make_diacritic_word_dictionary(): """Create dictionary to convert plain text words to words with diacritics.""" d = get_words( "Compatibilitatea sufletească nu este direct proporțională cu valoarea intensităţii sentimentelor.", lower=True, ) d2 = get_words( "Compatibilitatea sufleteasca nu este direct proportionala cu valoarea intensitatii sentimentelor.", lower=True, ) # Only add words containing diacritics. 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.insert( 1.0, "Sufleteasca nu este direct proportionala, cu valoarea intensitatii." ) 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()Oh, and I almost forgot, there was an error in get_words). Punctuation should be replaced by an empty string, not a blank.