话题水军识别应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
12 KiB

  1. #coding:utf8
  2. import re
  3. import pymysql
  4. import pandas as pd
  5. import numpy as np
  6. import networkx as nx
  7. import traceback
  8. import json
  9. from jsonpath_ng import jsonpath, parse
  10. def parse_data(raw_data,url):
  11. val = None
  12. try:
  13. if "#json#" in url:
  14. parm = url.split("#")
  15. data1 = parse_data(raw_data, parm[0])
  16. data1_json = json.loads(data1)
  17. expr = parse(parm[2])
  18. match = [match.value for match in expr.find(data1_json)]
  19. val = match[0]
  20. else:
  21. all_result = raw_data['data']
  22. param_split = str(url).split(":")
  23. datasourcestr = all_result[param_split[0]]
  24. datasource = json.loads(datasourcestr)
  25. # 创建 JsonPath 表达式对象
  26. expr = parse(param_split[1])
  27. # 使用表达式来选择 JSON 元素
  28. match = [match.value for match in expr.find(datasource)]
  29. val = match[0]
  30. except Exception as e:
  31. traceback.print_exc()
  32. val = ''
  33. return val
  34. def get_taskId(raw_data):
  35. taskid = raw_data["metadata"]["admin"]["reply_file"]["taskId"]
  36. all_result = raw_data['data']
  37. param_split = taskid.split(":")
  38. datasourcestr = all_result[param_split[0]]
  39. datasource = json.loads(datasourcestr)
  40. # 创建 JsonPath 表达式对象
  41. expr = parse(param_split[1])
  42. # 使用表达式来选择 JSON 元素
  43. match = [match.value for match in expr.find(datasource)]
  44. val = match[0]
  45. return val
  46. def mysqlData(dbConfig,taskId,logging):
  47. result=''
  48. try:
  49. # taskId=get_taskId(raw_data)
  50. db = pymysql.connect(host=dbConfig["host"], user=dbConfig["username"], passwd=dbConfig["password"],
  51. db=dbConfig["db"], port=int(dbConfig["port"]), charset='utf8',cursorclass=pymysql.cursors.DictCursor, connect_timeout=30)
  52. db.ping(reconnect=True)
  53. cursor = db.cursor()
  54. # sql="SELECT ReviewerAccountId, PostAccountId FROM {} WHERE topicId={}".format(inputdata["table"],inputdata["topicId"])
  55. sql="select a.ReviewerAccountId,a.ReviewerAccountName,b.accountId PostAccountId,b.accountName PostAccountName,a.ShareCount,a.LikeCount,a.CommentCount,a.CommentTime from reply a LEFT JOIN user_post b on a.postId = b.postId where a.taskId = {}".format(taskId)
  56. cursor.execute(sql)
  57. result = cursor.fetchall()
  58. db.commit()
  59. cursor.close()
  60. db.close()
  61. except:
  62. logging.info("专题关系数据查询失败!")
  63. logging.info(traceback.format_exc())
  64. return result
  65. def get_replyData(data):
  66. reply=pd.DataFrame(data)
  67. reply = reply.drop_duplicates().reset_index(drop=True) # 去重
  68. reply=reply[['ReviewerAccountId', 'PostAccountId']]
  69. # reply.columns = ['ReviewerAccountId', 'ReviewerAccountName', 'PostAccountId', 'PostAccountName',
  70. # 'ShareCount', 'LikeCount', 'CommentCount', 'CommentTime']
  71. reply = reply[['ReviewerAccountId', 'PostAccountId']]
  72. reply['ReviewerAccountId'] = reply['ReviewerAccountId'].astype(str)
  73. reply['PostAccountId'] = reply['PostAccountId'].astype(str)
  74. reply = reply.groupby(['ReviewerAccountId', 'PostAccountId']).size().reset_index()
  75. # user_net_df = user_net(reply) ##SNA数据清洗
  76. edgeweightset = reply.fillna(0)
  77. edgeweightset.columns = ['source', 'target', 'count']
  78. edgeweightset_l = [[] for _ in range(edgeweightset.shape[0])]
  79. for i in range(len(edgeweightset_l)):
  80. for j in range(edgeweightset.shape[1]):
  81. edgeweightset_l[i].append(edgeweightset.iloc[i, j])
  82. g = nx.DiGraph()
  83. g.add_weighted_edges_from(edgeweightset_l)
  84. degree = [g.degree(),
  85. g.in_degree(),
  86. g.out_degree()]
  87. centrality = [nx.degree_centrality(g), # 计算图 g 中每个节点的度中心性。度中心性是指节点的度(与其他节点相连的边的数量)与图中节点总数的比值。
  88. nx.closeness_centrality(g), # 计算图 g 中每个节点的接近中心性。接近中心性是指节点到其他节点的平均最短路径长度的倒数。
  89. nx.pagerank(g), # 计算图 g 中每个节点的 PageRank 值。PageRank 是一种用于评估网页重要性的算法,也可以应用于其他网络中的节点重要性评估。
  90. nx.clustering(g)] # 计算图 g 中每个节点的聚集系数。聚集系数是指节点的邻居之间存在连接的概率。
  91. #把主贴相关信息拿出来
  92. tmp=edgeweightset["target"].values
  93. node_list = []
  94. nodes = g.nodes() # 提取网络中节点列表
  95. for node in nodes:
  96. if node not in tmp:
  97. continue
  98. node_list.append([node,
  99. degree[0][node],
  100. degree[1][node],
  101. degree[2][node],
  102. centrality[0][node],
  103. centrality[1][node],
  104. centrality[2][node],
  105. centrality[3][node]])
  106. node_list = pd.DataFrame(node_list)
  107. node_list.columns = ['Id', 'degree', 'in_degree', 'out_degree',
  108. 'degree_centrality', 'closeness_centrality', 'pagerank', 'clustering']
  109. node_list['user_flag_infl'] = 0
  110. node_list['user_flag_act'] = 0
  111. node_list.user_flag_infl[node_list['out_degree'] > np.percentile(node_list['out_degree'], 95)] = 1
  112. node_list.user_flag_act[(node_list['in_degree'] > np.percentile(node_list['in_degree'], 90)) &
  113. (node_list['closeness_centrality'] > np.percentile(node_list['closeness_centrality'],
  114. 50))] = 1
  115. node_dic=node_list.set_index('Id')[['degree', 'in_degree','out_degree','degree_centrality','closeness_centrality','pagerank','clustering']].T.to_dict()
  116. return node_dic
  117. def get_content(inputdata,logging):
  118. """
  119. :param inputdata:json数据
  120. :return: prompt及其他参数
  121. """
  122. res={}
  123. admin=inputdata["metadata"]["admin"]
  124. data=inputdata["data"]
  125. prompt=admin["prompt"]
  126. if_user=re.findall("{{(.*)}}",prompt)
  127. if_data=re.findall("@@(.*)@@",prompt)
  128. if if_user != []:
  129. user_data=inputdata["metadata"]["user"]
  130. if if_user[0] in user_data.keys():
  131. tmp=user_data[if_user[0]]
  132. prompt=re.sub("{{(.*)}}",tmp,prompt)
  133. if if_data!=[] and if_data[0] in data.keys():
  134. tmp1=data[if_data[0]]
  135. prompt=re.sub("@@(.*)@@",tmp1,prompt)
  136. res["prompt"]=prompt
  137. res["authorization"]=admin["authorization"]
  138. res["model"]=admin["model"]
  139. res["temperature"]=admin["temperature"]
  140. res["authorization"]=admin["authorization"]
  141. res["top_p"]=admin["top_p"]
  142. res["n"]=admin["n"]
  143. return res
  144. if __name__=="__main__":
  145. inputdata={
  146. "metadata":{
  147. "output":{
  148. "output_type":"table",
  149. "label_col":[
  150. "软件著作抽取结果"
  151. ]
  152. },
  153. "input":{
  154. "input_type":"text",
  155. "label":[
  156. "7_软件著作过滤器"
  157. ]
  158. },
  159. "address":"http://172.18.1.181:9011/chatGpt/",
  160. "admin":{
  161. "user_file": "12_任务拆分",
  162. "post_file": "13_获取发帖信息",
  163. "reply_file": {
  164. "taskId": "1_twitter采集:$.taskId",
  165. "host": "172.24.12.126",
  166. "user": "root",
  167. "passwd": "baifendian123",
  168. "db": "analyze",
  169. "port": 3306
  170. }
  171. },
  172. "index":1
  173. },
  174. "data":{
  175. "sgwg":"[{ \"taskId\":\"http://172.18.1.130:9985/group33/default/20230816/16/05/1/1-基于时间序列遥感 影像洪涝检测系统.jpg\",\"fileType\":\"jpg\", \"filePath\":\"/软件著作/1-基于时间序列遥感 影像洪涝检测系统.jpg\",\"fileId\":\"cd6592f0389bb1da25afbb44901f9cde\",\"fileName\":\"1-基于时间序列遥感 影像洪涝检测系统.jpg\" },{ \"fileUrl\":\"http://172.18.1.130:9985/group33/default/20230816/16/06/1/2-基于遥感影像的快速变化检测系统.jpg\",\"fileType\":\"jpg\", \"filePath\":\"/软件著作/2-基于遥感影像的快速变化检测系统.jpg\",\"fileId\":\"338847e34904fa96e8834cb220667db8\",\"fileName\":\"2-基于遥感影像的快速变化检测系统.jpg\" },{ \"fileUrl\":\"http://172.18.1.130:9985/group33/default/20230816/16/08/1/3-基于时空模型的遥感时间序列森林火灾检测系统.jpg\",\"fileType\":\"jpg\", \"filePath\":\"/软件著作/1/3-基于时空模型的遥感时间序列森林火灾检测系统.jpg\",\"fileId\":\"944eec1cf98f216ea953459dac4dd505\",\"fileName\":\"3-基于时空模型的遥感时间序列森林火灾检测系统.jpg\" },{ \"fileUrl\":\"http://172.18.1.130:9985/group33/default/20230816/16/09/1/4-基于隐马尔可夫模型的遥感时间序列分类系统.jpg\",\"fileType\":\"jpg\", \"filePath\":\"/软件著作/4-基于隐马尔可夫模型的遥感时间序列分类系统.jpg\",\"fileId\":\"eb378cb9ee914323f601500378dfad76\",\"fileName\":\"4-基于隐马尔可夫模型的遥感时间序列分类系统.jpg\" }]",
  176. "1_twitter采集":"{\"taskId\":100}",
  177. "3_OCR识别内容":"{\"content\":\" 22222222222222222222222222222222222222222222222222\\n中华人民共和国国家版权局\\n计算机软件著作权登记证书\\n证书号:软著登字第1623261号\\n软件名称:\\n基于遥感影像的快速变化检测系统\\nV1.0\\n著作权人:中国科学院遥感与数字地球研究所\\n开发完成日期:2016年08月01日\\n首次发表日期:未发表\\n权利取得方式:原始取得\\n权利范围:全部权利\\n登记号:2017SR037977\\n根据《计算机软件保护条例》和《计算机软件著作权登记办法》的\\n规定,经中国版权保护中心审核,对以上事项予以登记\\n计算机软件著作权\\n登记专用章\\n2017年02月10日\\nNo.01433672\",\"fileId\":\"338847e34904fa96e8834cb220667db8\",\"fileName\":\"2-基于遥感影像的快速变化检测系统.jpg\",\"filePath\":\"/软件著作/2-基于遥感影像的快速变化检测系统.jpg\",\"fileType\":\"jpg\",\"fileUrl\":\"http://172.18.1.130:9985/group33/default/20230816/16/06/1/2-基于遥感影像的快速变化检测系统.jpg\",\"pageNum\":1}",
  178. "businessKey":"185aef3b1c810799a6be8314abf6512c",
  179. "7_软件著作过滤器":"{\"content\":\" 22222222222222222222222222222222222222222222222222\\n中华人民共和国国家版权局\\n计算机软件著作权登记证书\\n证书号:软著登字第1623261号\\n软件名称:\\n基于遥感影像的快速变化检测系统\\nV1.0\\n著作权人:中国科学院遥感与数字地球研究所\\n开发完成日期:2016年08月01日\\n首次发表日期:未发表\\n权利取得方式:原始取得\\n权利范围:全部权利\\n登记号:2017SR037977\\n根据《计算机软件保护条例》和《计算机软件著作权登记办法》的\\n规定,经中国版权保护中心审核,对以上事项予以登记\\n计算机软件著作权\\n登记专用章\\n2017年02月10日\\nNo.01433672\",\"fileId\":\"338847e34904fa96e8834cb220667db8\",\"fileName\":\"2-基于遥感影像的快速变化检测系统.jpg\",\"filePath\":\"/软件著作/2-基于遥感影像的快速变化检测系统.jpg\",\"fileType\":\"jpg\",\"fileUrl\":\"http://172.18.1.130:9985/group33/default/20230816/16/06/1/2-基于遥感影像的快速变化检测系统.jpg\",\"pageNum\":1}"
  180. },
  181. "created":1691004265000,
  182. "module":"OCR",
  183. "start_tag":"false",
  184. "last_edit":1692464331000,
  185. "next_app_id":[
  186. {
  187. "start_id":86,
  188. "edge_id":49,
  189. "end_id":90
  190. }
  191. ],
  192. "transfer_id":11,
  193. "blueprint_id":3,
  194. "scenes_id":3,
  195. "scenario":{
  196. "dataloss":1,
  197. "autoCommitTriggerLast":1,
  198. "maxErrors":3,
  199. "autoCommit":1,
  200. "freshVariables":1
  201. },
  202. "wait_condition":[
  203. ],
  204. "scheduling":{
  205. "interval":-1,
  206. "type":"single"
  207. },
  208. "name":"软件著作抽取",
  209. "businessKey":"185aef3b1c810799a6be8314abf6512c",
  210. "id":86,
  211. "describe":"软件著作抽取"
  212. }
  213. a=get_taskId(inputdata)
  214. print(a)