File size: 2,796 Bytes
1998bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from textblob import TextBlob

# 定义颜色映射
ADJECTIVE_COLORS = {
    "positive": "#4CAF50",  # 绿色
    "negative": "#F44336",  # 红色
    "neutral": "#FFC107"    # 黄色
}

# 自定义短语情感覆盖规则
PHRASE_SENTIMENT_OVERRIDES = {
    "significant drop": "negative",
    "significant drops": "negative",
    "sharp decline": "negative",
    "strong performance": "positive",
    "Poor performance": "negative"
    # 可以继续添加更多短语规则...
}

# 负面触发词集合
NEGATIVE_TRIGGERS = {"drop", "decline", "failure", "loss", "down", "worse", "weak", "poor"}

def get_phrase_sentiment(phrase):
    """增强的短语情感分析逻辑"""
    # 1. 优先检查自定义规则
    lower_phrase = phrase.lower()
    if lower_phrase in PHRASE_SENTIMENT_OVERRIDES:
        return PHRASE_SENTIMENT_OVERRIDES[lower_phrase]
    
    # 2. 检查负面触发词
    words = TextBlob(phrase).words
    if any(w.lower() in NEGATIVE_TRIGGERS for w in words):
        return "negative"
    
    # 3. 默认情感分析
    sentiment = TextBlob(phrase).sentiment.polarity
    if sentiment > 0.1:
        return "positive"
    elif sentiment < -0.1:
        return "negative"
    else:
        return "neutral"

def highlight_adjectives(text):
    """高亮形容词短语并根据情感着色"""
    if not isinstance(text, str) or not text.strip():
        return text
    
    try:
        blob = TextBlob(text)
        highlighted = []
        i = 0
        tags = blob.tags
        
        while i < len(tags):
            word, tag = tags[i]
            
            # 检查形容词短语模式 (形容词+名词)
            if tag.startswith('JJ') and i+1 < len(tags) and tags[i+1][1].startswith('NN'):
                phrase = f"{word} {tags[i+1][0]}"
                # 使用增强的情感分析
                sentiment = get_phrase_sentiment(phrase)
                
                color = ADJECTIVE_COLORS.get(sentiment, "#000000")
                highlighted.append(f'<span style="color: {color}; font-weight: bold">{phrase}</span>')
                i += 2  # 跳过下一个词,因为已经处理了
            elif tag.startswith('JJ'):  # 单独形容词
                sentiment = get_phrase_sentiment(word)  # 也能处理单个词
                color = ADJECTIVE_COLORS.get(sentiment, "#000000")
                highlighted.append(f'<span style="color: {color}; font-weight: bold">{word}</span>')
                i += 1
            else:
                highlighted.append(word)
                i += 1
        
        # 保留原始空格和标点
        return " ".join(highlighted).replace(" ,", ",").replace(" .", ".").replace(" '", "'")
    
    except Exception as e:
        print(f"Error processing text: {e}")
        return text