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
|
## LIBRARIES
# //////////////////////////////////////////////////////////////////////////////
# import pandas as pd # pandas for excel reading
import re # regex
import json # json
import os.path # ...
import datetime
t0_ = datetime.datetime.now()
## LOCAL FUNCTIONS
# //////////////////////////////////////////////////////////////////////////////
## 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)
# 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 (.)
# ---
def isSignificant(x) :
# fisrts exclude some notable exceptions (mostly brands)
if x in ['7UP', '3ΑΛΦΑ', '17'] :
return True
return not bool( re.match("\S*\d+\S*", x) )
def kbLatinString( txt ) :
maTable = txt.maketrans(
"ςερτυθιοπασδφγηξκλζχψωβνμΕΡΤΥΘΙΟΠΑΣΔΦΓΗΞΚΛΖΧΨΩΒΝΜάέήίόύώϊΐϋΆΈΉΊΌΎΏΪΫQWERTYUIOPASDFGHJKLZXCVBNM",
"sertyuiopasdfghjklzxcvbnmertyuiopasdfghjklzxcvbnmaehioyviiyaehioyviyqwertyuiopasdfghjklzxcvbnm"
)
txt = txt.replace('\'', '')
return txt.translate(maTable).lower()
# letters-only translation to key-pressed characters (latin)
# ---
def kbLatinLetter( txt ) :
maTable = txt.maketrans(
"ςερτυθιοπασδφγηξκλζχψωβνμΕΡΤΥΘΙΟΠΑΣΔΦΓΗΞΚΛΖΧΨΩΒΝΜάέήίόύώϊΐϋΆΈΉΊΌΎΏΪΫ",
"sertyuiopasdfghjklzxcvbnmertyuiopasdfghjklzxcvbnmaehioyviiyaehioyviy"
)
return txt.translate(maTable).lower()
## 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',
### 'ΟΛΙΚΗΣ 'ΑΛΕΣΗΣ; Ολικής Άλεσης',
### 'Χωρίς προσθήκη ζάχαρης; Χωρίς-Ζάχαρη'
### ]
# --- 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',
'Ολες-τις-Χρήσεις',
]
# text after "all-links" marked
# ---
def markLinkedWords(text) :
for lw in linkedWords :
text = markLink( lw, text )
return text
## PREPADE (or build) exception objects
# //////////////////////////////////////////////////////////////////////////////
# --- list of possible combos to check
_2check_kb = []
_2check = ['με', 'σε', 'για', 'όλες', 'χωρίς' ]
for it in _2check :
_2check_kb.append(kbLatinString(it))
linkedWordsFound = [
{ 'w' : 'se', 'links' : [] },
{ 'w' : 'oles-tis', 'links' : [] },
{ 'w' : 'xvris', 'links' : [] }
]
def recordLink( parent, child, id ) :
if parent != '' :
for it in linkedWordsFound :
if parent == it['w'] :
is_a_new_combo = True
for li in it['links'] :
if li['w'] == child :
li['p'].append(id)
li['c'] += 1
is_a_new_combo = False
break
if is_a_new_combo :
it['links'].append({ 'w': child, 'p': [ id ], 'c': 1 })
# --- 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',
'Γαϊδούρας Γαϊδάρου',
'ΚΑΛΟΓΕΡΑΚΗΣ ΚΑΛΟΓΕΡΑΚΗ',
'ΚΑΪΔΑΝΤΖΗΣ ΚΑΪΔΑΝΤΖΗ',
'ΥΦΑΝΤΗΣ ΥΦΑΝΤΗ',
'ΣΥΝΑΓΡΙΔΑ ΣΥΝΑΓΡΙΔΕΣ',
'Ντομάτα Ντομάτας',
'Ελαφρύ Ελαφρά Light',
'Εγχώρια Ελληνικό Ελληνικά',
'τριμμένη τριμμένο',
'Τόνος Τόνου',
'Κριθαρένια κρίθινα'
]
## Read data
# //////////////////////////////////////////////////////////////////////////////
_file = open ('data/eshop-products.json', "r") # JSON source file
results_ = json.loads(_file.read()) # Reading from file
_file.close() # Closing file
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['Title'] # product description
pid = row['ID'] # product-id
fq = row['freq'] # frequency
description = cleanText(description) # clean description string before spliting
description = markLinkedWords(description) # ...
keys = description.split() # split to words = keys
is_combo_key = False
combo_key = ''
# append words (and their combos) to the list
for w in keys :
if kbLatinString(w) in _2check_kb :
combo_key = w
is_combo_key = True
else :
if is_combo_key :
recordLink(kbLatinString(combo_key), kbLatinString(w), pid)
is_combo_key = False
combo_key = ''
# print(linkedWordsFound)
# PRINT RESULTS
# ---
for ri in linkedWordsFound :
print('---', ri['w'], ':', len(ri['links']))
subtotal = 0
for li in ri['links'] :
subtotal += li['c']
for li in ri['links'] :
## if li['c'] > 10 or li['c']/subtotal > .2 :
print( ri['w'], li['w'], ' : ', li['c'], ' (', int(li['c']*100/subtotal), '%)' )
|