This repository has been archived by the owner on Dec 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
81 lines (61 loc) Β· 2.27 KB
/
preprocess.py
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
from data import categories
import csv
import re
import json
with open('./result.json', 'r') as result_file:
dataset = json.load(result_file, encoding='UTF-8')
mfrs = ['GET', 'HEYROO']
units = ['P', 'L', 'T', 'ml']
lasts = ['g', 'μ
']
products = {}
def is_appendable(product, original_name):
for p in products[category]:
if p['name'] == product['name']:
p['original'].append(original_name)
return False
return True
for category in categories.keys():
products[category] = []
for product in dataset[category]:
original_name = product['name']
segments = product['name'].split(')')
if len(segments) > 1:
if '(' not in segments[0]:
product['name'] = segments[1]
segments = product['name'].split('(')
product['name'] = segments[0]
for mfr in mfrs:
if mfr in product['name']:
product['name'] = product['name'].replace(mfr, '')
for unit in units:
if unit in product['name']:
# names like 'PBλΈκΈ°μ°μ ' must not be replaced
if product['name'].find(unit) > 0:
product['name'] = product['name'].replace(unit, '')
# to filter names like 'μμ±λ§λνλν¬2μ
'
for last in lasts:
if last in product['name']:
if product['name'][-1:] == last:
product['name'] = product['name'][:len(product['name']) - 1]
numbers = re.findall(r'[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?', product['name'])
if numbers:
last_number = numbers[-1:][0]
if product['name'].endswith(last_number):
product['name'] = product['name'].replace(last_number, '')
product['name'] = product['name'].strip()
if is_appendable(product, original_name):
product['original'] = [original_name]
products[category].append(product)
with open('./preprocessed.json', 'w') as result_file:
json.dump(products, result_file, ensure_ascii=False, sort_keys=True, indent=2)
print('π Preprocessed!')
keywords = {}
for category in categories.keys():
keywords[category] = []
for product in products[category]:
keywords[category].append(product['name'])
with open('./output.csv', 'w') as csv_file:
for key in keywords.keys():
for product in keywords[key]:
csv_file.write(f'{key},{product}\n')
print('πͺ Generated Output CSV file!')