Commit 48180b6c authored by 高雅喆's avatar 高雅喆

update

parent 035053a0
......@@ -147,73 +147,73 @@ def compute_ai_scan(x):
def get_user_tag_score(cl_id, all_word_tags, size=10):
# try:
db_jerry_test = pymysql.connect(host='172.16.40.158', port=4000, user='root', passwd='3SYz54LS9#^9sBvC',
db='jerry_test', charset='utf8')
cur_jerry_test = db_jerry_test.cursor()
# 用户的非搜索行为
user_df_service_sql = "select time,cl_id,score_type,tag_id,tag_referrer,action from user_new_tag_log " \
"where cl_id ='{}' and action != 'do_search' ".format(cl_id)
cur_jerry_test.execute(user_df_service_sql)
data = list(cur_jerry_test.fetchall())
if data:
user_log_df = pd.DataFrame(data)
user_log_df.columns = ["time", "cl_id", "score_type", "tag_id", "tag_referrer", "action"]
else:
user_log_df = pd.DataFrame(columns=["time", "cl_id", "score_type", "tag_id", "tag_referrer", "action"])
# 用户的搜索行为
user_df_search_sql = "select time,cl_id,score_type,tag_id,tag_referrer,action from user_new_tag_log " \
"where cl_id ='{}' and action = 'do_search'".format(cl_id)
cur_jerry_test.execute(user_df_search_sql)
data_search = list(cur_jerry_test.fetchall())
if data_search:
user_df_search = pd.DataFrame(data_search)
user_df_search.columns = ["time", "cl_id", "score_type", "tag_id", "tag_referrer", "action"]
else:
user_df_search = pd.DataFrame(columns=["time", "cl_id", "score_type", "tag_id", "tag_referrer", "action"])
# 搜索词转成tag
for index, row in user_df_search.iterrows():
if row['tag_referrer'] in all_word_tags:
for search_tag in all_word_tags[row['tag_referrer']]:
row['tag_id'] = int(search_tag)
user_log_df = user_log_df.append(row, ignore_index=True)
break
if not user_log_df.empty:
user_log_df["days_diff_now"] = round((int(time.time())-user_log_df["time"]) / (24*60*60))
user_log_df["score"] = user_log_df.apply(
lambda x: compute_henqiang(x.days_diff_now) if x.score_type == "henqiang" else (
compute_jiaoqiang(x.days_diff_now) if x.score_type == "jiaoqiang" else (
compute_ai_scan(x.days_diff_now) if x.score_type == "ai_scan" else (
compute_ruoyixiang(x.days_diff_now) if x.score_type == "ruoyixiang" else compute_validate(x.days_diff_now)))), axis=1)
finally_score = user_log_df.sort_values(by=["score","time"],ascending=False)
finally_score.drop_duplicates(subset="tag_id", inplace=True)
finally_score["weight"] = finally_score['score'] / finally_score['score'].sum()
finally_score["pay_type"] = finally_score.apply(
lambda x: 3 if x.action == "api/order/validate" else (
2 if x.action == "api/settlement/alipay_callback" else 1
), axis=1
)
score_result = finally_score[["tag_id", "cl_id", "score", "weight", "pay_type"]]
score_result.rename(columns={"cl_id": "device_id"}, inplace=True)
# 写tidb
delete_sql = "delete from api_market_personas where device_id='{}'".format(cl_id)
cur_jerry_test.execute(delete_sql)
db_jerry_test.commit()
for index, row in score_result.iterrows():
insert_sql = "insert into api_market_personas values (null, {}, '{}', {}, {}, {})".format(
row['tag_id'], row['device_id'], row['score'], row['weight'], row['pay_type'])
cur_jerry_test.execute(insert_sql)
try:
db_jerry_test = pymysql.connect(host='172.16.40.158', port=4000, user='root', passwd='3SYz54LS9#^9sBvC',
db='jerry_test', charset='utf8')
cur_jerry_test = db_jerry_test.cursor()
# 用户的非搜索行为
user_df_service_sql = "select time,cl_id,score_type,tag_id,tag_referrer,action from user_new_tag_log " \
"where cl_id ='{}' and action != 'do_search' ".format(cl_id)
cur_jerry_test.execute(user_df_service_sql)
data = list(cur_jerry_test.fetchall())
if data:
user_log_df = pd.DataFrame(data)
user_log_df.columns = ["time", "cl_id", "score_type", "tag_id", "tag_referrer", "action"]
else:
user_log_df = pd.DataFrame(columns=["time", "cl_id", "score_type", "tag_id", "tag_referrer", "action"])
# 用户的搜索行为
user_df_search_sql = "select time,cl_id,score_type,tag_id,tag_referrer,action from user_new_tag_log " \
"where cl_id ='{}' and action = 'do_search'".format(cl_id)
cur_jerry_test.execute(user_df_search_sql)
data_search = list(cur_jerry_test.fetchall())
if data_search:
user_df_search = pd.DataFrame(data_search)
user_df_search.columns = ["time", "cl_id", "score_type", "tag_id", "tag_referrer", "action"]
else:
user_df_search = pd.DataFrame(columns=["time", "cl_id", "score_type", "tag_id", "tag_referrer", "action"])
# 搜索词转成tag
for index, row in user_df_search.iterrows():
if row['tag_referrer'] in all_word_tags:
for search_tag in all_word_tags[row['tag_referrer']]:
row['tag_id'] = int(search_tag)
user_log_df = user_log_df.append(row, ignore_index=True)
break
if not user_log_df.empty:
user_log_df["days_diff_now"] = round((int(time.time())-user_log_df["time"]) / (24*60*60))
user_log_df["score"] = user_log_df.apply(
lambda x: compute_henqiang(x.days_diff_now) if x.score_type == "henqiang" else (
compute_jiaoqiang(x.days_diff_now) if x.score_type == "jiaoqiang" else (
compute_ai_scan(x.days_diff_now) if x.score_type == "ai_scan" else (
compute_ruoyixiang(x.days_diff_now) if x.score_type == "ruoyixiang" else compute_validate(x.days_diff_now)))), axis=1)
finally_score = user_log_df.sort_values(by=["score","time"],ascending=False)
finally_score.drop_duplicates(subset="tag_id", inplace=True)
finally_score["weight"] = finally_score['score'] / finally_score['score'].sum()
finally_score["pay_type"] = finally_score.apply(
lambda x: 3 if x.action == "api/order/validate" else (
2 if x.action == "api/settlement/alipay_callback" else 1
), axis=1
)
score_result = finally_score[["tag_id", "cl_id", "score", "weight", "pay_type"]]
score_result.rename(columns={"cl_id": "device_id"}, inplace=True)
# 写tidb
delete_sql = "delete from api_market_personas where device_id='{}'".format(cl_id)
cur_jerry_test.execute(delete_sql)
db_jerry_test.commit()
db_jerry_test.close()
return "sucess"
else:
return "user log is empty"
# except Exception as e:
# return 'pass'
for index, row in score_result.iterrows():
insert_sql = "insert into api_market_personas values (null, {}, '{}', {}, {}, {})".format(
row['tag_id'], row['device_id'], row['score'], row['weight'], row['pay_type'])
cur_jerry_test.execute(insert_sql)
db_jerry_test.commit()
db_jerry_test.close()
return "sucess"
else:
return "user log is empty"
except Exception as e:
return 'pass'
if __name__ == '__main__':
......
......@@ -226,7 +226,6 @@ def get_user_service_portrait(cl_id, all_word_tags, all_tag_tag_type, all_3tag_2
user_df_search.columns = ["time", "cl_id", "score_type", "tag_id", "tag_referrer", "action"]
else:
user_df_search = pd.DataFrame(columns=["time", "cl_id", "score_type", "tag_id", "tag_referrer", "action"])
db_jerry_test.close()
# 搜索词转成tag
# user_df_search_2_tag = pd.DataFrame(columns=list(user_df_service.columns))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment