diff --git a/utils/cipingTotal.py b/utils/cipingTotal.py index 9e5e975..54931c3 100644 --- a/utils/cipingTotal.py +++ b/utils/cipingTotal.py @@ -23,9 +23,10 @@ def main(): # 格式整理 list_count = sorted(word_count.items(),key=lambda x:x[1],reverse=True) - + # list_count = [[key, str(value)] for key, value in list_count] + # return list_count for i in range(100): print(list_count[i],file=result) if __name__ == '__main__': - main() \ No newline at end of file + print(main()) \ No newline at end of file diff --git a/utils/getPublicData.py b/utils/getPublicData.py index 829978b..f24ca25 100644 --- a/utils/getPublicData.py +++ b/utils/getPublicData.py @@ -92,4 +92,6 @@ def getAllHotWords(): def getAllTopics(): data =[] - return data \ No newline at end of file + return data +if __name__ == '__main__': + print(getAllHotWords()) \ No newline at end of file diff --git a/utils/yuqingpredict.py b/utils/yuqingpredict.py index e78dd1e..f9feaab 100644 --- a/utils/yuqingpredict.py +++ b/utils/yuqingpredict.py @@ -44,15 +44,22 @@ def mergeTopics(article_topics, comment_topics):# 合并话题 merged_dict[topic['name']] += topic['value'] else: merged_dict[topic['name']] = topic['value'] - merged_list = [{'name': key, 'value': value} for key, value in merged_dict.items()] + merged_dict = sorted(merged_dict.items(), key=lambda item: item[1], reverse=True) + merged_list = [[key, str(value)] for key, value in merged_dict] return merged_list -def getTopicData(): - # 读取合并文件 merge.csv # 取前十个话题 - top_10_topics = pd.read_csv('./merged_topics.csv').head(10) - # 获取话题名称和对应的值 - X = top_10_topics['name'].tolist() - Y = top_10_topics['value'].tolist() - return X, Y +def getAllTopicData(): + # 读取合并文件 merge.csv + # data = [] + # df = pd.read_csv('./merged_topics.csv',encoding='utf8') + # for i in df.values: + # try: + # data.append([ + # re.search('[\u4e00-\u9fa5]+',str(i)).group(), + # re.search('\d+',str(i)).group() + # ]) + # except: + # continue + return mergeTopics(getTopicByArticle(), getTopicByComments()) def getTopicCreatedAtandpredictData(topic):# 统计特定话题的评论在每个日期的数量,并返回日期和对应的评论数量 createdAt = {} @@ -71,9 +78,9 @@ def getTopicCreatedAtandpredictData(topic):# 统计特定话题的评论在每 createdAt = {k: createdAt[k] for k in sorted(createdAt, key=lambda date: datetime.datetime.strptime(date, "%Y-%m-%d"))} createdAt.update(predict_future_values(createdAt)) sorted_data = {k: createdAt[k] for k in sorted(createdAt, key=lambda date: datetime.datetime.strptime(date, "%Y-%m-%d"))} - result_list = [0] * (len(sorted_data) - 5) + [1] * 5 - return topic,sorted_data,result_list - # return topic,list(createdAt.keys()),list(createdAt.values()),result_list + # result_list = [0] * (len(sorted_data) - 5) + [1] * 5 + print(list(createdAt.keys()),list(createdAt.values())) + return list(createdAt.keys()),list(createdAt.values()) def writeTopicsToCSV(topics, file_name): # 检查文件是否存在,如果存在则附加写入,否则新建一个 @@ -91,6 +98,7 @@ def writeTopicsToCSV(topics, file_name): writer.writerow(topic) if __name__ == '__main__': # 将话题数据写入 CSV 文件 - # merged_topics = mergeTopics(getTopicByArticle(), getTopicByComments()) + # print(mergeTopics(getTopicByArticle(), getTopicByComments())) # writeTopicsToCSV(merged_topics, 'merged_topics.csv') - print(getTopicCreatedAtandpredictData("生活")) + print(getAllTopicData()) + diff --git a/views/page/page.py b/views/page/page.py index c3d684c..260534f 100644 --- a/views/page/page.py +++ b/views/page/page.py @@ -6,6 +6,8 @@ from utils.getTableData import * from utils.getPublicData import getAllHotWords, getAllTopics from utils.getEchartsData import * from utils.getTopicPageData import * +from utils.yuqingpredict import * +from utils.getPublicData import getAllHotWords pb = Blueprint('page', __name__, @@ -38,6 +40,7 @@ def home(): def hotWord(): username = session.get('username') hotWordList = getAllHotWords() + print(hotWordList) defaultHotWord = hotWordList[0][0] if request.args.get('hotWord'): defaultHotWord = request.args.get('hotWord') @@ -51,7 +54,6 @@ def hotWord(): sentences = '正面' elif value < 0.5: sentences = '负面' - comments = getCommentFilterData(defaultHotWord) return render_template('hotWord.html', username=username, @@ -161,6 +163,34 @@ def yuqingChar(): x1Data=x1Data, y1Data=y1Data) +@pb.route('/yuqingpredict') +def yuqingpredict(): + username = session.get('username') + TopicList = getAllTopicData() + defaultTopic = TopicList[0][0] + if request.args.get('Topic'): + defaultTopic = request.args.get('Topic') + TopicLen = getTopicLen(defaultTopic) + X, Y = getTopicCreatedAtandpredictData(defaultTopic) + sentences = '' + value = SnowNLP(defaultTopic).sentiments + if value == 0.5: + sentences = '中性' + elif value > 0.5: + sentences = '正面' + elif value < 0.5: + sentences = '负面' + comments = getCommentFilterDataTopic(defaultTopic) + return render_template('yuqingpredict.html', + username=username, + hotWordList=TopicList, + defaultHotWord=defaultTopic, + hotWordLen=TopicLen, + sentences=sentences, + xData=X, + yData=Y, + comments=comments) + @pb.route('/articleCloud') def articleCloud(): diff --git a/views/page/templates/articleChar.html b/views/page/templates/articleChar.html index 06d251c..bbc0243 100644 --- a/views/page/templates/articleChar.html +++ b/views/page/templates/articleChar.html @@ -42,7 +42,7 @@
  • 数据可视化
  • - - +
  • 词云图
  • diff --git a/views/page/templates/articleContentCloud.html b/views/page/templates/articleContentCloud.html index 4f64e51..c252ed3 100644 --- a/views/page/templates/articleContentCloud.html +++ b/views/page/templates/articleContentCloud.html @@ -72,7 +72,7 @@
    评论分析 - +
  • 词云图
  • -
  • 首页
  • - - +
  • - 机器学习预测 + 词云图
  • - - +
  • 词云图
  • diff --git a/views/page/templates/hotWord.html b/views/page/templates/hotWord.html index 2cb738f..4600cf1 100644 --- a/views/page/templates/hotWord.html +++ b/views/page/templates/hotWord.html @@ -2,7 +2,6 @@ {% block title %} 热词统计 {% endblock %} - {% block nav %}