File size: 4,251 Bytes
98c2b46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# rhetoric_analyzer.py
import spacy
from spacy.tokens import Doc, Span, Token
from typing import List
import data_models # Düz yapı importu
from rich.console import Console
# VADER'ı import et
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

console = Console()
# VADER duygu analizcisini başlat
analyzer = SentimentIntensityAnalyzer()

# Basit Retorik İpuçları

# Güçlü Duygu Eşiği (VADER compound score için)
# -1 (çok negatif) ile +1 (çok pozitif) arasında.
# |score| > 0.5 genellikle belirgin bir duygu ifade eder.
STRONG_SENTIMENT_THRESHOLD = 0.5

# Superlative'ler (En üstünlük belirten sıfatlar/zarflar)
# spaCy'nin POS tag'lerini kullanabiliriz: 'JJS' (superlative adjective), 'RBS' (superlative adverb)
SUPERLATIVE_TAGS = {"JJS", "RBS"}

# Retorik Sorular (Basit: Soru işareti var mı?)
# Daha gelişmiş analiz gerekir ama başlangıç için bu yeterli.

def analyze_sentence_sentiment(sent: Span) -> List[data_models.Finding]:
    """VADER kullanarak cümlenin duygu skorunu analiz eder ve güçlü duyguları bulur."""
    findings = []
    # VADER'ın polarity_scores fonksiyonu bir dict döndürür: {'neg', 'neu', 'pos', 'compound'}
    vs = analyzer.polarity_scores(sent.text)
    compound_score = vs['compound']

    description = None
    severity = "Low" # Varsayılan

    if compound_score >= STRONG_SENTIMENT_THRESHOLD:
        description = f"Sentence potentially expresses strong positive sentiment (VADER score: {compound_score:.2f})."
        severity = "Medium"
    elif compound_score <= -STRONG_SENTIMENT_THRESHOLD:
        description = f"Sentence potentially expresses strong negative sentiment (VADER score: {compound_score:.2f})."
        severity = "Medium"

    if description:
        findings.append(data_models.Finding(
            finding_type="RhetoricalDevice",
            description=description,
            severity=severity,
            span_start=sent.start_char,
            span_end=sent.end_char,
            details={"device_type": "Strong Sentiment", "vader_score": vs}
        ))
    return findings

def detect_superlatives(sent: Span) -> List[data_models.Finding]:
    """Cümlede superlative (en üstünlük) ifadeleri arar."""
    findings = []
    superlative_words = []
    for token in sent:
        if token.tag_ in SUPERLATIVE_TAGS:
            superlative_words.append(token.text)

    if superlative_words:
        findings.append(data_models.Finding(
            finding_type="RhetoricalDevice",
            description=f"Use of superlative(s) detected: {', '.join(superlative_words)}.",
            severity="Low", # Tek başına zayıf bir gösterge
            span_start=sent.start_char,
            span_end=sent.end_char,
            details={"device_type": "Superlative", "words": superlative_words}
        ))
    return findings

def detect_rhetorical_questions(sent: Span) -> List[data_models.Finding]:
    """Cümlede soru işareti olup olmadığını kontrol eder (çok basit)."""
    findings = []
    if sent.text.strip().endswith("?"):
        findings.append(data_models.Finding(
            finding_type="RhetoricalDevice",
            description="Sentence ends with a question mark (potential rhetorical question).",
            severity="Low", # Bağlam olmadan bilmek zor
            span_start=sent.start_char,
            span_end=sent.end_char,
            details={"device_type": "Potential Question"}
        ))
    return findings

def simple_rhetoric_analyzer(doc: Doc) -> List[data_models.Finding]:
    """

    Metindeki cümleleri basit kurallarla analiz ederek potansiyel retorik araçları bulur.

    """
    all_findings = []
    for sent in doc.sents:
        # Her cümle için retorik kontrollerini çalıştır
        all_findings.extend(analyze_sentence_sentiment(sent))
        all_findings.extend(detect_superlatives(sent))
        all_findings.extend(detect_rhetorical_questions(sent))
        # Buraya diğer retorik fonksiyon çağrıları eklenebilir

    console.print(f" -> Simple Rhetoric Analyzer found {len(all_findings)} potential rhetorical indicators.", style="dim")
    return all_findings