|
|
| import spacy
|
| from spacy.tokens import Doc, Span, Token
|
| from typing import List
|
| import data_models
|
| from rich.console import Console
|
|
|
| from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
|
|
| console = Console()
|
|
|
| analyzer = SentimentIntensityAnalyzer()
|
|
|
|
|
|
|
|
|
|
|
|
|
| STRONG_SENTIMENT_THRESHOLD = 0.5
|
|
|
|
|
|
|
| SUPERLATIVE_TAGS = {"JJS", "RBS"}
|
|
|
|
|
|
|
|
|
| def analyze_sentence_sentiment(sent: Span) -> List[data_models.Finding]:
|
| """VADER kullanarak cümlenin duygu skorunu analiz eder ve güçlü duyguları bulur."""
|
| findings = []
|
|
|
| vs = analyzer.polarity_scores(sent.text)
|
| compound_score = vs['compound']
|
|
|
| description = None
|
| severity = "Low"
|
|
|
| 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",
|
| 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",
|
| 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:
|
|
|
| all_findings.extend(analyze_sentence_sentiment(sent))
|
| all_findings.extend(detect_superlatives(sent))
|
| all_findings.extend(detect_rhetorical_questions(sent))
|
|
|
|
|
| console.print(f" -> Simple Rhetoric Analyzer found {len(all_findings)} potential rhetorical indicators.", style="dim")
|
| return all_findings |