ranking.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. (60, 0.9),
  23. (140, 0.6),
  24. (300, 0.1),
  25. ]
  26. _SIZE_CONF = [
  27. (50, 0.3),
  28. (150, 1),
  29. (400, 1),
  30. ]
  31. _BEDROOMS_CONF = [
  32. (3, 0.6),
  33. (4, 1),
  34. (10, 1),
  35. ]
  36. # disabled
  37. _COMMUTE_CONF = [
  38. (0, 1),
  39. (60*70, 1),
  40. ]
  41. _PRICE_CONF = [
  42. (300, 1),
  43. (400, 0.7),
  44. (450, 0.3),
  45. ]
  46. def _linear(signal, configuration):
  47. if signal < configuration[0][0] or signal > configuration[-1][0]:
  48. return 0
  49. for i, (limit, value) in enumerate(configuration):
  50. next_limit, next_value = configuration[i+1]
  51. if signal <= next_limit:
  52. limit_diff = next_limit - limit
  53. signal_ratio = (signal - limit) / float(limit_diff)
  54. return value + (next_value - value) * signal_ratio
  55. def r_size(size):
  56. return _linear(size, _SIZE_CONF)
  57. def r_price(price):
  58. return _linear(price, _PRICE_CONF)
  59. def r_commute_duration(duration):
  60. return 1
  61. # return _linear(duration, _COMMUTE_CONF)
  62. def r_bedrooms(bedrooms):
  63. return _linear(bedrooms, _BEDROOMS_CONF)
  64. def r_ber(ber):
  65. ber = ber or 'Exempt'
  66. value = None
  67. if '-' in ber:
  68. a, b = ber.split('-')
  69. value = (_BER_RANKINGS[a] + _BER_RANKINGS[b]) / 20.
  70. else:
  71. value = _BER_RANKINGS[ber]
  72. return _linear(value, _BER_CONF)
  73. def _rank(data):
  74. res = 1.0
  75. res *= r_size(data['size'])
  76. res *= r_price(data['price'])
  77. res *= r_commute_duration(data['commute_duration'])
  78. res *= r_ber(data['ber'])
  79. res *= r_bedrooms(data['bedrooms'])
  80. return res
  81. def add_scores(properties_dict):
  82. for p in properties_dict.values():
  83. p['score'] = _rank(p)