ranking.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. _TYPE_CONF = {
  21. 'Site': 0,
  22. 'Other': 1,
  23. }
  24. _BER_CONF = {
  25. 25: 1,
  26. 70: 0.9,
  27. 260: 0.5,
  28. }
  29. _SIZE_CONF = {
  30. 90: 0,
  31. 100: 0.5,
  32. 180: 1,
  33. 400: 1,
  34. }
  35. _BEDROOMS_CONF = {
  36. None: 0.4,
  37. 2: 0.4,
  38. 3: 0.8,
  39. 4: 1,
  40. 10: 1,
  41. }
  42. _BATHROOMS_CONF = {
  43. None: 0.7,
  44. 1: 0.5,
  45. 2: 1,
  46. 5: 1,
  47. }
  48. # disabled
  49. _COMMUTE_CONF = {
  50. 0: 1,
  51. 60 * 70: 1,
  52. }
  53. _PRICE_CONF = {
  54. 400: 1,
  55. 800: 0.6,
  56. }
  57. _RERANKING_CONF = {
  58. 0: 0,
  59. 1: 1,
  60. }
  61. def _linear(signal, configuration):
  62. if signal in configuration:
  63. return configuration[signal]
  64. configuration = sorted([(k, v) for k, v in configuration.items() if k is not None])
  65. if signal < configuration[0][0] or signal > configuration[-1][0]:
  66. return 0
  67. for i, (limit, value) in enumerate(configuration):
  68. next_limit, next_value = configuration[i+1]
  69. if signal <= next_limit:
  70. limit_diff = next_limit - limit
  71. signal_ratio = (signal - limit) / float(limit_diff)
  72. return value + (next_value - value) * signal_ratio
  73. def r_size(size):
  74. return _linear(size, _SIZE_CONF)
  75. def r_price(price):
  76. return _linear(price, _PRICE_CONF)
  77. def r_commute_duration(duration):
  78. return _linear(duration, _COMMUTE_CONF)
  79. def r_bedrooms(bedrooms):
  80. return _linear(bedrooms, _BEDROOMS_CONF)
  81. def r_bathrooms(bathrooms):
  82. return _linear(bathrooms, _BATHROOMS_CONF)
  83. def r_ber(ber):
  84. ber = ber or 'Exempt'
  85. value = None
  86. if '-' in ber:
  87. a, b = ber.split('-')
  88. value = (_BER_RANKINGS[a] + _BER_RANKINGS[b]) / 20.
  89. else:
  90. value = _BER_RANKINGS[ber]
  91. return _linear(value, _BER_CONF)
  92. def r_type(ptype):
  93. return _TYPE_CONF.get(ptype, _TYPE_CONF['Other'])
  94. def r_rerank(score):
  95. return _linear(score, _RERANKING_CONF)
  96. def _rank(data):
  97. res = 1.0
  98. if data['type'] != 'Site':
  99. res *= r_price(data['price'])
  100. res *= r_size(data['size'])
  101. if 'commute_duration' in data:
  102. res *= r_commute_duration(data['commute_duration'])
  103. res *= r_ber(data['ber'])
  104. res *= r_bedrooms(data['bedrooms'])
  105. res *= r_bathrooms(data['bathrooms'])
  106. res *= r_type(data['type'])
  107. else:
  108. res *= r_type(data['type'])
  109. return res
  110. def add_scores(properties_dict):
  111. for p in properties_dict.values():
  112. p['score'] = _rank(p)
  113. return {k: v for k, v in properties_dict.items() if v['score'] > 0}
  114. def rerank(properties_dict):
  115. for p in properties_dict.values():
  116. p['score'] = r_rerank(p['score'])
  117. return {k: v for k, v in properties_dict.items() if v['score'] > 0}