neuralworm commited on
Commit
7445a27
1 Parent(s): f16f0ac

translation fix

Browse files
Files changed (2) hide show
  1. app.py +28 -12
  2. gematria.db +2 -2
app.py CHANGED
@@ -5,7 +5,7 @@ import sqlite3
5
  import logging
6
  from util import process_json_files
7
  from gematria import calculate_gematria
8
- from deep_translator import GoogleTranslator
9
 
10
  logging.basicConfig(level=logging.INFO, format='%(message)s')
11
 
@@ -25,18 +25,22 @@ def initialize_database():
25
  book INTEGER,
26
  title TEXT,
27
  chapter INTEGER,
28
- verse INTEGER
 
29
  )
30
  ''')
31
  conn.commit()
32
  conn.close()
33
 
34
  def insert_phrase_to_db(c, gematria_sum, phrase_candidate, book_id, title, chapter_id, verse_id):
35
- logging.info(f"Inserting: {gematria_sum}, {phrase_candidate}, {book_id}, {title}, {chapter_id + 1}, {verse_id + 1}")
36
- c.execute('''
37
- INSERT INTO results (gematria_sum, words, book, title, chapter, verse)
38
- VALUES (?, ?, ?, ?, ?, ?)
39
- ''', (gematria_sum, phrase_candidate, book_id, title, chapter_id + 1, verse_id + 1))
 
 
 
40
 
41
  def populate_database(tanach_texts, max_phrase_length=1):
42
  conn = sqlite3.connect('gematria.db')
@@ -70,13 +74,20 @@ def search_gematria_in_db(gematria_sum):
70
  ''', (gematria_sum,))
71
  results = c.fetchall()
72
  conn.close()
 
73
  return results
74
 
75
  def translate_phrases(phrases):
76
  translator = GoogleTranslator(source='auto', target='en')
77
  translated_phrases = []
78
  for phrase in phrases:
79
- translated_phrases.append(translator.translate(phrase))
 
 
 
 
 
 
80
  return translated_phrases
81
 
82
  def db(tanach_texts, max_phrase_length=1):
@@ -103,13 +114,18 @@ def gematria_search_interface(phrase):
103
  return "No matching phrases found.", "\n".join(debug_output)
104
 
105
  phrases = [match[0] for match in matching_phrases]
 
106
  translations = translate_phrases(phrases)
 
107
 
108
  result = "Matching phrases:\n"
109
  for match, translation in zip(matching_phrases, translations):
 
 
 
110
  result += f"Book: {match[2]} ({match[3]})\nChapter: {match[4]}, Verse: {match[5]}\nPhrase: {match[0]}\nTranslation: {translation}\n\n"
111
 
112
- return result, "\n".join(debug_callback)
113
 
114
  def run_test():
115
  debug_output = []
@@ -131,7 +147,7 @@ def run_test():
131
  # Load the test JSON contents for 01.json
132
  test_texts_01 = process_json_files(1, 1)
133
  db(test_texts_01, max_phrase_length=2) # Populate the database with 1-word phrases
134
- search_phrase_01 = "讗转拽诇讱 砖诪注转讬"
135
  expected_gematria_01 = calculate_gematria(search_phrase_01.replace(" ", ""))
136
 
137
  matching_phrases_01 = search_gematria_in_db(expected_gematria_01)
@@ -142,8 +158,8 @@ def run_test():
142
 
143
  iface = gr.Interface(
144
  fn=gematria_search_interface,
145
- inputs=gr.inputs.Textbox(label="Enter phrase"),
146
- outputs=[gr.outputs.Textbox(label="Results"), gr.outputs.Textbox(label="Debug Output")],
147
  title="Gematria Search in Tanach",
148
  description="Search for phrases in Tanach that have the same gematria value as the entered phrase.",
149
  live=False, # Disable live update
 
5
  import logging
6
  from util import process_json_files
7
  from gematria import calculate_gematria
8
+ from deep_translator import GoogleTranslator, exceptions
9
 
10
  logging.basicConfig(level=logging.INFO, format='%(message)s')
11
 
 
25
  book INTEGER,
26
  title TEXT,
27
  chapter INTEGER,
28
+ verse INTEGER,
29
+ UNIQUE(gematria_sum, words, book, title, chapter, verse)
30
  )
31
  ''')
32
  conn.commit()
33
  conn.close()
34
 
35
  def insert_phrase_to_db(c, gematria_sum, phrase_candidate, book_id, title, chapter_id, verse_id):
36
+ try:
37
+ logging.info(f"Inserting: {gematria_sum}, {phrase_candidate}, {book_id}, {title}, {chapter_id + 1}, {verse_id + 1}")
38
+ c.execute('''
39
+ INSERT INTO results (gematria_sum, words, book, title, chapter, verse)
40
+ VALUES (?, ?, ?, ?, ?, ?)
41
+ ''', (gematria_sum, phrase_candidate, book_id, title, chapter_id + 1, verse_id + 1))
42
+ except sqlite3.IntegrityError:
43
+ logging.info(f"Entry already exists: {gematria_sum}, {phrase_candidate}, {book_id}, {title}, {chapter_id + 1}, {verse_id + 1}")
44
 
45
  def populate_database(tanach_texts, max_phrase_length=1):
46
  conn = sqlite3.connect('gematria.db')
 
74
  ''', (gematria_sum,))
75
  results = c.fetchall()
76
  conn.close()
77
+ logging.info(f"Search results: {results}")
78
  return results
79
 
80
  def translate_phrases(phrases):
81
  translator = GoogleTranslator(source='auto', target='en')
82
  translated_phrases = []
83
  for phrase in phrases:
84
+ try:
85
+ logging.info(f"Translating phrase: {phrase}")
86
+ translated_phrases.append(translator.translate(phrase))
87
+ logging.info(f"Translated phrase: {translated_phrases[-1]}")
88
+ except (exceptions.TranslationNotFound, exceptions.NotValidPayload, exceptions.ServerException, exceptions.RequestError) as e:
89
+ logging.error(f"Error translating phrase '{phrase}': {e}")
90
+ translated_phrases.append("[Translation Error]")
91
  return translated_phrases
92
 
93
  def db(tanach_texts, max_phrase_length=1):
 
114
  return "No matching phrases found.", "\n".join(debug_output)
115
 
116
  phrases = [match[0] for match in matching_phrases]
117
+ debug_callback(f"Debug: Phrases to be translated: {phrases}")
118
  translations = translate_phrases(phrases)
119
+ debug_callback(f"Debug: Translations: {translations}")
120
 
121
  result = "Matching phrases:\n"
122
  for match, translation in zip(matching_phrases, translations):
123
+ if len(match) < 5:
124
+ debug_callback(f"Error: Expected tuple of length 5, but got {len(match)}: {match}")
125
+ continue
126
  result += f"Book: {match[2]} ({match[3]})\nChapter: {match[4]}, Verse: {match[5]}\nPhrase: {match[0]}\nTranslation: {translation}\n\n"
127
 
128
+ return result, "\n".join(debug_output)
129
 
130
  def run_test():
131
  debug_output = []
 
147
  # Load the test JSON contents for 01.json
148
  test_texts_01 = process_json_files(1, 1)
149
  db(test_texts_01, max_phrase_length=2) # Populate the database with 1-word phrases
150
+ search_phrase_01 = "讗诇讜祝 讚讬砖谉"
151
  expected_gematria_01 = calculate_gematria(search_phrase_01.replace(" ", ""))
152
 
153
  matching_phrases_01 = search_gematria_in_db(expected_gematria_01)
 
158
 
159
  iface = gr.Interface(
160
  fn=gematria_search_interface,
161
+ inputs=gr.Textbox(label="Enter phrase"),
162
+ outputs=[gr.Textbox(label="Results"), gr.Textbox(label="Debug Output")],
163
  title="Gematria Search in Tanach",
164
  description="Search for phrases in Tanach that have the same gematria value as the entered phrase.",
165
  live=False, # Disable live update
gematria.db CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:830a2259ac32c26a72c0dcfc88dac57965a299afd28140f50e17e1349f58e73c
3
- size 1363968
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a74da41e964fe494ca3378faaa3be77929f1b0a680c312cee73106b96fb31055
3
+ size 2891776