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
#5
yes, the code should compare words from the FORM with words that do not have diacritics from the dictionary-2.txt. If a match is found, the word in the form should be replaced with the corresponding word that has diacritics from dictinary.txt.

I change the code. The Print is ok, but in the form nothing changes. Why ?


import re
import tkinter as tk
from tkinter import filedialog, messagebox, simpledialog

root = None

# Încărcarea bazei de date din fișierul .txt
with open('dictionar.txt', 'r', encoding='utf-8') as f:
    continut_dictionar = f.read().lower()

with open('dictionar-2.txt', 'r', encoding='utf-8') as f:
    continut_dictionar_fara_diacritice = f.read().lower()

diacritice = {
    'a': 'ă', 'A': 'Ă', 'i': 'î', 'I': 'Î', 's': 'ș', 'S': 'Ș', 't': 'ț', 'T': 'Ț'
}

SUFIXE = ["-mi", "-a", "-ti", "-au"]
CUVINTE_SKIP = []

def get_words(filename):
    """Read words from file.  Remove punctuation and convert to lowercase.  Return list of words."""
    punctuation = re.compile("[:'\.”“;\?!]")
    with open(filename, 'r', encoding='utf-8') as f:
        return [re.sub(punctuation, " ", word).lower() for word in f.read().split()]
def elimina_sufix(cuvant):
    for sufix in SUFIXE:
        if cuvant.endswith(sufix):
            return cuvant[:-len(sufix)]
    return cuvant

def elimina_operatori(cuvant):
    operatori = [":", '"', "'", ".", "”", "“", ",", ";", "?", "!"]
    while cuvant and cuvant[0] in operatori:
        cuvant = cuvant[1:]
    while cuvant and cuvant[-1] in operatori:
        cuvant = cuvant[:-1]
    return cuvant

def verifica_text(cuvinte_text):
    text_input.tag_remove("evidentiat", "1.0", tk.END)  # Ștergem evidențierea anterioară
    start_index = "1.0"
    cuvant_gasit = False

    for cuv in cuvinte_text:
        cuv_baza = elimina_sufix(cuv).lower()
        cuv_baza = elimina_operatori(cuv_baza)
        end_index = start_index + f"+{len(cuv)}c"
        if cuv_baza not in continut_dictionar and cuv_baza not in CUVINTE_SKIP:
            text_input.tag_add("evidentiat", start_index, end_index)
            text_input.tag_configure("evidentiat", background="yellow", font=("Arial", 12, "bold"))
            cuvant_gasit = True
            break  # Întrerupem bucla după primul cuvânt nerecunoscut
        start_index = end_index + "+1c"

    if not cuvant_gasit:
        messagebox.showinfo("Informare", "Totul este ok!")

def modifica_cuvant():
    global continut_dictionar  # Declarăm variabila ca globală pentru a o putea modifica
    start_index = text_input.tag_ranges("evidentiat")[0]
    end_index = text_input.tag_ranges("evidentiat")[1]
    cuvant = text_input.get(start_index, end_index)

    cuvant_modificat = simpledialog.askstring("Modificare cuvânt", f"Modificați cuvântul '{cuvant}':")
    if cuvant_modificat:
        cuvant_modificat = elimina_sufix(cuvant_modificat).lower()
        cuvant_modificat = elimina_operatori(cuvant_modificat)

        if cuvant_modificat:
            text_input.delete(start_index, end_index)  # Șterge cuvântul evidentiat
            text_input.insert(start_index, cuvant_modificat)  # Inserează cuvântul modificat
            cuvinte_text = text_input.get("1.0", tk.END).split()  # Actualizează lista de cuvinte
            cuvinte_text = [elimina_sufix(cuv.lower()) for cuv in cuvinte_text]
            cuvinte_text = [elimina_operatori(cuv) for cuv in cuvinte_text]
            continut_dictionar += f", {cuvant_modificat}"
            with open('dictionar.txt', 'a', encoding='utf-8') as f:
                f.write(f", {cuvant_modificat}")  # Adaugă cuvântul modificat în dictionar.txt
            verifica_text(cuvinte_text)
            print("Cuvantul modificat a fost adaugat in dictionar.txt")

def skip_cuvant():
    start_index = text_input.tag_ranges("evidentiat")[0]
    end_index = text_input.tag_ranges("evidentiat")[1]
    cuvant = text_input.get(start_index, end_index)
    CUVINTE_SKIP.append(cuvant.lower())
    with open('dictionar.txt', 'a', encoding='utf-8') as f:
        f.write(f", {cuvant}")  # Adaugă cuvântul skip în dictionar.txt
    verifica_text(text_input.get("1.0", tk.END).split())
    print("Cuvantul a fost adaugat in dictionar.txt")


def adauga_diacritice():
    text = text_input.get("1.0", tk.END)
    cuvinte_text = text.split()

    cuvinte_cu_diacritice = []
    for cuv in cuvinte_text:
        cuv_fara_diacritice = cuv
        if cuv_fara_diacritice in plain_2_diacritic:
            cuv_cu_diacritice = plain_2_diacritic[cuv_fara_diacritice]
            cuvinte_cu_diacritice.append(cuv_cu_diacritice)
        else:
            cuvinte_cu_diacritice.append(cuv_fara_diacritice)

    text_cu_diacritice = " ".join(cuvinte_cu_diacritice)

    text_input.delete("1.0", tk.END)
    text_input.insert("1.0", text_cu_diacritice)

    # Actualizează interfața grafică
    global root
    root.update()

    print("Diacritice au fost adăugate")

def main():
    global text_input
    text_input = tk.Text(root, height=20, width=50)
    text_input.pack(pady=20)

    btn_verifica = tk.Button(root, text="Verificare", command=lambda: verifica_text(text_input.get("1.0", tk.END).split()))
    btn_verifica.pack(side=tk.LEFT, padx=10)

    btn_modifica = tk.Button(root, text="Modificare", command=modifica_cuvant)
    btn_modifica.pack(side=tk.LEFT, padx=10)

    btn_skip = tk.Button(root, text="Skip", command=skip_cuvant)
    btn_skip.pack(side=tk.LEFT, padx=10)

    btn_diacritice = tk.Button(root, text="Diacritice", command=adauga_diacritice)
    btn_diacritice.pack(side=tk.LEFT, padx=10)

# Eliminăm definiția globală din main() și o mutăm aici
root = tk.Tk()
root.title("Adăugare Diacritice")

if __name__ == "__main__":
    # Create the plain to diacritic translation dictionary
    plain_2_diacritic = dict(zip(get_words("dictionar-2.txt"), get_words("dictionar.txt")))
    print("Plain to Diacritic Dictionary:")
    print(plain_2_diacritic)

    main()  # Apelăm funcția main() pentru a începe interfața grafică
    root.mainloop()  # Rulăm bucla principală a interfeței grafice
Reply


Messages In This Thread
RE: Form that puts diacritics on the words in the text - by Melcu54 - Aug-18-2023, 04:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract a string between 2 words from a text file OscarBoots 2 1,948 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 2,936 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Open and read multiple text files and match words kozaizsvemira 3 6,879 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  Counting the most relevant words in a text file caiomartins 2 2,569 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,206 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Check text contains words similar to themes/topics (thesaurus) Bec 1 39,322 Jul-28-2020, 04:17 PM
Last Post: Larz60+
  Need Help Typing Text into Tough Form [xpath / selenium] digitalmatic7 0 1,814 Jun-05-2019, 06:46 AM
Last Post: digitalmatic7
  Creating Dictionary form LOG /text file DG1234 7 5,684 Feb-13-2019, 08:08 PM
Last Post: DG1234
  Counting words in text dan789 4 2,753 Nov-11-2018, 07:37 PM
Last Post: dan789
  Compare all words in input() to all words in file Trianne 1 2,827 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