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
#7
thanks, deanhystad. You are really great !

I will save here another version, just a little bit different.

import tkinter as tk
import re

def adauga_diacritice():
    text = text_input.get("1.0", tk.END)
    words = []

    for word in get_words(text):
        if word.lower() in plain_2_diacritic:
            diacritic_word = plain_2_diacritic[word.lower()]
            if word.istitle():  # Preserve title case
                words.append(diacritic_word.capitalize())
            elif word.isupper():  # Preserve uppercase
                words.append(diacritic_word.upper())
            else:
                words.append(diacritic_word)
        else:
            words.append(word)

    text = " ".join(words)
    text_input.delete("1.0", tk.END)
    text_input.insert("1.0", text)

def make_diacritic_word_dictionary():
    d = get_words(
        "Compatibilitatea sufletească nu este direct proporțională cu valoarea intensităţii sentimentelor."
    )
    d2 = get_words(
        "Compatibilitatea sufleteasca nu este direct proportionala cu valoarea intensitatii sentimentelor."
    )
    return {w2: w for w, w2 in zip(d, d2) if w != w2}

def get_words(text):
    return re.findall(r'\b\w+\b', text)

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()
OR THIS:


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):
    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():
    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,
    )
    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()
Reply


Messages In This Thread
RE: Form that puts diacritics on the words in the text - by Melcu54 - Aug-18-2023, 07:50 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