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
#8
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()
Reply


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