juanboy

舆情函数定义

  1 +from utils.query import query
  2 +from utils.getPublicData import *
  3 +articleList = getAllArticleData()
  4 +commentList = getAllCommentsData()
  5 +
  6 +def column_exists(table_name, column_name):
  7 + sql = "SHOW COLUMNS FROM {} LIKE %s".format(table_name)
  8 + params = [column_name]
  9 + result = query(sql, params, type='select')
  10 + return len(result) > 0
  11 +# 添加新的标注列
  12 +def add_label_article(): # 为文章添加标注列
  13 + if not column_exists('article', 'label'):
  14 + sql = "ALTER TABLE article ADD COLUMN label TEXT NULL"
  15 + params = []
  16 + query(sql, params)
  17 +
  18 +def add_label_comments(): # 为评论添加标注列
  19 + if not column_exists('comments', 'label'):
  20 + sql = "ALTER TABLE comments ADD COLUMN label TEXT NULL"
  21 + params = []
  22 + query(sql, params)
  23 +
  24 +def drop_label():# 删除标注列
  25 + sql = "ALTER TABLE article DROP COLUMN label "
  26 + params = []
  27 + query(sql, params)
  28 +
  29 +def drop_label1():# 删除标注列
  30 + sql = "ALTER TABLE comments DROP COLUMN label "
  31 + params = []
  32 + query(sql, params)
  33 +
  34 +
  35 +# 处理数据并添加标注
  36 +def process_data(data):
  37 + processed_data =testmodel(data)
  38 + return processed_data
  39 +
  40 +def testmodel(test):
  41 + return "wicao"
  42 +
  43 +def topicdefine():
  44 + label_article=[]
  45 + label_comments=[]
  46 + for x in articleList:
  47 + label_article.append((x[0],process_data(x[5])))
  48 + for x in commentList:
  49 + label_comments.append((x[5],process_data(x[4])))
  50 + return label_article,label_comments
  51 +
  52 +# 更新数据库
  53 +def update_data(label_article,label_comments):
  54 + add_label_comments()
  55 + add_label_article()
  56 + for row in label_article:
  57 + id, label = row
  58 + sql = "UPDATE article SET label = %s WHERE id = %s"
  59 + params = [str(label),str(id)]
  60 + query(sql, params)
  61 + for row in label_comments:
  62 + label, id = row
  63 + sql = "UPDATE comments SET label = %s WHERE authorName = %s"
  64 + params = [str(label),str(id)]
  65 + query(sql, params)
  66 +
  67 +# x,y=topicdefine()
  68 +# update_data(x,y)
  69 +drop_label()
  70 +drop_label1()
@@ -5,7 +5,7 @@ def getTopicLen(topic):# 统计特定话题下的评论数目 @@ -5,7 +5,7 @@ def getTopicLen(topic):# 统计特定话题下的评论数目
5 topic_len = 0 5 topic_len = 0
6 for i in commentsList: 6 for i in commentsList:
7 if i[9] == topic: 7 if i[9] == topic:
8 - topicLen+=1 8 + topic_len+=1
9 return topic_len 9 return topic_len
10 10
11 def getTopicPageCreatedAtCharData(topic):# 统计特定话题的评论在每个日期的数量,并返回日期和对应的评论数量 11 def getTopicPageCreatedAtCharData(topic):# 统计特定话题的评论在每个日期的数量,并返回日期和对应的评论数量