#!/usr/bin/env python3
"""
分析推文数据，找出最常见的问题
"""

import pandas as pd
import re
from collections import Counter

# 加载推文数据
df = pd.read_csv('data/Tweets.csv')

# 获取负面推文
tweets = df[df['airline_sentiment'] == 'negative']['text'].tolist()

# 定义问题分类体系（来自smart_care_system.py）
PROBLEM_CLASSIFICATION = {
    "服务类": {
        "餐食": ["food", "meal", "dinner", "lunch", "breakfast", "snack", "beverage", "drink"],
        "服务态度": ["service", "staff", "crew", "attitude", "rude", "friendly", "helpful"],
        "登机": ["boarding", "check-in", "boarding pass", "gate"],
        "行李服务": ["baggage", "luggage", "suitcase", "lost", "delayed", "damaged"],
        "客服支持": ["support", "customer service", "help", "assistance", "contact"]
    },
    "硬件类": {
        "座位": ["seat", "comfort", "legroom", "space", "cushion"],
        "设施": ["facility", "entertainment", "screen", "wifi", "power", "charging"],
        "飞机状况": ["plane", "aircraft", "clean", "maintenance", "condition"]
    },
    "流程类": {
        "延误": ["delay", "late", "delayed", "on time", "schedule", "timely"],
        "取消": ["cancel", "cancelled", "cancellation"],
        "改签": ["reschedule", "change", "modify", "reroute"],
        "超售": ["oversold", "overbooked"],
        "中转": ["connection", "transfer", "layover"]
    }
}

# 将分类体系转换为扁平的关键词到问题类型的映射
keyword_to_problem = {}
for primary, secondaries in PROBLEM_CLASSIFICATION.items():
    for secondary, keywords in secondaries.items():
        for keyword in keywords:
            keyword_to_problem[keyword.lower()] = (primary, secondary)

# 分析推文，找出问题
def analyze_tweet_problems(tweet):
    """分析单条推文中的问题"""
    tweet_lower = tweet.lower()
    problems_found = set()
    
    # 检查每个关键词
    for keyword, (primary, secondary) in keyword_to_problem.items():
        if keyword in tweet_lower:
            problems_found.add((primary, secondary))
    
    return problems_found

# 统计所有推文的问题
problem_counter = Counter()
for tweet in tweets:
    problems = analyze_tweet_problems(tweet)
    for problem in problems:
        problem_counter[problem] += 1

# 找出前15个最常见的问题
print("前15个最常见的问题：")
for i, ((primary, secondary), count) in enumerate(problem_counter.most_common(15), 1):
    print(f"{i}. {primary} - {secondary}: {count} 次")

# 生成问卷问题列表
questionnaire_questions = [f"{secondary}" for (primary, secondary), count in problem_counter.most_common(15)]
print("\n问卷问题列表：")
for i, question in enumerate(questionnaire_questions, 1):
    print(f"{i}. {question}")
