bartman081523 commited on
Commit
8881e78
·
1 Parent(s): 973aec5

translation cache for cli version

Browse files
Files changed (1) hide show
  1. cli_clock.py +89 -64
cli_clock.py CHANGED
@@ -7,6 +7,7 @@ import pytz
7
  from deep_translator import GoogleTranslator
8
  from deep_translator.exceptions import NotValidLength, RequestError
9
  from utils import process_json_files, flatten_text_with_line_breaks, build_word_index
 
10
 
11
  # Set up logging
12
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -15,77 +16,101 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
15
  TANACH_DATA = process_json_files(1, 39)
16
  WORD_INDEX = build_word_index(TANACH_DATA)
17
 
18
- # --- Translation Cache ---
19
- translation_cache = {}
20
-
21
- def translate_chapter(hebrew_chapter):
22
- """Translates a Hebrew chapter to English, caching the result."""
23
- if hebrew_chapter in translation_cache:
24
- return translation_cache[hebrew_chapter]
25
-
26
- try:
27
- translator = GoogleTranslator(source='iw', target='en')
28
- max_length = 2000 # Slightly below the limit to be safe
29
- translated_text = ""
30
-
31
- # Split the chapter into chunks smaller than the max length
32
- chunks = [hebrew_chapter[i:i + max_length] for i in range(0, len(hebrew_chapter), max_length)]
33
-
34
- for chunk in chunks:
35
- translated_text += translator.translate(chunk)
36
-
37
- translation_cache[hebrew_chapter] = translated_text.split('\n') # Store as list of lines
38
- return translation_cache[hebrew_chapter]
39
-
40
- except RequestError as e:
41
- logging.warning(f"Translation failed: Request Error - {e}")
42
- return ["Translation unavailable: Request Error"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  def display_current_verse():
45
- """Displays the verse corresponding to the current time."""
46
- while True:
47
- now = datetime.datetime.now()
48
- current_time_str = now.strftime("%H:%M:%S")
49
-
50
- word_data, _ = get_current_word_data(current_time_str)
51
- if word_data is None:
52
- logging.error("Word data not found for current time.")
53
- time.sleep(1)
54
- continue
55
-
56
- book_id = word_data["book_id"]
57
- chapter_id = word_data["chapter_id"]
58
- verse_id = word_data["verse_id"]
59
-
60
- hebrew_chapter = flatten_text_with_line_breaks(TANACH_DATA[book_id]["text"][chapter_id])
61
- english_chapter = translate_chapter('\n'.join(hebrew_chapter))
62
-
63
- print("\033c", end="") # Clear the terminal
64
- print(f"Time: {current_time_str}")
65
- print(f"{TANACH_DATA[book_id]['title']}, Chapter {chapter_id + 1}, Verse {verse_id}")
66
- print("-" * 30)
67
- print(hebrew_chapter[verse_id - 1])
68
- print(english_chapter[verse_id - 1]) # Display corresponding English line
69
- print("-" * 30)
70
- time.sleep(1)
71
 
72
  # --- Utility Functions --- (Same as before)
73
 
74
  def get_current_word_data(client_time_str):
75
- """Gets data about the current word based on the client's time."""
76
- try:
77
- client_time = datetime.datetime.strptime(client_time_str, "%H:%M:%S")
78
- total_seconds = int(client_time.strftime("%H")) * 3600 + \
79
- int(client_time.strftime("%M")) * 60 + \
80
- int(client_time.strftime("%S"))
81
 
82
- # Find the closest key in WORD_INDEX
83
- word_position = min(WORD_INDEX.keys(), key=lambda k: abs(k - total_seconds))
84
 
85
- return WORD_INDEX[word_position], word_position
86
- except Exception as e:
87
- logging.error(f"Error processing client time: {e}")
88
- return None, None
89
 
90
  if __name__ == "__main__":
91
- display_current_verse()
 
7
  from deep_translator import GoogleTranslator
8
  from deep_translator.exceptions import NotValidLength, RequestError
9
  from utils import process_json_files, flatten_text_with_line_breaks, build_word_index
10
+ import sqlite3 # Import sqlite3 for database handling
11
 
12
  # Set up logging
13
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
16
  TANACH_DATA = process_json_files(1, 39)
17
  WORD_INDEX = build_word_index(TANACH_DATA)
18
 
19
+ # --- Database Setup ---
20
+ conn = sqlite3.connect('translation_cache.db') # Create or connect to database
21
+ cursor = conn.cursor()
22
+ cursor.execute('''
23
+ CREATE TABLE IF NOT EXISTS translations (
24
+ book_id INTEGER,
25
+ chapter_id INTEGER,
26
+ english_text TEXT,
27
+ PRIMARY KEY (book_id, chapter_id)
28
+ )
29
+ ''')
30
+ conn.commit()
31
+
32
+ def translate_chapter(hebrew_chapter, book_id, chapter_id):
33
+ """Translates a Hebrew chapter to English, caching the result in the database."""
34
+
35
+ # Check if translation exists in the database
36
+ cursor.execute(
37
+ "SELECT english_text FROM translations WHERE book_id=? AND chapter_id=?",
38
+ (book_id, chapter_id)
39
+ )
40
+ result = cursor.fetchone()
41
+
42
+ if result:
43
+ return result[0].split('\n') # Retrieve from database and split into lines
44
+
45
+ try:
46
+ translator = GoogleTranslator(source='iw', target='en')
47
+ max_length = 2000 # Slightly below the limit to be safe
48
+ translated_text = ""
49
+
50
+ # Split the chapter into chunks smaller than the max length
51
+ chunks = [hebrew_chapter[i:i + max_length] for i in range(0, len(hebrew_chapter), max_length)]
52
+
53
+ for chunk in chunks:
54
+ translated_text += translator.translate(chunk)
55
+
56
+ # Store the translation in the database
57
+ cursor.execute(
58
+ "INSERT INTO translations (book_id, chapter_id, english_text) VALUES (?, ?, ?)",
59
+ (book_id, chapter_id, translated_text)
60
+ )
61
+ conn.commit()
62
+
63
+ return translated_text.split('\n') # Return as list of lines
64
+
65
+ except RequestError as e:
66
+ logging.warning(f"Translation failed: Request Error - {e}")
67
+ return ["Translation unavailable: Request Error"]
68
 
69
  def display_current_verse():
70
+ """Displays the verse corresponding to the current time."""
71
+ while True:
72
+ now = datetime.datetime.now()
73
+ current_time_str = now.strftime("%H:%M:%S")
74
+
75
+ word_data, _ = get_current_word_data(current_time_str)
76
+ if word_data is None:
77
+ logging.error("Word data not found for current time.")
78
+ time.sleep(1)
79
+ continue
80
+
81
+ book_id = word_data["book_id"]
82
+ chapter_id = word_data["chapter_id"]
83
+ verse_id = word_data["verse_id"]
84
+
85
+ hebrew_chapter = flatten_text_with_line_breaks(TANACH_DATA[book_id]["text"][chapter_id])
86
+ english_chapter = translate_chapter('\n'.join(hebrew_chapter), book_id, chapter_id)
87
+
88
+ print("\033c", end="") # Clear the terminal
89
+ print(f"Time: {current_time_str}")
90
+ print(f"{TANACH_DATA[book_id]['title']}, Chapter {chapter_id + 1}, Verse {verse_id}")
91
+ print("-" * 30)
92
+ print(hebrew_chapter[verse_id - 1])
93
+ print(english_chapter[verse_id - 1]) # Display corresponding English line
94
+ print("-" * 30)
95
+ time.sleep(1)
96
 
97
  # --- Utility Functions --- (Same as before)
98
 
99
  def get_current_word_data(client_time_str):
100
+ """Gets data about the current word based on the client's time."""
101
+ try:
102
+ client_time = datetime.datetime.strptime(client_time_str, "%H:%M:%S")
103
+ total_seconds = int(client_time.strftime("%H")) * 3600 + \
104
+ int(client_time.strftime("%M")) * 60 + \
105
+ int(client_time.strftime("%S"))
106
 
107
+ # Find the closest key in WORD_INDEX
108
+ word_position = min(WORD_INDEX.keys(), key=lambda k: abs(k - total_seconds))
109
 
110
+ return WORD_INDEX[word_position], word_position
111
+ except Exception as e:
112
+ logging.error(f"Error processing client time: {e}")
113
+ return None, None
114
 
115
  if __name__ == "__main__":
116
+ display_current_verse()