spiderAB.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. """
  2. @author: luojunhui
  3. """
  4. class SearchABTest(object):
  5. """
  6. 搜索策略实验方案
  7. """
  8. ori_title = None
  9. article_summary = None
  10. article_keys = None
  11. trace_id = None
  12. def __init__(self, info, searchMethod):
  13. self.ori_title = info["ori_title"]
  14. self.article_summary = info["kimi_summary"]
  15. self.article_keys = info["kimi_keys"]
  16. self.trace_id = info["trace_id"]
  17. self.search_method = searchMethod
  18. async def base_search(self):
  19. """
  20. 兜底策略
  21. """
  22. result = await self.search_method.search_v1(
  23. text=self.article_keys[0],
  24. trace_id=self.trace_id
  25. )
  26. if result:
  27. return result
  28. else:
  29. sub_result = await self.search_method.search_v1(
  30. text=self.article_keys[1],
  31. trace_id=self.trace_id
  32. )
  33. if sub_result:
  34. return sub_result
  35. else:
  36. return await self.search_method.search_v1(
  37. text=self.article_keys[2],
  38. trace_id=self.trace_id
  39. )
  40. async def ab_0(self):
  41. """
  42. 默认原标题搜索
  43. :return:
  44. """
  45. search_result = await self.search_method.search_v1(
  46. text=self.ori_title,
  47. trace_id=self.trace_id
  48. )
  49. if search_result:
  50. return search_result
  51. else:
  52. return await self.base_search()
  53. async def ab_1(self):
  54. """
  55. 使用 content_summary搜索
  56. :return:
  57. """
  58. search_result = await self.search_method.search_v1(
  59. text=self.article_summary,
  60. trace_id=self.trace_id
  61. )
  62. if search_result:
  63. return search_result
  64. else:
  65. return await self.ab_0()
  66. async def ab_2(self):
  67. """
  68. 使用文本关键词搜索
  69. :return:
  70. """
  71. search_result = await self.search_method.search_v1(
  72. text=self.article_keys[0],
  73. trace_id=self.trace_id
  74. )
  75. if search_result:
  76. return search_result
  77. else:
  78. return await self.base_search()
  79. async def ab_3(self):
  80. """
  81. 使用文本关键词搜索
  82. :return:
  83. """
  84. search_result = await self.search_method.search_v1(
  85. text=self.article_keys[1],
  86. trace_id=self.trace_id
  87. )
  88. if search_result:
  89. return search_result
  90. else:
  91. return await self.base_search()
  92. async def ab_4(self):
  93. """
  94. 使用文本关键词搜索
  95. :return:
  96. """
  97. search_result = await self.search_method.search_v1(
  98. text=self.article_keys[2],
  99. trace_id=self.trace_id
  100. )
  101. if search_result:
  102. return search_result
  103. else:
  104. return await self.base_search()
  105. async def ab_5(self):
  106. """
  107. 增量搜索, 返回result_list
  108. :return:
  109. """
  110. result_list = await self.search_method.search_v2(
  111. text=self.article_summary[:15],
  112. trace_id=self.trace_id
  113. )
  114. if len(result_list) > 3:
  115. return result_list
  116. else:
  117. result_list += await self.search_method.search_v2(
  118. text=self.ori_title[:15],
  119. trace_id=self.trace_id
  120. )
  121. if len(result_list) > 3:
  122. return result_list
  123. else:
  124. result_list += await self.search_method.search_v2(
  125. text=self.article_keys[0],
  126. trace_id=self.trace_id
  127. )
  128. if len(result_list) > 3:
  129. return result_list
  130. else:
  131. result_list += await self.search_method.search_v2(
  132. text=self.article_keys[1],
  133. trace_id=self.trace_id
  134. )
  135. if result_list:
  136. return result_list
  137. else:
  138. result_list += await self.search_method.search_v2(
  139. text=self.article_keys[2],
  140. trace_id=self.trace_id
  141. )
  142. return result_list