Added a feature to select the emotion analysis model, supporting switching betwe…
…en the basic and improved models.
Showing
3 changed files
with
81 additions
and
26 deletions
| @@ -167,24 +167,44 @@ def getYuQingCharDataOne(): | @@ -167,24 +167,44 @@ def getYuQingCharDataOne(): | ||
| 167 | biedata = [{'name': x, 'value': y} for x, y in zip(X, Y)] | 167 | biedata = [{'name': x, 'value': y} for x, y in zip(X, Y)] |
| 168 | return X, Y, biedata | 168 | return X, Y, biedata |
| 169 | 169 | ||
| 170 | -def getYuQingCharDataTwo(): | ||
| 171 | - # 分析评论和文章的情感 | 170 | +def getYuQingCharDataTwo(model_type='pro'): |
| 171 | + """ | ||
| 172 | + 分析评论和文章的情感 | ||
| 173 | + :param model_type: 使用的模型类型,'basic' 为基础模型,'pro' 为改进模型 | ||
| 174 | + """ | ||
| 172 | comment_texts = [comment[4] for comment in commentList] | 175 | comment_texts = [comment[4] for comment in commentList] |
| 173 | article_texts = [article[5] for article in articleList] | 176 | article_texts = [article[5] for article in articleList] |
| 174 | 177 | ||
| 175 | - # 预测评论情感 | ||
| 176 | - comment_predictions = predict_sentiment(comment_texts) | ||
| 177 | - if comment_predictions is not None: | ||
| 178 | - comment_sentiments = ['良好' if pred == 0 else '不良' for pred in comment_predictions] | ||
| 179 | - else: | 178 | + if model_type == 'basic': |
| 179 | + # 使用基础模型(SnowNLP) | ||
| 180 | comment_sentiments = [] | 180 | comment_sentiments = [] |
| 181 | - | ||
| 182 | - # 预测文章情感 | ||
| 183 | - article_predictions = predict_sentiment(article_texts) | ||
| 184 | - if article_predictions is not None: | ||
| 185 | - article_sentiments = ['良好' if pred == 0 else '不良' for pred in article_predictions] | ||
| 186 | - else: | 181 | + for text in comment_texts: |
| 182 | + value = SnowNLP(text).sentiments | ||
| 183 | + if value > 0.6: | ||
| 184 | + comment_sentiments.append('良好') | ||
| 185 | + else: | ||
| 186 | + comment_sentiments.append('不良') | ||
| 187 | + | ||
| 187 | article_sentiments = [] | 188 | article_sentiments = [] |
| 189 | + for text in article_texts: | ||
| 190 | + value = SnowNLP(text).sentiments | ||
| 191 | + if value > 0.6: | ||
| 192 | + article_sentiments.append('良好') | ||
| 193 | + else: | ||
| 194 | + article_sentiments.append('不良') | ||
| 195 | + else: | ||
| 196 | + # 使用改进模型 | ||
| 197 | + comment_predictions = predict_sentiment(comment_texts) | ||
| 198 | + if comment_predictions is not None: | ||
| 199 | + comment_sentiments = ['良好' if pred == 0 else '不良' for pred in comment_predictions] | ||
| 200 | + else: | ||
| 201 | + comment_sentiments = [] | ||
| 202 | + | ||
| 203 | + article_predictions = predict_sentiment(article_texts) | ||
| 204 | + if article_predictions is not None: | ||
| 205 | + article_sentiments = ['良好' if pred == 0 else '不良' for pred in article_predictions] | ||
| 206 | + else: | ||
| 207 | + article_sentiments = [] | ||
| 188 | 208 | ||
| 189 | # 统计结果 | 209 | # 统计结果 |
| 190 | comment_counts = Counter(comment_sentiments) | 210 | comment_counts = Counter(comment_sentiments) |
| @@ -199,8 +199,11 @@ def commentChar(): | @@ -199,8 +199,11 @@ def commentChar(): | ||
| 199 | @pb.route('/yuqingChar') | 199 | @pb.route('/yuqingChar') |
| 200 | def yuqingChar(): | 200 | def yuqingChar(): |
| 201 | username = session.get('username') | 201 | username = session.get('username') |
| 202 | + # 获取模型选择参数 | ||
| 203 | + model_type = request.args.get('model', 'pro') # 默认使用改进模型 | ||
| 204 | + | ||
| 202 | X, Y, biedata = getYuQingCharDataOne() | 205 | X, Y, biedata = getYuQingCharDataOne() |
| 203 | - biedata1, biedata2 = getYuQingCharDataTwo() | 206 | + biedata1, biedata2 = getYuQingCharDataTwo(model_type) |
| 204 | x1Data, y1Data = getYuQingCharDataThree() | 207 | x1Data, y1Data = getYuQingCharDataThree() |
| 205 | return render_template('yuqingChar.html', | 208 | return render_template('yuqingChar.html', |
| 206 | username=username, | 209 | username=username, |
| @@ -210,7 +213,8 @@ def yuqingChar(): | @@ -210,7 +213,8 @@ def yuqingChar(): | ||
| 210 | biedata1=biedata1, | 213 | biedata1=biedata1, |
| 211 | biedata2=biedata2, | 214 | biedata2=biedata2, |
| 212 | x1Data=x1Data, | 215 | x1Data=x1Data, |
| 213 | - y1Data=y1Data) | 216 | + y1Data=y1Data, |
| 217 | + model_type=model_type) | ||
| 214 | 218 | ||
| 215 | @pb.route('/yuqingpredict') | 219 | @pb.route('/yuqingpredict') |
| 216 | def yuqingpredict(): | 220 | def yuqingpredict(): |
| @@ -222,13 +226,26 @@ def yuqingpredict(): | @@ -222,13 +226,26 @@ def yuqingpredict(): | ||
| 222 | TopicLen = getTopicLen(defaultTopic) | 226 | TopicLen = getTopicLen(defaultTopic) |
| 223 | X, Y = getTopicCreatedAtandpredictData(defaultTopic) | 227 | X, Y = getTopicCreatedAtandpredictData(defaultTopic) |
| 224 | 228 | ||
| 225 | - # 使用改进版模型进行情感预测 | ||
| 226 | - predicted_label, confidence = predict_sentiment(defaultTopic) | ||
| 227 | - if predicted_label is not None: | ||
| 228 | - sentences = '良好' if predicted_label == 0 else '不良' | ||
| 229 | - sentences = f"{sentences} (置信度: {confidence:.2f})" | 229 | + # 获取模型选择参数 |
| 230 | + model_type = request.args.get('model', 'pro') # 默认使用改进模型 | ||
| 231 | + | ||
| 232 | + if model_type == 'basic': | ||
| 233 | + # 使用基础模型(SnowNLP) | ||
| 234 | + value = SnowNLP(defaultTopic).sentiments | ||
| 235 | + if value == 0.5: | ||
| 236 | + sentences = '中性' | ||
| 237 | + elif value > 0.5: | ||
| 238 | + sentences = '正面' | ||
| 239 | + elif value < 0.5: | ||
| 240 | + sentences = '负面' | ||
| 230 | else: | 241 | else: |
| 231 | - sentences = '预测失败' | 242 | + # 使用改进模型 |
| 243 | + predicted_label, confidence = predict_sentiment(defaultTopic) | ||
| 244 | + if predicted_label is not None: | ||
| 245 | + sentences = '良好' if predicted_label == 0 else '不良' | ||
| 246 | + sentences = f"{sentences} (置信度: {confidence:.2f})" | ||
| 247 | + else: | ||
| 248 | + sentences = '预测失败' | ||
| 232 | 249 | ||
| 233 | comments = getCommentFilterDataTopic(defaultTopic) | 250 | comments = getCommentFilterDataTopic(defaultTopic) |
| 234 | return render_template('yuqingpredict.html', | 251 | return render_template('yuqingpredict.html', |
| @@ -239,7 +256,8 @@ def yuqingpredict(): | @@ -239,7 +256,8 @@ def yuqingpredict(): | ||
| 239 | sentences=sentences, | 256 | sentences=sentences, |
| 240 | xData=X, | 257 | xData=X, |
| 241 | yData=Y, | 258 | yData=Y, |
| 242 | - comments=comments) | 259 | + comments=comments, |
| 260 | + model_type=model_type) | ||
| 243 | 261 | ||
| 244 | 262 | ||
| 245 | @pb.route('/articleCloud') | 263 | @pb.route('/articleCloud') |
| @@ -155,18 +155,35 @@ | @@ -155,18 +155,35 @@ | ||
| 155 | <select onchange="hotWordChange(event)" class="form-control mb-3"> | 155 | <select onchange="hotWordChange(event)" class="form-control mb-3"> |
| 156 | {% for i in hotWordList %} | 156 | {% for i in hotWordList %} |
| 157 | {% if defaultHotWord == i[0] %} | 157 | {% if defaultHotWord == i[0] %} |
| 158 | - <option selected value="{{ i[0] }}">{{ i[0] }}</option> | 158 | + <option selected value="{{ i[0] }}">{{ i[0] }}</option> |
| 159 | {% else %} | 159 | {% else %} |
| 160 | - <option value="{{ i[0] }}">{{ i[0] }}</option> | 160 | + <option value="{{ i[0] }}">{{ i[0] }}</option> |
| 161 | {% endif %} | 161 | {% endif %} |
| 162 | {% endfor %} | 162 | {% endfor %} |
| 163 | </select> | 163 | </select> |
| 164 | + </div> | ||
| 165 | + <div class="form-group"> | ||
| 166 | + <label>模型选择</label> | ||
| 167 | + <select onchange="modelChange(event)" class="form-control mb-3"> | ||
| 168 | + {% if model_type == 'basic' %} | ||
| 169 | + <option selected value="basic">基础模型</option> | ||
| 170 | + <option value="pro">改进模型</option> | ||
| 171 | + {% else %} | ||
| 172 | + <option value="basic">基础模型</option> | ||
| 173 | + <option selected value="pro">改进模型</option> | ||
| 174 | + {% endif %} | ||
| 175 | + </select> | ||
| 176 | + </div> | ||
| 164 | <script> | 177 | <script> |
| 165 | function hotWordChange(e){ | 178 | function hotWordChange(e){ |
| 166 | - window.location.href = 'http://127.0.0.1:5000/page/yuqingpredict?Topic=' + e.target.value | 179 | + const model = document.querySelector('select[onchange="modelChange(event)"]').value; |
| 180 | + window.location.href = 'http://127.0.0.1:5000/page/yuqingpredict?Topic=' + e.target.value + '&model=' + model; | ||
| 181 | + } | ||
| 182 | + function modelChange(e){ | ||
| 183 | + const topic = document.querySelector('select[onchange="hotWordChange(event)"]').value; | ||
| 184 | + window.location.href = 'http://127.0.0.1:5000/page/yuqingpredict?Topic=' + topic + '&model=' + e.target.value; | ||
| 167 | } | 185 | } |
| 168 | </script> | 186 | </script> |
| 169 | - </div> | ||
| 170 | </div> | 187 | </div> |
| 171 | </div> | 188 | </div> |
| 172 | 189 |
-
Please register or login to post a comment