Spaces:
Runtime error
Runtime error
nos
commited on
Update lib/torah.py
Browse files- lib/torah.py +197 -3
lib/torah.py
CHANGED
|
@@ -1,7 +1,201 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import os
|
| 3 |
-
import re
|
| 4 |
from deep_translator import GoogleTranslator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def process_json_files(start, end, step, length=0, tlang="en", spaces_include=False, strip_in_braces=True, strip_diacritics=True):
|
| 7 |
base_path = "resources/texts"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from deep_translator import GoogleTranslator
|
| 2 |
+
import torahcodes.resources.func.utils as util
|
| 3 |
+
from hebrew_numbers import gematria_to_int
|
| 4 |
+
from textblob import TextBlob
|
| 5 |
+
from os import listdir
|
| 6 |
+
from os.path import isfile, join
|
| 7 |
+
import re
|
| 8 |
+
import time
|
| 9 |
+
import random
|
| 10 |
+
import os
|
| 11 |
+
import json
|
| 12 |
+
|
| 13 |
+
BLUE, RED, WHITE, YELLOW, MAGENTA, GREEN, END = '\33[1;94m', '\033[1;91m', '\33[1;97m', '\33[1;93m', '\033[1;35m', '\033[1;32m', '\033[0m'
|
| 14 |
+
ORANGE = '\033[1;33m' # orange
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
data_dir = "resources/texts"
|
| 18 |
+
|
| 19 |
+
class BibleBooks():
|
| 20 |
+
def __init__(self):
|
| 21 |
+
self.folder = data_dir
|
| 22 |
+
self.book = {}
|
| 23 |
+
def load(self):
|
| 24 |
+
|
| 25 |
+
for f in listdir(self.folder):
|
| 26 |
+
if isfile(join(self.folder, f)) and f.endswith(".json"):
|
| 27 |
+
fn = f.split('.')
|
| 28 |
+
#print('Load', fn[0])
|
| 29 |
+
with open(self.folder+f, encoding="utf-8-sig") as File:
|
| 30 |
+
self.book[fn[0]] = File.read()
|
| 31 |
+
|
| 32 |
+
def rawdata(self, bookname):
|
| 33 |
+
return self.book[bookname]
|
| 34 |
+
|
| 35 |
+
def booklist(self):
|
| 36 |
+
return list(self.book.keys())
|
| 37 |
+
|
| 38 |
+
books = BibleBooks()
|
| 39 |
+
|
| 40 |
+
class Torah():
|
| 41 |
+
def __init__(self):
|
| 42 |
+
self.book = ''
|
| 43 |
+
self.gcode = {
|
| 44 |
+
'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 600,
|
| 45 |
+
'k': 10, 'l': 20, 'm': 30, 'n': 40, 'o': 50, 'p': 60, 'q': 70, 'r': 80, 's': 90,
|
| 46 |
+
't': 100, 'u': 200, 'v': 700, 'w': 900, 'x': 300, 'y': 400, 'z': 500
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
def loadbooks(self):
|
| 50 |
+
books.load()
|
| 51 |
+
|
| 52 |
+
def func_getnumber(self, listL, listW):
|
| 53 |
+
return util.fn_GetNumberValues(listL, listW)
|
| 54 |
+
|
| 55 |
+
def func_checklang(self, word, lang):
|
| 56 |
+
b = TextBlob(word)
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
b.detect_language()
|
| 60 |
+
if (b.detect_language() == lang):
|
| 61 |
+
return True
|
| 62 |
+
except:
|
| 63 |
+
return True
|
| 64 |
+
return False
|
| 65 |
+
|
| 66 |
+
def numtobook(self, number):
|
| 67 |
+
for x in books.booklist():
|
| 68 |
+
xt = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", x)
|
| 69 |
+
if xt[0] == str(number):
|
| 70 |
+
return x
|
| 71 |
+
|
| 72 |
+
def func_translate(self, lang_in, lang_out, data):
|
| 73 |
+
translated = GoogleTranslator(source=lang_in, target=lang_out).translate(data.strip())
|
| 74 |
+
return translated
|
| 75 |
+
|
| 76 |
+
def gematria(self, word: str) -> int:
|
| 77 |
+
try:
|
| 78 |
+
if word.isdigit():
|
| 79 |
+
return int(word)
|
| 80 |
+
|
| 81 |
+
# Aufteilen des Wortes in Buchstaben und Zahlen
|
| 82 |
+
letters = [char for char in word if char.isalpha()]
|
| 83 |
+
numbers = [int(char) for char in word if char.isdigit()]
|
| 84 |
+
|
| 85 |
+
# Berechnen des Gematria-Werts für die Buchstaben
|
| 86 |
+
letters_value = sum([self.gcode[char] for char in letters if char in self.gcode])
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# Hinzufügen der Summe der Zahlen zum Gematria-Wert der Buchstaben
|
| 90 |
+
total_value = letters_value + sum(numbers)
|
| 91 |
+
|
| 92 |
+
return total_value
|
| 93 |
+
except:
|
| 94 |
+
print(word)
|
| 95 |
+
raise ValueError
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def gematrix(self, phrase: str) -> int:
|
| 99 |
+
phrase = self.strip_accents(phrase.lower())
|
| 100 |
+
phrase = ''.join([i for i in phrase if i.isalpha() or i.isdigit() or i.isspace()])
|
| 101 |
+
|
| 102 |
+
# Aufteilen der Eingabe in separate Wörter und Zahlen
|
| 103 |
+
elements = phrase.split()
|
| 104 |
+
total_value = 0
|
| 105 |
+
|
| 106 |
+
for element in elements:
|
| 107 |
+
if element.isalpha():
|
| 108 |
+
# Berechne den Wert für Buchstaben
|
| 109 |
+
total_value += sum([self.gcode[char] for char in element if char in self.gcode])
|
| 110 |
+
elif element.isdigit():
|
| 111 |
+
# Addiere Zahlen direkt zum Gesamtwert
|
| 112 |
+
total_value += int(element)
|
| 113 |
+
|
| 114 |
+
return total_value
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def strip_accents(self, s):
|
| 122 |
+
try:
|
| 123 |
+
return ''.join(
|
| 124 |
+
c for c in unicodedata.normalize('NFD', s)
|
| 125 |
+
if unicodedata.category(c) != 'Mn'
|
| 126 |
+
)
|
| 127 |
+
except:
|
| 128 |
+
return s
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def gematria_iw_int(text):
|
| 132 |
+
return gematria_to_int(text)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def func_ParseTranslation(self, translated, lang, active):
|
| 136 |
+
abd = 'abcdefghijklmnñopqrstuvwxyz1234567890'
|
| 137 |
+
str_split = translated.split(' ')
|
| 138 |
+
str_final = ''
|
| 139 |
+
for word in str_split:
|
| 140 |
+
try:
|
| 141 |
+
if word[0].lower() in abd:
|
| 142 |
+
if active == 'true':
|
| 143 |
+
if self.func_checklang(word, lang) == True:
|
| 144 |
+
str_final = str_final+ word+' '
|
| 145 |
+
else:
|
| 146 |
+
str_final = str_final+ word+' '
|
| 147 |
+
except:
|
| 148 |
+
pass
|
| 149 |
+
|
| 150 |
+
if not str_final == '':
|
| 151 |
+
return str_final
|
| 152 |
+
else:
|
| 153 |
+
return 0
|
| 154 |
+
def els(self, namebook, number, tracert='false', visualice=False):
|
| 155 |
+
space = number
|
| 156 |
+
abd = 'abcdefghijklmnñopqrstuvwxyz'
|
| 157 |
+
i=1
|
| 158 |
+
rese=""
|
| 159 |
+
totalvalue = 0
|
| 160 |
+
D = self.GetDataBook(namebook)
|
| 161 |
+
for (z,b,y) in D:
|
| 162 |
+
try:
|
| 163 |
+
charnum = 0
|
| 164 |
+
res=""
|
| 165 |
+
|
| 166 |
+
for char in D[z,b,y]:
|
| 167 |
+
charnum = charnum+1
|
| 168 |
+
if (i % int(space)) == 0:
|
| 169 |
+
if tracert == 'true':
|
| 170 |
+
totalvalue = totalvalue + int(charnum)
|
| 171 |
+
print('Source:',int(z),'chapter:', int(b),'Verse:', int(y),'CharNum:',int(charnum),'Char:', char)
|
| 172 |
+
|
| 173 |
+
res=res+char
|
| 174 |
+
|
| 175 |
+
i=i+1
|
| 176 |
+
rese=rese+" "+res
|
| 177 |
+
except:
|
| 178 |
+
pass
|
| 179 |
+
#print('Total', totalvalue)
|
| 180 |
+
ret = re.sub('\s+', ' ', rese.strip())
|
| 181 |
+
return ret, totalvalue
|
| 182 |
+
|
| 183 |
+
def GetDataBook(self, bibleNumberBook):
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
JSON = books.rawdata(bibleNumberBook)
|
| 187 |
+
ListOfJSONStringsParsed, ListOfJSONStringsParsedWithSpaces = util.fn_TextFilePreprocess(JSON)
|
| 188 |
+
ListOfDictsOfJSONStringsParsed, ListOfDictsOfJSONStringsParsedWithSpaces = util.fn_ConvertJSONStringsToDicts(ListOfJSONStringsParsed, ListOfJSONStringsParsedWithSpaces)
|
| 189 |
+
SearchTextChosen = util.fn_GetNumberOfTextChosen(ListOfDictsOfJSONStringsParsed)
|
| 190 |
+
ZippedTupleNoSpaces, ZippedTupleWithSpaces = util.fn_ZippedTupleCreate(ListOfDictsOfJSONStringsParsed, ListOfDictsOfJSONStringsParsedWithSpaces, SearchTextChosen)
|
| 191 |
+
D, DS = util.fn_DictionaryOfVersesCreate(ZippedTupleNoSpaces, ZippedTupleWithSpaces)
|
| 192 |
+
S, L, DL, D5, ListOfWords = util.fn_DataObjectsCreate(D, DS)
|
| 193 |
+
N, NW = util.fn_GetNumberValues(S, ListOfWords)
|
| 194 |
+
ListOfIndexesCustom = util.fn_ListOfIndexesCustomCreate(D5)
|
| 195 |
+
W = util.fn_TupleOfWordsAndGematriaValuesCreate(ListOfWords, NW)
|
| 196 |
+
|
| 197 |
+
return D
|
| 198 |
+
|
| 199 |
|
| 200 |
def process_json_files(start, end, step, length=0, tlang="en", spaces_include=False, strip_in_braces=True, strip_diacritics=True):
|
| 201 |
base_path = "resources/texts"
|