Advertisement
daniel10096783

German translator

Oct 17th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. import sqlite3
  2.  
  3. from googletrans import Translator
  4. translator = Translator()
  5.  
  6. import os.path
  7.  
  8.  
  9. def transfunction(word):
  10.     trans = translator.translate(word, src="en", dest="de")
  11.     return trans.text
  12.  
  13.  
  14. def translation(conn):
  15.     word_en = input("Your word?\n")
  16.     word_de = transfunction(word_en)
  17.     print("Means in german: ", word_de)
  18.  
  19.     conn.execute('''
  20.    INSERT INTO words(en, de)
  21.    VALUES (?, ?)
  22.    ''',
  23.     (word_en, word_de))
  24.     conn.commit()
  25.  
  26.  
  27. def start():
  28.     if os.path.exists("data.db") == False:
  29.         conn = sqlite3.connect("data.db")
  30.  
  31.         conn.execute('''
  32.        CREATE TABLE words (
  33.        en TEXT NOT NULL,
  34.        de TEXT NOT NULL )
  35.        ''')
  36.         conn.close()
  37.  
  38.  
  39. def showall(conn):
  40.     cur = conn.cursor()
  41.     cur.execute("SELECT * FROM words")
  42.     rows = cur.fetchall()
  43.     for row in rows:
  44.         print(row)
  45.  
  46.  
  47. def main():
  48.     start()
  49.     conn = sqlite3.connect("data.db")
  50.  
  51.  
  52.     while True:
  53.         selection = input("\n What do you want to do: \n \
  54.            1. tranlate \n \
  55.            2. show all word\n \
  56.            3. close app \n \
  57.            \n")
  58.  
  59.         if selection == "1":
  60.             translation(conn)
  61.         elif selection == "2":
  62.             showall(conn)
  63.         elif selection == "3":
  64.             break
  65.  
  66.     conn.close()
  67.  
  68.  
  69.  
  70. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement