加入消极,积极top参数

This commit is contained in:
lzh
2022-02-22 13:57:29 +08:00
parent 5df1bed0ad
commit d92ea36a58
3 changed files with 15 additions and 9 deletions
+4 -2
View File
@@ -57,10 +57,12 @@ def display():
code, msg = 200, "success"
url = request.json.get("url")
username = request.json.get("username")
top = request.json.get("top", 10)
product_id = url.split('id=')[-1] if url.split('id=') else url
data = get_data(username, product_id)
data = data.update(get_top_positive_negative_frequency(data.pop("comments")))
return jsonify({"msg": msg, "code": code, "data":data})
data.update(get_top_positive_negative_frequency(data, top))
data.pop("comments")
return jsonify({"msg": msg, "code": code, "data": data})
if __name__ == '__main__':
+10 -6
View File
@@ -1,5 +1,5 @@
import jieba
import os
def stopwordslist(filepath):
stopwords = [line.strip() for line in open(filepath, 'r', encoding='gbk').readlines()]
@@ -20,7 +20,7 @@ def sentiment_dict(filepath):
# 对句子去除停用词
def movestopwords(sentence):
stopwords = stopwordslist('stopword.txt') # 这里加载停用词的路径
stopwords = stopwordslist(os.path.join(os.path.abspath("."),'stopword.txt')) # 这里加载停用词的路径
outstr = ''
for word in sentence:
if word not in stopwords:
@@ -37,7 +37,8 @@ def get_top_positive_negative_frequency(data, top=10):
:param top:
:return:
"""
senti_dict = sentiment_dict('BosonNLP_sentiment_score.txt')
senti_dict = sentiment_dict(os.path.join(os.path.abspath("."),'BosonNLP_sentiment_score.txt'))
print(os.path.join(os.path.abspath("."),'BosonNLP_sentiment_score.txt'))
content_seg = movestopwords(data["comments"])
scores = {}
frequency = {}
@@ -49,8 +50,8 @@ def get_top_positive_negative_frequency(data, top=10):
frequency[word] = 1
scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)
frequency = dict(sorted(frequency.items(), key=lambda x: x[1], reverse=True))
positive = [item[0] for item in scores[0:top]]
negative = [item[0] for item in scores[-top:]]
positive = dict(scores[0:top])
negative = dict(scores[-top:])
return {
"positive": positive,
"negative": negative,
@@ -60,4 +61,7 @@ def get_top_positive_negative_frequency(data, top=10):
if __name__ == '__main__':
# 读取nlp情感词分数
print(get_top_positive_negative_frequency(15))
from utils import get_data
data = get_data("lzh","1512002")
print(get_top_positive_negative_frequency(data,15))
+1 -1
View File
@@ -59,7 +59,7 @@ def parse_comments(username, product_id, page_limit=20):
res = requests.get(comments_url, headers=headers).json()
if res:
pages = res.get("data", {}).get("pagination", {}).get("totalPage", 1)
# 我们这里最多只爬取10页,一次爬多容易触发反爬虫机制
# 我们这里最多只爬取page_limit页,一次爬多容易触发反爬虫机制
pages = pages if pages <= page_limit else page_limit
for page in range(1, pages + 1):
comments_url = "https://you.163.com/xhr/comment/listByItemByTag.json?__timestamp={}&itemId={}&size=20&page={}&orderBy=0".format(