ranking.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # From http://www.berratings.com/wp-content/uploads/2013/11/BER_Cert_with_explaination-e1397729848491.jpg
  2. _BER_RANKINGS = {
  3. 'A1': (25 + 25) / 2.0,
  4. 'A2': (25 + 50) / 2.0,
  5. 'A3': (50 + 75) / 2.0,
  6. 'B1': (75 + 100) / 2.0,
  7. 'B2': (100 + 125) / 2.0,
  8. 'B3': (125 + 150) / 2.0,
  9. 'C1': (150 + 175) / 2.0,
  10. 'C2': (175 + 200) / 2.0,
  11. 'C3': (200 + 225) / 2.0,
  12. 'D1': (225 + 260) / 2.0,
  13. 'D2': (260 + 300) / 2.0,
  14. 'E1': (300 + 340) / 2.0,
  15. 'E2': (340 + 380) / 2.0,
  16. 'F': (380 + 450) / 2.0,
  17. 'G': (450 + 450) / 2.0,
  18. 'Exempt': (500 + 500) / 2.0,
  19. }
  20. _BER_CONF = [
  21. (25, 1),
  22. (300, 0.1),
  23. ]
  24. _SIZE_CONF = [
  25. (50, 0.3),
  26. (150, 1),
  27. (400, 1),
  28. ]
  29. _BEDROOMS_CONF = [
  30. (2, 0.3),
  31. (4, 1),
  32. (10, 1),
  33. ]
  34. _COMMUTE_CONF = [
  35. (0, 1),
  36. (60*70, 0),
  37. ]
  38. _PRICE_CONF = [
  39. (250, 1),
  40. (450, 0.5),
  41. ]
  42. def _linear(signal, configuration):
  43. if signal < configuration[0][0] or signal > configuration[-1][0]:
  44. return 0
  45. for i, (limit, value) in enumerate(configuration):
  46. next_limit, next_value = configuration[i+1]
  47. if signal <= next_limit:
  48. limit_diff = next_limit - limit
  49. signal_ratio = (signal - limit) / float(limit_diff)
  50. return value + (next_value - value) * signal_ratio
  51. def r_size(size):
  52. return _linear(size, _SIZE_CONF)
  53. def r_price(price):
  54. return _linear(price, _PRICE_CONF)
  55. def r_commute_duration(duration):
  56. return _linear(duration, _COMMUTE_CONF)
  57. def r_bedrooms(bedrooms):
  58. return _linear(bedrooms, _BEDROOMS_CONF)
  59. def r_ber(ber):
  60. ber = ber or 'Exempt'
  61. value = None
  62. if '-' in ber:
  63. a, b = ber.split('-')
  64. value = (_BER_RANKINGS[a] + _BER_RANKINGS[b]) / 20.
  65. else:
  66. value = _BER_RANKINGS[ber]
  67. return _linear(value, _BER_CONF)
  68. def _rank(data):
  69. res = 1.0
  70. res *= r_size(data['size'])
  71. res *= r_price(data['price'])
  72. res *= r_commute_duration(data['commute_duration'])
  73. res *= r_ber(data['ber'])
  74. res *= r_bedrooms(data['bedrooms'])
  75. return res
  76. def add_scores(properties_dict):
  77. for p in properties_dict.values():
  78. p['score'] = _rank(p)