|
| 1 | +# Backend/AnalysisWorker.py |
| 2 | +from PySide6.QtCore import QObject, Signal |
| 3 | +from PySide6.QtGui import QImage |
| 4 | +import time |
| 5 | + |
| 6 | +from Analysis.SentimentAnalysis import run_sentiment_summary |
| 7 | +from Analysis.WordCloud import WordCloudAnalyzer |
| 8 | + |
| 9 | +class AnalysisWorker(QObject): |
| 10 | + """ |
| 11 | + Threaded worker to run analysis (sentiment summary + wordcloud) on a list of sentences. |
| 12 | + Emits progress updates for the splash and returns QImage results. |
| 13 | + """ |
| 14 | + |
| 15 | + progress_updated = Signal(str) |
| 16 | + progress_percentage = Signal(int) |
| 17 | + finished = Signal() |
| 18 | + sentiment_ready = Signal(QImage) |
| 19 | + wordcloud_ready = Signal(QImage) |
| 20 | + |
| 21 | + def __init__(self, sentences: list[str], sentiment_size: tuple = (1600, 520), |
| 22 | + wordcloud_size: tuple = (2800, 1680), max_words: int = 200): |
| 23 | + super().__init__() |
| 24 | + self.sentences = sentences or [] |
| 25 | + self.sent_w, self.sent_h = sentiment_size |
| 26 | + self.wc_w, self.wc_h = wordcloud_size |
| 27 | + self.max_words = max_words |
| 28 | + self._cancelled = False |
| 29 | + |
| 30 | + def cancel(self): |
| 31 | + self._cancelled = True |
| 32 | + |
| 33 | + def run(self) -> None: |
| 34 | + try: |
| 35 | + total_stages = 4 |
| 36 | + stage = 0 |
| 37 | + |
| 38 | + # Stage 1: loading/extract count |
| 39 | + stage += 1 |
| 40 | + self.progress_updated.emit("Preparing sentences for analysis...") |
| 41 | + self.progress_percentage.emit(int((stage/total_stages)*100 * 0.02)) # small percent |
| 42 | + |
| 43 | + sentences = self.sentences |
| 44 | + n = len(sentences) |
| 45 | + if self._cancelled: |
| 46 | + self.progress_updated.emit("Analysis cancelled.") |
| 47 | + self.finished.emit() |
| 48 | + return |
| 49 | + |
| 50 | + # Stage 2: Sentiment (iterate sentences — dynamic progress) |
| 51 | + stage += 1 |
| 52 | + self.progress_updated.emit("Running sentiment analysis...") |
| 53 | + # We'll update percent dynamically across this stage (weight: 45%) |
| 54 | + sentiment_stage_weight = 45 |
| 55 | + base = int(((stage-1)/total_stages) * 100) |
| 56 | + if n == 0: |
| 57 | + self.progress_percentage.emit(base + 1) |
| 58 | + else: |
| 59 | + # process in micro-batches to allow progress updates |
| 60 | + batch = max(1, n // 20) |
| 61 | + processed = 0 |
| 62 | + # build text list chunked — run_sentiment_summary expects sentences list |
| 63 | + # but it's not incremental; to show progress we compute compound in loop using VADER directly would be needed. |
| 64 | + # For simplicity and to avoid importing internals, call run_sentiment_summary once but fake granular progress. |
| 65 | + # Show incremental progress while computing |
| 66 | + for i in range(0, n, batch): |
| 67 | + if self._cancelled: |
| 68 | + self.progress_updated.emit("Analysis cancelled.") |
| 69 | + self.finished.emit() |
| 70 | + return |
| 71 | + # small sleep to let UI update if heavy |
| 72 | + time.sleep(0.01) |
| 73 | + processed += min(batch, n - i) |
| 74 | + frac = processed / n |
| 75 | + pct = base + int(frac * sentiment_stage_weight) |
| 76 | + self.progress_percentage.emit(min(pct, 99)) |
| 77 | + |
| 78 | + # Now compute final sentiment image |
| 79 | + sentiment_img = run_sentiment_summary(sentences, width=self.sent_w, height=self.sent_h) |
| 80 | + self.sentiment_ready.emit(sentiment_img) |
| 81 | + |
| 82 | + # Stage 3: Wordcloud (weight: 45%) |
| 83 | + stage += 1 |
| 84 | + self.progress_updated.emit("Generating word cloud...") |
| 85 | + wc_base = int(((stage-1)/total_stages) * 100) |
| 86 | + # Quick progress ticks while generating |
| 87 | + # generate_wordcloud is blocking; show small animation ticks before/after |
| 88 | + for tick in range(3): |
| 89 | + if self._cancelled: |
| 90 | + self.progress_updated.emit("Analysis cancelled.") |
| 91 | + self.finished.emit() |
| 92 | + return |
| 93 | + time.sleep(0.05) |
| 94 | + self.progress_percentage.emit(wc_base + int((tick+1) * (40/3))) |
| 95 | + |
| 96 | + wc_img = WordCloudAnalyzer(max_words=self.max_words).generate_wordcloud(sentences, width=self.wc_w, height=self.wc_h) |
| 97 | + self.wordcloud_ready.emit(wc_img) |
| 98 | + self.progress_percentage.emit(95) |
| 99 | + |
| 100 | + # Stage 4: Finalizing |
| 101 | + stage += 1 |
| 102 | + self.progress_updated.emit("Finalizing results...") |
| 103 | + time.sleep(0.05) |
| 104 | + self.progress_percentage.emit(100) |
| 105 | + self.progress_updated.emit("Analysis complete.") |
| 106 | + self.finished.emit() |
| 107 | + |
| 108 | + except Exception as e: |
| 109 | + # best-effort error reporting |
| 110 | + try: |
| 111 | + self.progress_updated.emit(f"Analysis error: {str(e)}") |
| 112 | + except Exception: |
| 113 | + pass |
| 114 | + self.finished.emit() |
0 commit comments