# From http://www.berratings.com/wp-content/uploads/2013/11/BER_Cert_with_explaination-e1397729848491.jpg _BER_RANKINGS = { 'A1': (25 + 25) / 2.0, 'A2': (25 + 50) / 2.0, 'A3': (50 + 75) / 2.0, 'B1': (75 + 100) / 2.0, 'B2': (100 + 125) / 2.0, 'B3': (125 + 150) / 2.0, 'C1': (150 + 175) / 2.0, 'C2': (175 + 200) / 2.0, 'C3': (200 + 225) / 2.0, 'D1': (225 + 260) / 2.0, 'D2': (260 + 300) / 2.0, 'E1': (300 + 340) / 2.0, 'E2': (340 + 380) / 2.0, 'F': (380 + 450) / 2.0, 'G': (450 + 450) / 2.0, 'Exempt': (500 + 500) / 2.0, } _TYPE_CONF = { 'Site': 0, 'Other': 1, } _BER_CONF = { 25: 1, # A1 70: 0.7, # A3 140: 0.2, # B2 # 260: 0.5, # #D1 # 300: 1, # E1 # 450: 1, # G } _SIZE_CONF = { 90: 0, 100: 0.5, 180: 1, 400: 1, } _BEDROOMS_CONF = { None: 0.4, 2: 0.4, 3: 0.8, 4: 1, 10: 1, } _BATHROOMS_CONF = { None: 0.7, 1: 0.5, 2: 1, 4: 1, } # disabled _COMMUTE_CONF = { 0: 1, 60 * 70: 1, } _PRICE_CONF = { 300: 1, 900: 0.6, } _RERANKING_CONF = { 0: 0, 1: 1, } def _linear(signal, configuration): if signal in configuration: return configuration[signal] configuration = sorted([(k, v) for k, v in configuration.items() if k is not None]) if signal < configuration[0][0] or signal > configuration[-1][0]: return 0 for i, (limit, value) in enumerate(configuration): next_limit, next_value = configuration[i+1] if signal <= next_limit: limit_diff = next_limit - limit signal_ratio = (signal - limit) / float(limit_diff) return value + (next_value - value) * signal_ratio def r_size(size): return _linear(size, _SIZE_CONF) def r_price(price): return _linear(price, _PRICE_CONF) def r_commute_duration(duration): return _linear(duration, _COMMUTE_CONF) def r_bedrooms(bedrooms): return _linear(bedrooms, _BEDROOMS_CONF) def r_bathrooms(bathrooms): return _linear(bathrooms, _BATHROOMS_CONF) def r_ber(ber): ber = ber or 'Exempt' value = None if '-' in ber: a, b = ber.split('-') value = (_BER_RANKINGS[a] + _BER_RANKINGS[b]) / 20. else: value = _BER_RANKINGS[ber] return _linear(value, _BER_CONF) def r_type(ptype): return _TYPE_CONF.get(ptype, _TYPE_CONF['Other']) def r_rerank(score): return _linear(score, _RERANKING_CONF) def _rank(data): res = 1.0 if data['type'] != 'Site': res *= r_price(data['price']) res *= r_size(data['size']) if 'commute_duration' in data: res *= r_commute_duration(data['commute_duration']) res *= r_ber(data['ber']) res *= r_bedrooms(data['bedrooms']) res *= r_bathrooms(data['bathrooms']) res *= r_type(data['type']) else: res *= r_type(data['type']) return res def add_scores(properties_dict): for p in properties_dict.values(): p['score'] = _rank(p) return {k: v for k, v in properties_dict.items() if v['score'] > 0} def rerank(properties_dict): for p in properties_dict.values(): p['score'] = r_rerank(p['score']) return {k: v for k, v in properties_dict.items() if v['score'] > 0}