1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
## LIBRARIES
# //////////////////////////////////////////////////////////////////////////////
# import pandas as pd # pandas for excel reading
import mysql.connector as mysql # mysql connector
import re # regex
import json # json
import os.path # ...
import datetime
t0_ = datetime.datetime.now()
## LOCAL FUNCTIONS
# //////////////////////////////////////////////////////////////////////////////
# do-me-INTeger
# ---
def domeInt(x) :
if isinstance(x, str) : # if string
return int(x.strip())
if isinstance(x, float) : # if float
return round(x)
return x # otherwise is int already
# do-me-Float
# ---
def domeFloat(x) :
if isinstance(x, str) :
return float(x.strip())
else :
return x + 0.00 # make sure that result is float
## Clean Text ...
# -> removes some general/neutral words and symbols
# -> ignores some in-line characters
# -> also strips spare spaces
# function is applied onto the full title/description
# ---
def cleanText(x) :
ignoreList = '" ( ) [ ]'.split(' ')
for r in ignoreList :
x = x.replace(r, ' ')
x = x.replace(' ', ' ') # remove spare spaces
x = x.replace(' ', ' ')
x = x.replace(' ', ' ')
return x.replace(' ', ' ') # one lase (just in case)
## kbLatinString ...
# -> translate/re-wrrite string using latin-characters
# -> function is used used anywhere
def kbLatinString( txt ) :
maTable = txt.maketrans(
"ςερτυθιοπασδφγηξκλζχψωβνμΕΡΤΥΘΙΟΠΑΣΔΦΓΗΞΚΛΖΧΨΩΒΝΜάέήίόύώϊΐϋΆΈΉΊΌΎΏΪΫQWERTYUIOPASDFGHJKLZXCVBNM",
"sertyuiopasdfghjklzxcvbnmertyuiopasdfghjklzxcvbnmaehioyviiyaehioyviyqwertyuiopasdfghjklzxcvbnm"
)
txt = txt.replace('\'', '')
return txt.translate(maTable).lower()
# letters-only translation to key-pressed characters (latin)
# this minimized version of kbLatinString is used in markLink()
# ---
def kbLatinLetter( txt ) :
maTable = txt.maketrans(
"ςερτυθιοπασδφγηξκλζχψωβνμΕΡΤΥΘΙΟΠΑΣΔΦΓΗΞΚΛΖΧΨΩΒΝΜάέήίόύώϊΐϋΆΈΉΊΌΎΏΪΫ",
"sertyuiopasdfghjklzxcvbnmertyuiopasdfghjklzxcvbnmaehioyviiyaehioyviy"
)
return txt.translate(maTable).lower()
# isSignificant
# decides if the term is significant to be indexed;
# a term is significant if does not contain digit-chars [0-9], comma (,) or period (.)
# ---
significantExceptios = '7UP 3ΑΛΦΑ 17 3Π 7DAYS K2R'.split(' ')
def isSignificant(x) :
# fisrts exclude some notable exceptions (mostly brands)
if x in significantExceptios :
return True
return not bool( re.match("\S*\d+\S*", x) )
# check if word: w
# … has synonyms; return list of synonyms
# ---
def synonymKeys(w) :
w_kb = kbLatinString(w)
syns = [ w ]
found = False
# check if has synonyms
for group in synonyms :
possibles = group.split()
for wi in possibles :
if kbLatinString(wi) == w_kb :
syns = possibles
found = True
break
if found :
break
return syns
# set root-keyword: wl (if not exist)
# update frequency: f
# into list: l
# NOTE:
# * wl is a list of synonym-words
# ** comparison is based on the *keyboard* format
## ---
def rootKey ( wl, f, l ) :
keyExists = False
w_kb = kbLatinString(wl[0]) # cache kb format
# check if exists in root keys already
# NOTE: you only need to check the 1st word of synonyms-list
for it in l :
if it['kb'] == w_kb :
keyExists = True
it['f'] += f
break
# if not exists, append keyword
if keyExists == False :
l.append({
'w' : wl,
'kb' : w_kb,
'f' : f,
'c' : []
})
# connect keys: a , b (each one is a list of synonmyms)
# of product with id: i
# with frequency: f
# into list: l
## ---
def connectKeys( a, b, i, f, l ) :
kbA = kbLatinString(a[0])
kbB = kbLatinString(b[0])
if kbA == kbB :
return False ## exclude just-in-case
for it in l :
if it['kb'] == kbA :
# found: a;
# let's update connection to: b
bExists = False
for jt in it['c'] :
if jt['kb'] == kbB :
bExists = True
# update the connection's data
jt['f'] += f
jt['p'].append(i)
break
if bExists == False :
# create connection with word: b
it['c'].append({
'w': b,
'kb': kbB,
'f': f,
'p': [ i ]
})
break
## let mysql to return valid strings
## (otherwise it returns strings with missed characters)
# credit: https://stackoverflow.com/a/68784172
# analytical credit: https://stackoverflow.com/questions/27566078/
def get_data_from_db(cursor, sql):
output = []
cursor.execute(sql)
row = cursor.fetchone()
while row is not None:
row_to_return = row.decode('utf-8') if isinstance(row, bytearray) else row
output.append(row_to_return)
row = cursor.fetchone()
return output
## replaces
# do all replaces in place
# --- (preproccessing)
replaces = []
replaceSource = [
'3 ΑΛΦΑ ;3ΑΛΦΑ ',
'HEAD & SHOULDERS ;HEAD&SHOULDERS ',
'W.K Kellogg ; ',
'ΦΙΛΕΤ ;Φιλέτο ',
'ΕΝΕΛΛΑΔ ;Εν-Ελλάδι ',
'ΓΑΛΟΠΟΥΛ ;Γαλοπούλα ',
'7 DAYS ;7DAYS ',
'ΜΠΑΡΜΠΑ ΣΤΑΘΗ ;ΜΠΑΡΜΠΑ-ΣΤΑΘΗΣ '
]
for it in replaceSource :
st = it.split(';')
replaces.append({ 'src': st[0], 'trg': st[1] })
def do_replaces(w) :
for it in replaces :
w = w.replace(it['src'], it['trg'])
return w
## main preproccess function for product descriptions
# ---
def preprocessEdit(w) :
w = do_replaces(w)
# ... do other things if needed
# then ...
return w
## mark a link to a text
# conecting them with a dash/minus character
# ---
def markLink(lws, text) :
text_kb = kbLatinLetter(text.replace(' ', '-'))
lws_kb = kbLatinLetter(lws)
try:
index_l = text_kb.lower().index(lws_kb.lower())
except:
return text
else:
return text[:index_l] + lws + text[index_l + len(lws):]
### # --- list of normalized word combinations
### replaceWords = [
### 'HEAD & SHOULDERS; HEAD&SOULDERS',
### 'ΟΛΙΚΗΣ 'ΑΛΕΣΗΣ; Ολικής Άλεσης',
### 'Χωρίς προσθήκη ζάχαρης; Χωρίς-Ζάχαρη',
### 'ΚΑΠΝ.CRETA-FARMS; ΚΑΠΝΙΣΤΗ CRETA-FARMS'
### ]
# --- list of linked-words
linkedWords = [
'Χωρίς-Γλουτένη',
'Χωρίς-Ζάχαρη',
'Χωρίς-Αλάτι',
'Χωρίς-Λακτόζη',
'Χωρίς-Συντηρητικά',
'Χωρίς-Αλκοόλ',
'Χωρίς-Kαφεϊνη',
'Χωρίς-Γλυκάνισο',
'Χωρίς-Ανθρακικό',
'Υψηλής-Παστερίωσης',
'Ολικής-Άλεσης',
'Ολικής-Aλέσεως',
'Χαρτί-Υγείας',
'ρολό-υγείας',
'χαρτί-τουαλέτας',
'Χαρτί-Κουζίνας',
'Μπάρες-Δημητριακών',
'Μπαρμπα-Στάθης',
'COCA-COLA'
'Aς-Μαγειρέψουμε',
'ΚΡΙΣ-ΚΡΙΣ',
'ΚΡΙ-ΚΡΙ',
'ΕΛ-ΓΚΡΕΚΟ',
'FREE-STEP',
'EL-SABOR',
'LE-PETIT-MARSEILLAIS',
'DOUWE-EGBERTS',
'ΕΝ-ΕΛΛΑΔΙ',
'SPIN-SPAN',
'CRETA-FARMS',
'CRETA-FARM',
'NES-CAFE',
'Ολες-τις-Χρήσεις',
'Το-Μάννα',
'Χωρίς-προσθήκη-ζάχαρης'
]
# mark linked words (connect them with a dash)
# return new text after "all-links" are marked
# ---
def markLinkedWords(text) :
for lw in linkedWords :
text = markLink( lw, text )
return text
## handle words that can never be the first word on a search
noRootKeywords = []
noRoot = [
'χωρίς',
'εισαγωγής',
'δώρο',
'γεύση',
'γεύσεις',
'φέτες',
'Χωρίς-Γλουτένη',
'Χωρίς-Ζάχαρη',
'Χωρίς-Αλάτι',
'Χωρίς-Λακτόζη',
'Χωρίς-Συντηρητικά',
'Χωρίς-Αλκοόλ',
'Χωρίς-Kαφεϊνη',
'Χωρίς-Γλυκάνισο',
'Χωρίς-Ανθρακικό',
'Υψηλής-Παστερίωσης',
'Ολες-τις-Χρήσεις',
'Ολικής-Άλεσης',
'Ολικής-Aλέσεως',
'Ολικής',
'Γαϊδούρας',
'Γαϊδάρου',
'Ρούχων',
'Πιάτων',
'πλύσεις',
'Πλυντηρίου',
'Φύλλων',
'Γάλακτος',
'Χρήσης',
'Τύπου',
'Ολλανδίας',
'Απορριμμάτων',
'Medium',
'Μαλλιά',
'Μαλλιών',
'Γενικής',
'Plus',
'Classic',
'Έκπληξη',
'Μάνης',
'Ελάτου',
'Άγριων',
'Βοτάνων',
'Λακωνίας',
'ΠΑΡΑΓΓΕΛΙΩΝ'
]
for w in noRoot :
noRootKeywords.append(kbLatinString(w))
## PREPARE (or build) exception objects
# //////////////////////////////////////////////////////////////////////////////
# --- list of words to exclude from keywords
# NOTE:
# APPLIED in PER-WORD base -> after spliting description to words
removeList = []
removeOriginals = 'μας με σε για του της των από στο στον & r s ft l τ e g h k m n o p s x'.split(' ')
for it in removeOriginals :
removeList.append(kbLatinString(it))
# --- list of synonyms
# in fact
synonyms = [
'μπίρα μπύρα μπίρες μπύρες',
'αυγά αβγά αυγό',
'σίκαλης σικάλεως',
'ξηρά ξερά',
'ρολό ρολλό',
'coca-cola cocacola coke',
'χαρτί-υγείας ρολό-υγείας χαρτί-τουαλέτας',
'χαρτί-κουζίνας ρολό-κουζίνας',
'οινος κρασι',
'ΚΑΤΣΕΛΗΣ ΚΑΤΣΕΛΗ',
'DR-OETKER OETKER',
'DR.BECKMANN BECKMANN',
'NES-CAFE NESCAFE',
'Ολικής-Άλεσης Ολικής-Aλέσεως Ολικής',
'τσίπουρο ρακή',
'Βρώμη Βρώμης',
'Φράουλα Φράουλες Φράουλας',
'Μαλλιά Μαλλιών',
'Κέικ, Cake',
'CRETA-FARMS CRETA-FARM',
'MARSEILLAIS LE-PETIT-MARSEILLAIS PETIT-MARSEILLAIS',
'Γαϊδούρας Γαϊδάρου',
'ΚΑΛΟΓΕΡΑΚΗΣ ΚΑΛΟΓΕΡΑΚΗ',
'ΚΑΪΔΑΝΤΖΗΣ ΚΑΪΔΑΝΤΖΗ',
'ΥΦΑΝΤΗΣ ΥΦΑΝΤΗ',
'ΣΥΝΑΓΡΙΔΑ ΣΥΝΑΓΡΙΔΕΣ',
'Ντομάτα Ντομάτας',
'Ελαφρύ Ελαφρά Light',
'Εγχώρια Εγχώριες Ελληνικό Ελληνική Ελληνικά',
'τριμμένη τριμμένο',
'Τόνος Τόνου',
'Κριθαρένια κρίθινα',
'Χωρίς-Kαφεϊνη Decaffeine',
'Το-Μάννα Μάννα',
'Κράνμπερι Κράνμπερις',
'Κρήτης Κρητικό',
'Πέννες Πένες',
'Μακαρόνια Σπαγγέτι Σπαγγετίνι Σπαγγετόνι',
'Καρτέλλα Καρτέλα Καρτέλλες'
]
## Read data
# //////////////////////////////////////////////////////////////////////////////
## LOCAL CONSTANTS
_COL = {
# -- main info
'freq' : 0, # frequency (based on recent orders)
'pid' : 1, # product id
'brand' : 2, # brand
'barcd' : 3, # barcode
'sklcd' : 4,
'eyscd' : 5,
'descr' : 6, # product description
'bpcs' : 7, # bpcs_code
'img' : 8 # product's image file-name
}
# enter your
HOST = "127.0.0.1" # server IP address/domain name
DATABASE = "dev_pythia_db" # database name
USER = "pythia_db_user_dev"
PASSWORD = "VnEP0eysjiXDHcfM"
# connect to MySQL server
_dbc = mysql.connect(
host=HOST,
database=DATABASE,
user=USER,
password=PASSWORD,
use_unicode=True,
charset='utf8'
)
print("Connected to:", _dbc.get_server_info())
# execute SQL to get all data you need
crs = _dbc.cursor()
query = '''
SELECT count(pl.eys_code) as FREQuency,
pl.product_id as product_id,
pb.brand_name,
pl.barcode, pl.skl_code, pl.eys_code,
IF( pd.description IS NOT NULL , pd.description , pl.product_description ) AS product_description,
pl.bpcs_code,
pd.image_path
FROM product_list as pl
LEFT JOIN delivery_orders_products AS dop ON dop.product_id = pl.eys_code
LEFT JOIN delivery_orders AS do ON dop.order_id = do.id
LEFT JOIN product_list AS replacement ON dop.replacement_for = replacement.eys_code
LEFT JOIN product_details AS pd ON pl.eys_code = pd.eys_code
LEFT JOIN product_brands pb ON pl.brand_id = pb.id
WHERE pl.active = 1 AND pl.sap_code IS NOT NULL AND pl.product_category_sap_4 NOT LIKE '72%'
GROUP BY pl.product_id
ORDER BY FREQuency DESC
'''
results_ = get_data_from_db(crs, query)
t_read = datetime.datetime.now()
# --- Lists to fill
keywords_ = [] # all data; main exported object
minilist_ = []
products_ = []
## keywords format:
## [
## {
## w : [ word, word-synonym, ... ],
## kb : = kbLatinString(word)
## f : 150,
## c : [
## { w: ['fish', 'fishes'], f: 150, p: [122, 254, 907] },
## { w: ['juice'], f: 50, p: [254, 351] }
## ]
## },
## ...
## ]
##
## --- index:
## w : words / list of synonyms (str/utf-8)
# kb : ascii-latin-keypoard format of first item of "w" list
## f : frequency (int)
## c : combos / connections (list of objects)
## p : list of product-ids found in specific words-combination (list of int)
records_counter = 0
## LOOP through the rows to pre-proccess all products
## ---
for row in results_ :
records_counter += 1
description = row[_COL['descr']] # product description
## depricate: pid = domeInt( row[_COL['pid']] ) # product-id
fq = 0 if None else domeInt( row[_COL['freq']] ) # frequency
barcode = 0 if None else domeInt( row[_COL['barcd']] )
sklcode = 0 if None else domeInt( row[_COL['sklcd']] )
eyscode = 0 if None else domeInt( row[_COL['eyscd']] )
bpcs = 0 if None else domeInt( row[_COL['bpcs']] )
img = row[_COL['img']]
pid = eyscode # actual product id (pid) it the eys_code
# edit descriptions
description = preprocessEdit(description)
# setup product
# ---
products_.append({
'w' : description,
'id' : eyscode,
'f' : fq,
'bc' : barcode,
'sc' : sklcode,
'bp' : bpcs,
'i' : img
})
description = cleanText(description) # clean description string before spliting
description = markLinkedWords(description) # ...
keys = [] # list of product's key(word)s
words = description.split() # split to words
for w in words :
if kbLatinString(w) not in removeList: # if not in removeList
if isSignificant(w) : # and if significant
keys.append(w) # keep it
## print(pid, description, words, keys)
## print(pid, keys)
# append words (and their combos) to the list
for w in keys :
wl = synonymKeys(w)
# update root word frequency (if w CAN be a root word)
if kbLatinLetter(w) not in noRootKeywords :
rootKey( wl, fq, keywords_ )
## if no other keyword in description add a dummy one
# so preserve reference to the final product
if len(keys) == 1 :
connectKeys( wl, ['*'], pid, fq, keywords_ )
for w2 in keys :
if w2 != w :
w2syns = synonymKeys(w2)
connectKeys( wl, w2syns, pid, fq, keywords_ )
# --- remove 'kb' keywords
for ki in keywords_ :
del ki['kb'] # kb not needed (kb_translation in js is really fast)
## SORT keywords
# //////////////////////////////////////////////////////////////////////////////
# --- sort childs of each key (per frequency, desc)
for it in keywords_ :
it['c'].sort(key=lambda x: x['f'], reverse=True)
# --- sort root keys
keywords_.sort(key=lambda x: x['f'], reverse=True)
t_main = datetime.datetime.now()
print('Proccessing ended; saving results in json format ...')
## OUTPUT final data to a json-format file
# //////////////////////////////////////////////////////////////////////////////
with open("results/keywords.json", "w", encoding="utf-8") as outfile :
data = json.dump(keywords_, outfile, sort_keys=False, ensure_ascii=False)
with open("results/products.json", "w", encoding="utf-8") as outfile :
data = json.dump(products_, outfile, sort_keys=False, ensure_ascii=False, separators=(',', ':'))
t_end = datetime.datetime.now()
print(records_counter, 'products proccessed')
print('execution time:', (t_end - t0_))
print('read.n.parse sources:', (t_read - t0_))
print('proccessing products:', (t_main - t_read))
## NOTE:
## prepare cloud-sql-proxy
## ---
## * install cloud-sql-proxy
## : sudo wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O /usr/local/cloud_sql_proxy
## : sudo chmod +x /usr/local/cloud_sql_proxy
##
## * prepare/export/publish/copy credentials ...
## : sudo cp /some/path/to/cloudsqlproxy.json /usr/local
##
## * finaly run the database instance
## : /usr/local/cloud_sql_proxy -instances=pythia-251711:europe-west4:pythia-db-eu=tcp:3306 -credential_file=cloudsqlproxy.json
##
## after installation only the last command needs to run before connecting to the cloud-sql
# ΠΑΝΤΕΛΟΝΙ ΑΝΔ ΦΟΥΤ ΑΝ ΣΤΑ ΠΡΑΣ XXXL
# MAYBELLINECONCEALERAGEREWBLMEDIUM
|