/
challenge.py
213 lines (171 loc) · 5.92 KB
/
challenge.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
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
import os
import json
class MatchSearch:
def __init__(self):
self.result = {}
self.non_matches = []
def match_listings(self, listings, products, debug=None):
self.result = {}
self.non_matches = []
count = 0
for listing in listings:
product = find_product(listing, products)
count += 1
if debug is not None:
debug(count)
if product is not None:
name = product.name
if name in self.result:
self.result[name].append(listing.dict_without_tags())
else:
self.result[name] = []
self.result[name].append(listing.dict_without_tags())
else:
self.non_matches.append(listing)
return self.result
def find_product(listing, products):
listing_tags = listing.extract_tags()
product_ratings = {}
for product in products:
product_ratings[product.name] = {'product':product, 'rating':0}
for tag in listing_tags:
product_matches = find_word_match_in_products(tag, products)
for product_match in product_matches:
product_rating = product_ratings[product_match.name]
product_ratings[product_match.name] = {'product':product_rating['product'], 'rating':product_rating['rating'] + 1}
pairs = return_dict_pairs_sorted_descending(product_ratings)
if(pairs[0][1] > 0):
return filter_by_model(pairs, listing_tags)
else:
return None
def filter_by_model(pairs, listing_tags):
potential_choices = list(filter(lambda x: x[1]!= 0, pairs))
for potential_choice in potential_choices:
#if has rating greater than 1 and model is present
if potential_choice[1] > 1 and verify_model(potential_choice, listing_tags):
return potential_choice[0]
return None
def verify_model(potential_product, listing_tags):
model = prepare_string(potential_product[0].model)
split = model.split()
if len(split) == 1:
for tag in listing_tags:
if tag.startswith(model):
return True
else:
join_model = ''.join(model.split())
for tag in listing_tags:
if tag.startswith(join_model):
return True
listing_sentence = ' '.join(listing_tags)
if model in listing_sentence:
return True
return False
def return_dict_pairs_sorted_descending(product_ratings):
pairs = []
for key in product_ratings.keys():
value = product_ratings[key]
pairs.append((value['product'], value['rating']))
pairs = sorted(pairs, key=lambda v: v[1], reverse=True)
return pairs
def find_word_match_in_products(listing_tag, products):
matches = []
for product in products:
product_tags = product.extract_tags()
if listing_tag in product_tags:
matches.append(product)
return matches
ignored = 'dmc'
def prepare_string(s):
s = s.translate(s.maketrans("", "", ",.-!")).lower()
s = s.replace(ignored, '')
return s
class Listing:
def __init__(self, title, manufacturer, currency, price):
self.title = title
self.manufacturer = manufacturer
self.currency = currency
self.price = price
self.tags = []
def dict_without_tags(self):
d = dict(self.__dict__)
if 'tags' in d:
del d['tags']
return d
def __eq__(self, other):
return (isinstance(other, self.__class__)
and self.dict_without_tags() == other.dict_without_tags())
def extract_tags(self):
if not self.tags:
self.tags = self.extract_listing_tags()
return self.tags
def extract_listing_tags(self):
title = prepare_string(self.title)
manufacturer = prepare_string(self.manufacturer)
tags = []
tags.extend(title.split()[0:5])
tags.extend(manufacturer.split())
return tags
@staticmethod
def create(json_object):
return Listing(json_object['title'],json_object['manufacturer'],json_object['currency'], json_object['price'])
class Product:
def __init__(self, name, manufacturer, model, family, announced_date):
self.name = name
self.manufacturer = manufacturer
self.model = model
self.family = family
self.announced_date = announced_date
self.tags = []
def dict_without_tags(self):
d = dict(self.__dict__)
if 'tags' in d:
del d['tags']
return d
def __repr__(self):
return json.dumps(self.dict_without_tags())
def __eq__(self, other):
return (isinstance(other, self.__class__)
and self.dict_without_tags() == other.dict_without_tags())
def extract_tags(self):
if not self.tags:
self.tags = self.extract_product_tags()
return self.tags
def extract_product_tags(self):
product_tags = []
manufacturer = prepare_string(self.manufacturer)
family = prepare_string(self.family)
model = prepare_string(self.model)
product_tags.extend(manufacturer.split())
model_tags = model.split()
model_tags.append(''.join(model_tags))
product_tags.extend(model_tags)
product_tags.extend(family.split())
return product_tags
@staticmethod
def create(json_object):
return Product(json_object['product_name'], json_object['manufacturer'], json_object['model'], json_object['family'] if 'family' in json_object else '', json_object['announced-date'])
class FileReader:
def read_listings(self, filename):
listings_json = self.read_json_list(filename)
listings = []
for listing_json in listings_json:
listings.append(Listing.create(listing_json))
return listings
def read_products(self, filename):
products_json = self.read_json_list(filename)
products = []
for product_json in products_json:
products.append(Product.create(product_json))
return products
def read_json_list(self,filename):
script_dir = os.path.dirname(__file__)
file_path = filename
file_path = os.path.join(script_dir, file_path)
items_file = open(file_path, 'r')
items = []
for line in items_file:
item = json.loads(line)
items.append(item)
items_file.close();
return items