accountServer.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import aiohttp
  6. from applications.articleTools import ArticleDBTools
  7. class AccountServer(object):
  8. """
  9. 获取标题和公众号文章的相关性
  10. """
  11. def __init__(self, mysql_client, params):
  12. self.account_name_list = None
  13. self.sim_type = None
  14. self.interest_type = None
  15. self.min_time = None
  16. self.max_time = None
  17. self.rate = None
  18. self.title_list = None
  19. self.params = params
  20. self.AT = ArticleDBTools(mysql_client)
  21. async def request_for_nlp(self, title_list, account_interest, account_weight):
  22. """
  23. nlp process
  24. """
  25. headers = {"Content-Type": "application/json"}
  26. url = "http://localhost:6061/nlp"
  27. body = {
  28. "data": {
  29. "text_list_a": [i.replace("'", "") for i in title_list],
  30. "text_list_b": [i.replace("'", "") for i in account_interest],
  31. "score_list_b": account_weight,
  32. "symbol": 1,
  33. },
  34. "function": "similarities_cross_mean" if self.sim_type == "mean" else "similarities_cross_avg"
  35. }
  36. async with aiohttp.ClientSession() as session:
  37. async with session.post(url, headers=headers, json=body) as response:
  38. response_text = await response.text()
  39. if response_text:
  40. return await response.json()
  41. else:
  42. print("Received empty response")
  43. return {}
  44. def checkParams(self):
  45. """
  46. 校验传参
  47. :return:
  48. """
  49. try:
  50. self.title_list = self.params["text_list"]
  51. self.account_name_list = self.params.get("account_nickname_list", [])
  52. self.rate = self.params.get("rate", 0.1)
  53. self.max_time = self.params.get("max_time")
  54. self.min_time = self.params.get("min_time")
  55. self.interest_type = self.params.get("interest_type", "top")
  56. self.sim_type = self.params.get("sim_type", "mean")
  57. return None
  58. except Exception as e:
  59. response = {"error": "Params error", "detail": str(e)}
  60. return response
  61. async def getAccountInterest(
  62. self,
  63. account_name,
  64. method,
  65. rate=None,
  66. msg_type=None,
  67. index_list=None,
  68. min_time=None,
  69. max_time=None,
  70. ):
  71. """
  72. 获取账号的兴趣类型
  73. :param account_name:
  74. :param max_time:
  75. :param min_time:
  76. :param index_list:
  77. :param msg_type:
  78. :param keys_dict:
  79. :param rate:
  80. :param gh_id:
  81. :param method:
  82. :return:
  83. """
  84. good_df, bad_df = await self.AT.get_good_bad_articles(
  85. account_name=account_name,
  86. method=method,
  87. msg_type=msg_type,
  88. index_list=index_list,
  89. min_time=min_time,
  90. max_time=max_time,
  91. rate=rate,
  92. )
  93. view_count_list = good_df["show_view_count"].values.tolist()
  94. title_list = good_df["title"].values.tolist()
  95. print(view_count_list)
  96. print(title_list)
  97. return title_list, view_count_list
  98. async def getEachAccountScoreList(self, account_name):
  99. """
  100. 获取和单个账号的相关性分数
  101. :return:
  102. """
  103. try:
  104. account_interest, account_weight = await self.getAccountInterest(
  105. account_name=account_name,
  106. method=self.interest_type,
  107. rate=self.rate
  108. )
  109. sim_key = "score_list_mean" if self.sim_type == "mean" else "score_list_avg"
  110. response = await self.request_for_nlp(
  111. title_list=self.title_list,
  112. account_interest=account_interest,
  113. account_weight=account_weight
  114. )
  115. return {
  116. "score_list": response[sim_key],
  117. "text_list_max": response["text_list_max"],
  118. }
  119. except Exception as e:
  120. print(e)
  121. return {
  122. "score_list": [0] * len(self.title_list),
  123. "text_list_max": self.title_list,
  124. }
  125. async def getAccountListScoreList(self):
  126. """
  127. 获取AccountList中每一个账号的相关性分数
  128. :return:
  129. """
  130. response = {}
  131. for accountName in self.account_name_list:
  132. if response.get(accountName):
  133. continue
  134. else:
  135. response[accountName] = await self.getEachAccountScoreList(account_name=accountName)
  136. return response
  137. async def deal(self):
  138. """
  139. Deal Function
  140. :return:
  141. """
  142. return (
  143. self.checkParams() if self.checkParams() else await self.getAccountListScoreList()
  144. )