Python Forum
Form that puts diacritics on the words in the text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form that puts diacritics on the words in the text
#6
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.
Reply


Messages In This Thread
RE: Form that puts diacritics on the words in the text - by deanhystad - Aug-18-2023, 07:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract a string between 2 words from a text file OscarBoots 2 1,986 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 3,001 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Open and read multiple text files and match words kozaizsvemira 3 6,946 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  Counting the most relevant words in a text file caiomartins 2 2,617 Sep-21-2020, 08:39 AM
Last Post: caiomartins
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,250 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Check text contains words similar to themes/topics (thesaurus) Bec 1 42,916 Jul-28-2020, 04:17 PM
Last Post: Larz60+
  Need Help Typing Text into Tough Form [xpath / selenium] digitalmatic7 0 1,836 Jun-05-2019, 06:46 AM
Last Post: digitalmatic7
  Creating Dictionary form LOG /text file DG1234 7 5,770 Feb-13-2019, 08:08 PM
Last Post: DG1234
  Counting words in text dan789 4 2,794 Nov-11-2018, 07:37 PM
Last Post: dan789
  Compare all words in input() to all words in file Trianne 1 2,869 Oct-05-2018, 06:27 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020