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'{phrase}') i += 2 # 跳过下一个词,因为已经处理了 elif tag.startswith('JJ'): # 单独形容词 sentiment = get_phrase_sentiment(word) # 也能处理单个词 color = ADJECTIVE_COLORS.get(sentiment, "#000000") highlighted.append(f'{word}') 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