123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- """
- @author: luojunhui
- """
- import requests
- from applications.articleTools import ArticleDBTools
- class AccountServer(object):
- """
- 获取标题和公众号文章的相关性
- """
- def __init__(self, mysql_client, params):
- self.account_name_list = None
- self.sim_type = None
- self.interest_type = None
- self.min_time = None
- self.max_time = None
- self.rate = None
- self.title_list = None
- self.params = params
- self.AT = ArticleDBTools(mysql_client)
- async def request_for_nlp(self, title_list, account_interest, account_weight):
- """
- nlp process
- """
- headers = {"Content-Type": "application/json"}
- url = "http://localhost:6060/nlp"
- body = {
- "data": {
- "text_list_a": title_list,
- "text_list_b": account_interest,
- "score_list_b": account_weight,
- "symbol": 1,
- },
- "function": (
- "similarities_cross_mean"
- if self.sim_type == "mean"
- else "similarities_cross_avg"
- ),
- }
- response = requests.post(url=url, headers=headers, json=body)
- return response
- def checkParams(self):
- """
- 校验传参
- :return:
- """
- try:
- self.title_list = self.params["text_list"]
- self.account_name_list = self.params.get("account_nickname_list", [])
- self.rate = self.params.get("rate", 0.1)
- self.max_time = self.params.get("max_time")
- self.min_time = self.params.get("min_time")
- self.interest_type = self.params.get("interest_type", "top")
- self.sim_type = self.params.get("sim_type", "mean")
- return None
- except Exception as e:
- response = {"error": "Params error", "detail": str(e)}
- return response
- async def getAccountInterest(
- self,
- account_name,
- method,
- rate=None,
- msg_type=None,
- index_list=None,
- min_time=None,
- max_time=None,
- ):
- """
- 获取账号的兴趣类型
- :param account_name:
- :param max_time:
- :param min_time:
- :param index_list:
- :param msg_type:
- :param keys_dict:
- :param rate:
- :param gh_id:
- :param method:
- :return:
- """
- good_df, bad_df = await self.AT.get_good_bad_articles(
- account_name=account_name,
- method=method,
- msg_type=msg_type,
- index_list=index_list,
- min_time=min_time,
- max_time=max_time,
- rate=rate,
- )
- view_count_list = good_df["show_view_count"]
- title_list = good_df["title"]
- return title_list, view_count_list
- async def getEachAccountScoreList(self, account_name):
- """
- 获取和单个账号的相关性分数
- :return:
- """
- # try:
- account_interest, account_weight = await self.getAccountInterest(
- account_name=account_name,
- method=self.interest_type,
- )
- response = await self.request_for_nlp(
- title_list=self.title_list,
- account_interest=account_interest,
- account_weight=account_weight,
- )
- res = response.json()
- sim_key = "score_list_mean" if self.sim_type == "mean" else "score_list_avg"
- return {
- "score_list": res[sim_key],
- "text_list_max": res["text_list_max"],
- }
- # except Exception as e:
- # print(e)
- # return {
- # "score_list": [0] * len(self.title_list),
- # "text_list_max": self.title_list,
- # }
- async def getAccountListScoreList(self):
- """
- 获取AccountList中每一个账号的相关性分数
- :return:
- """
- response = {}
- for accountName in self.account_name_list:
- if response.get(accountName):
- continue
- else:
- response[accountName] = await self.getEachAccountScoreList(account_name=accountName)
- return response
- async def deal(self):
- """
- Deal Function
- :return:
- """
- return (
- self.checkParams() if self.checkParams() else await self.getAccountListScoreList()
- )
|