kora 1 year ago
parent
commit
4bb5cd8e33

+ 1 - 2
db/ndg2.gat4/keys/vettSpec.csv

@@ -1037,8 +1037,7 @@ intcode,unicode
 30001,AA
 30002,BA
 30003,194
-30004,"195
-195"
+30004,195
 30005,196
 30006,19B
 30007,1A2

+ 2 - 3
flask_be/engine/utilities/parsing_utilities.py

@@ -13,10 +13,9 @@ def combinations(s):
     result.extend([s[:start] + item + rest for rest in combinations(s[end + 1:])])
   return result
 
-#%% funzione interprete, sta alla base di ogni ricerca
-## DA MODIFICARE PER DB CIFRATO
+# Funzione interprete, preprocessa ogni ricerca
 def interpreter (data):
-    clean_data= data.replace("*", "%").replace("?", "_").replace(" ","").replace("'", "''").replace("’", "''")
+    clean_data= data.replace("\\*", "#ACTUAL*#").replace("\\?", "#ACTUAL?#").replace("*", "\\%").replace("?", "\\_").replace(" ","").replace("'", "''").replace("’", "''").replace('#ACTUAL%#', '*').replace('#ACTUAL_#', '?')
     return combinations(clean_data)    
 
 # %% funzione iniziale raddoppiata, è chiamata dalle funzioni di ricerca con iniziale raddoppiata

+ 25 - 24
flask_be/interface_sqlite3/encdec/de_code.py

@@ -6,7 +6,7 @@ class keyRing:
 
     def __init__(self, keyPath, dbEncoded, textsEncoded):
         self.keyPath = keyPath
-        self.vettSpec = self.getVettSpec(dbEncoded)
+        self.vettDictDec, self.vettDictEnc = self.getVettSpec(dbEncoded)
         self.textKeys = self.getKeys(textsEncoded)
 
     def getVettSpec(self, dbEncoded):
@@ -15,7 +15,17 @@ class keyRing:
         with open(self.keyPath + "vettSpec.csv", 'r') as file1:
             reader = csv.DictReader(file1)
             vettSpec = [row for row in reader]
-            return vettSpec
+            vettDictDec = {}
+            vettDictEnc = {}
+            for index, entry in enumerate(vettSpec):
+                if index==0:
+                    continue
+                vettDictDec[chr(int(entry['intcode']))] = chr(int(entry['unicode'], 16))
+                vettDictEnc[chr(int(entry['unicode'], 16))] = chr(int(entry['intcode']))
+            # Special chars
+            vettDictEnc['\\%'] = "%"
+            vettDictEnc['\\_'] = "_"
+            return vettDictDec, vettDictEnc
 
     def getKeys(self, textsEncoded):
         if not textsEncoded:
@@ -48,33 +58,24 @@ class keyRing:
 # DB Columns that need this:
 # FORM -> norm, spec, invnorm, invspec
 # LEM  -> norm, spec, invnorm, invspec, cat, omo
-def db_decode(vettSpec, string0):
+def db_decode(vettDictDec, string0):
+    return ''.join([vettDictDec[char] for char in string0])
 
-    res = ""
-    for char0 in string0:
-        #1
-        char0Dec = ord(char0) # Dal carattere al codice Unicode DECIMALE corrispondente
-        #2
-        char0ConvDec = next((el['unicode'] for el in vettSpec if el['intcode'] == str(char0Dec)), None) # Il codice DECIMALE (passato come stringa) viene ricercato in vettSpec, ritornando l'Unicode ESADECIMALE del carattere decriptato o None se non c'è riscontro -- il che non DOVREBBE succedere.
-        #3
-        res += chr(int(char0ConvDec, 16)) # Si converte il codice esadecimale a decimale e si usa la built-in chr per recuperare il carattere
-    return res
 #
-def db_encode(vettSpec, string0):
+def db_encode(vettDictEnc, string0):
     res = ""
+    prevChar = ""
     for char0 in string0:
-        # DA RIVEDERE
-        ############################
-        if char0 != "%" and "_":
-        ############################
-            #1
-            char0Hex = hex(ord(char0)) # Dal carattere al codice Unicode ESADECIMALE corrispondente
-            #2
-            char0ConvDec = next((el['intcode'] for el in vettSpec if el['unicode'] == char0Hex[2:].upper()), None) # Il codice ESADECIMALE, senza il prefisso '0x' (rimosso tramite [2:]) e convertito in maiuscole per rispettare il formato di vettSpec, viene ricercato in vettSpec, ritornando l'Unicode DECIMALE del carattere criptato o None se non c'è riscontro -- il che non DOVREBBE succedere.
-            #3
-            res += chr(int(char0ConvDec)) # Si usa la built-in chr per recuperare il carattere
+        if char0=="\\":
+            prevChar = "\\"
+            continue
+        if prevChar!="\\":
+            res += vettDictEnc[char0]
         else:
-            res += char0
+            comp = "\\" + char0
+            add = vettDictEnc.get(comp) if vettDictEnc.get(comp) is not None else vettDictEnc["\\"] + vettDictEnc[char0]
+            res += add
+
     return res
 
 # Text encoder/decoder

+ 4 - 4
flask_be/interface_sqlite3/query_handlers.py

@@ -105,8 +105,8 @@ class queryHandlerBasicSqlite:
             try:
                 data = queryData['data']
                 dataNorm = queryData['dataNorm']
-                data = [dc.db_encode(self.keyRing.vettSpec, datum) for datum in data]
-                dataNorm = [dc.db_encode(self.keyRing.vettSpec, datum) for datum in dataNorm]
+                data = [dc.db_encode(self.keyRing.vettDictEnc, datum) for datum in data]
+                dataNorm = [dc.db_encode(self.keyRing.vettDictEnc, datum) for datum in dataNorm]
                 queryData['data'] = data
                 queryData['dataNorm'] = dataNorm
             except KeyError as err:
@@ -118,13 +118,13 @@ class queryHandlerBasicSqlite:
         for row in result:
             for key, value in row.items():
                 if isColumnToDecode(key):
-                    row[key] = dc.db_decode(self.keyRing.vettSpec, value)
+                    row[key] = dc.db_decode(self.keyRing.vettDictDec, value)
         return result
 
     def db_results_decode_pandas(self, df):
         for col in df.columns:
             if isColumnToDecode(col):
-                df[col] = df[col].apply( lambda el: dc.db_decode(self.keyRing.vettSpec, el) )
+                df[col] = df[col].apply( lambda el: dc.db_decode(self.keyRing.vettDictDec, el) )
         return df