ranking.py 2.4 KB

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