accountServer.py 5.2 KB

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