-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.py
More file actions
83 lines (72 loc) · 2.42 KB
/
translator.py
File metadata and controls
83 lines (72 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# translator.py
import sqlite3
import customtkinter as ctk
import threading
# ---------- SQLite History Setup ----------
DB_FILE = "history.db"
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
source_text TEXT,
translated_text TEXT
)
""")
conn.commit()
# ---------- Offline Simple Translator ----------
# دیکشنری محدود (میتوانی گسترش دهی)
simple_dict = {
"hello": "سلام",
"how are you": "چطوری؟",
"good morning": "صبح بخیر",
"good night": "شب بخیر",
"thank you": "متشکرم",
"bye": "خداحافظ",
"yes": "بله",
"no": "نه",
"i love you": "دوستت دارم"
}
def translate_text(text):
text_lower = text.lower().strip()
return simple_dict.get(text_lower, "[ترجمه موجود نیست]")
# ---------- CLI Function ----------
def translate_cli():
while True:
text = input("Enter text (or 'exit'): ")
if text.lower() == "exit":
break
translated = translate_text(text)
print("Translated:", translated)
cursor.execute("INSERT INTO history (source_text, translated_text) VALUES (?, ?)", (text, translated))
conn.commit()
# ---------- GUI Function ----------
def launch_gui():
app = ctk.CTk()
app.title("Offline Translator")
app.geometry("500x350")
input_text = ctk.CTkTextbox(app, height=100)
input_text.pack(pady=10, padx=10, fill="x")
output_text = ctk.CTkTextbox(app, height=100)
output_text.pack(pady=10, padx=10, fill="x")
def do_translate():
text = input_text.get("1.0", "end-1c")
def run_translation():
translated = translate_text(text)
output_text.delete("1.0", "end")
output_text.insert("1.0", translated)
cursor.execute("INSERT INTO history (source_text, translated_text) VALUES (?, ?)", (text, translated))
conn.commit()
threading.Thread(target=run_translation, daemon=True).start()
translate_btn = ctk.CTkButton(app, text="Translate", command=do_translate)
translate_btn.pack(pady=5)
app.mainloop()
# ---------- Main ----------
if __name__ == "__main__":
choice = input("Choose mode: 1-CLI 2-GUI : ")
if choice == "1":
translate_cli()
elif choice == "2":
launch_gui()
else:
print("Invalid choice")