ranking.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.8),
  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. _COMMUTE_CONF = [
  37. (0, 1),
  38. (60*70, 0),
  39. ]
  40. _PRICE_CONF = [
  41. (300, 1),
  42. (400, 0.8),
  43. (550, 0.3),
  44. ]
  45. def _linear(signal, configuration):
  46. if signal < configuration[0][0] or signal > configuration[-1][0]:
  47. return 0
  48. for i, (limit, value) in enumerate(configuration):
  49. next_limit, next_value = configuration[i+1]
  50. if signal <= next_limit:
  51. limit_diff = next_limit - limit
  52. signal_ratio = (signal - limit) / float(limit_diff)
  53. return value + (next_value - value) * signal_ratio
  54. def r_size(size):
  55. return _linear(size, _SIZE_CONF)
  56. def r_price(price):
  57. return _linear(price, _PRICE_CONF)
  58. def r_commute_duration(duration):
  59. return _linear(duration, _COMMUTE_CONF)
  60. def r_bedrooms(bedrooms):
  61. return _linear(bedrooms, _BEDROOMS_CONF)
  62. def r_ber(ber):
  63. ber = ber or 'Exempt'
  64. value = None
  65. if '-' in ber:
  66. a, b = ber.split('-')
  67. value = (_BER_RANKINGS[a] + _BER_RANKINGS[b]) / 20.
  68. else:
  69. value = _BER_RANKINGS[ber]
  70. return _linear(value, _BER_CONF)
  71. def _rank(data):
  72. res = 1.0
  73. res *= r_size(data['size'])
  74. res *= r_price(data['price'])
  75. res *= r_commute_duration(data['commute_duration'])
  76. res *= r_ber(data['ber'])
  77. res *= r_bedrooms(data['bedrooms'])
  78. return res
  79. def add_scores(properties_dict):
  80. for p in properties_dict.values():
  81. p['score'] = _rank(p)