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.
 
 
 
 
 

338 lines
12 KiB

<?php
class CommentAnalyzer {
// 关键词和正则表达式配置
private $config = [
'positive' => [
'keywords' => [
'有效', '好用', '好了', '治愈', '见效', '不错', '管用', '有用',
'灵验', '缓解', '痊愈', '见效', '神效', '推荐', '感谢', '好了',
'管用', '有效果', '有效果', '奏效', '改善', '见效快', '神奇',
'有效', '有效果', '有效果', '有效果', '有效果', '有效果'
],
'patterns' => [
'/(喝|吃|用)了?\s*\d+\s*次?[就]?(好|痊愈|缓解|不咳|不痛)了/u',
'/(效果|结果)\s*(非常|很)?\s*(好|明显|不错|显著|惊人)/u',
'/(尝试|试了|用了)\s*\d+\s*[天个]?\s*(就)?(好|痊愈|缓解|见效)/u',
'/(咳嗽|咳|痛|疼|症状)\s*(明显)?\s*(减轻|缓解|消失|好了)/u',
'/(感谢|谢谢)\s*(分享|博主|张医生|宝旬)/u'
]
],
'negative' => [
'keywords' => [
'无效', '没用', '不好', '没效果', '不行', '不见效', '没好转',
'加重', '恶化', '白费', '失望', '避雷', '没用', '没效果',
'无效果', '无改善', '没作用', '不灵', '骗人', '忽悠', '上当'
],
'patterns' => [
'/(还是|依然|仍旧|仍然)\s*(咳|难受|痛|疼|没效果)/u',
'/(一点|完全|根本|丝毫)\s*没(效果|用|好转|作用|改善)/u',
'/(不仅|不但)\s*没(好|改善).*(反而|而且)\s*(加重|恶化)/u',
'/(浪费|白费|白忙|白折腾)\s*(时间|精力)/u',
'/(失望|后悔|上当|骗人|忽悠|别信)\s*(了|吧|!)/u'
]
]
];
/**
* 分析评论情感
*
* @param string $comment 评论内容
* @return string 情感分类: 'positive', 'negative' 或 'neutral'
*/
public function analyze($comment) {
// 预处理:去除多余空格和特殊字符
$comment = $this->preprocess($comment);
// 1. 检查否定词(优先级高)
foreach ($this->config['negative']['keywords'] as $word) {
if (mb_strpos($comment, $word) !== false) {
return 'negative';
}
}
// 2. 检查肯定词
foreach ($this->config['positive']['keywords'] as $word) {
if (mb_strpos($comment, $word) !== false) {
return 'positive';
}
}
// 3. 检查否定句式
foreach ($this->config['negative']['patterns'] as $pattern) {
if (preg_match($pattern, $comment)) {
return 'negative';
}
}
// 4. 检查肯定句式
foreach ($this->config['positive']['patterns'] as $pattern) {
if (preg_match($pattern, $comment)) {
return 'positive';
}
}
return 'neutral';
}
/**
* 预处理评论内容
*/
private function preprocess($comment) {
// 移除多余空格
$comment = preg_replace('/\s+/u', ' ', $comment);
// 移除常见标点符号(保留中文字符)
$comment = preg_replace('/[^\p{Han}\p{P}\w\s]/u', '', $comment);
return trim($comment);
}
}
// ===================== 使用示例 =====================
$analyzer = new CommentAnalyzer();
// 微博内容
$weibo = "针灸匠张宝旬\n原创\n25-7-7 14:41\n发布于 北京\n来自 华为 Mate X5 典藏版\n#张宝旬妙招# 刀片桑用花椒蒸梨。按图示做。汤水清甜微麻,一点不辣,还带股淡淡的花椒香味,吃梨肉、喝梨汤,效果好。 ​​​";
// 示例评论
$comments = [
"去年冬天寒咳,直接切梨加花椒加少许水蒸半个小时,喝几次就好了",
"试了完全没效果,咳嗽更严重了",
"花椒蒸梨一点用都没有,避雷!",
"喝了三天,依然咳得睡不着",
"效果非常明显,第二天就不咳了",
"这个方子对我很管用,咳嗽明显减轻了",
"按照方法做了,但感觉没什么变化",
"张医生的方法总是这么神奇,感谢分享!",
"孩子咳嗽试了这个方法,结果反而加重了",
"蒸梨的时候花椒放多了,味道有点怪",
"这个方法简单易行,推荐给大家",
"试了两次,效果不明显,可能不适合我",
"中医小妙招真是博大精深",
"花椒蒸梨?这是什么奇怪的组合",
"喝了当晚就不怎么咳了,太有效了!",
"完全没好转,白忙活一场",
"这个方法在抖音上看到过,亲测有效",
"咳嗽没缓解,反而胃不舒服了",
"张宝旬医生的方法值得信赖",
"没坚持喝,不知道效果如何"
];
// 分析评论并分类
$results = [
'positive' => [],
'negative' => []
];
foreach ($comments as $comment) {
$result = $analyzer->analyze($comment);
if ($result !== 'neutral') {
$results[$result][] = $comment;
}
}
// ===================== 显示结果 =====================
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>中医小妙招评论分析工具</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Microsoft YaHei', sans-serif;
}
body {
background-color: #f5f7fa;
color: #333;
line-height: 1.6;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
header {
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
color: white;
padding: 25px 30px;
position: relative;
}
h1 {
font-size: 28px;
margin-bottom: 10px;
}
.subtitle {
font-size: 16px;
opacity: 0.9;
}
.weibo-card {
background: #eef5ff;
border-left: 4px solid #2575fc;
padding: 20px;
margin: 20px;
border-radius: 5px;
}
.weibo-content {
font-size: 18px;
line-height: 1.7;
color: #1a1a1a;
}
.weibo-meta {
color: #666;
font-size: 14px;
margin-top: 10px;
}
.results-container {
display: flex;
flex-wrap: wrap;
padding: 20px;
}
.result-column {
flex: 1;
min-width: 300px;
padding: 15px;
}
.result-header {
font-size: 20px;
padding: 15px 0;
margin-bottom: 15px;
border-bottom: 2px solid;
display: flex;
align-items: center;
}
.positive .result-header {
color: #28a745;
border-bottom-color: #28a745;
}
.negative .result-header {
color: #dc3545;
border-bottom-color: #dc3545;
}
.result-header i {
margin-right: 10px;
font-size: 24px;
}
.comment-list {
list-style: none;
}
.comment-item {
background: #f8f9fa;
border-radius: 8px;
padding: 15px;
margin-bottom: 15px;
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
border-left: 4px solid;
transition: transform 0.2s;
}
.comment-item:hover {
transform: translateY(-3px);
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
.positive .comment-item {
border-left-color: #28a745;
background: #f0fff4;
}
.negative .comment-item {
border-left-color: #dc3545;
background: #fff0f0;
}
.comment-text {
margin-bottom: 8px;
font-size: 16px;
}
.comment-label {
display: inline-block;
padding: 3px 8px;
border-radius: 4px;
font-size: 12px;
font-weight: bold;
}
.positive .comment-label {
background: #28a745;
color: white;
}
.negative .comment-label {
background: #dc3545;
color: white;
}
.stats {
background: #f8f9fa;
padding: 20px;
text-align: center;
border-top: 1px solid #eee;
font-size: 18px;
color: #555;
}
.highlight {
font-weight: bold;
font-size: 24px;
color: #2575fc;
margin: 0 5px;
}
@media (max-width: 768px) {
.results-container {
flex-direction: column;
}
.result-column {
min-width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>中医小妙招评论分析工具</h1>
<p class="subtitle">自动筛选用户对中医方法有效性的反馈</p>
</header>
<div class="weibo-card">
<div class="weibo-content"><?= nl2br(htmlspecialchars($weibo)) ?></div>
<div class="weibo-meta">博主:针灸匠张宝旬 | 发布时间:2025-07-07</div>
</div>
<div class="stats">
分析结果:共 <span class="highlight"><?= count($comments) ?></span> 条评论,
其中有效反馈 <span class="highlight"><?= count($results['positive']) ?></span> 条,
无效反馈 <span class="highlight"><?= count($results['negative']) ?></span> 条
</div>
<div class="results-container">
<div class="result-column positive">
<div class="result-header">
<span>✅ 有效反馈 (<?= count($results['positive']) ?>)</span>
</div>
<ul class="comment-list">
<?php foreach ($results['positive'] as $comment): ?>
<li class="comment-item">
<div class="comment-text"><?= htmlspecialchars($comment) ?></div>
<span class="comment-label">有效</span>
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="result-column negative">
<div class="result-header">
<span>❌ 无效反馈 (<?= count($results['negative']) ?>)</span>
</div>
<ul class="comment-list">
<?php foreach ($results['negative'] as $comment): ?>
<li class="comment-item">
<div class="comment-text"><?= htmlspecialchars($comment) ?></div>
<span class="comment-label">无效</span>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</body>
</html>