ranking.py 3.2 KB

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