ranking.py 3.5 KB

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