File size: 28,348 Bytes
150e5be
a9c264e
 
 
 
 
 
150e5be
a9c264e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8acdbd0
150e5be
8acdbd0
 
150e5be
8acdbd0
 
150e5be
a9c264e
 
 
 
 
150e5be
a9c264e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150e5be
a9c264e
 
 
150e5be
 
 
 
a9c264e
150e5be
a9c264e
150e5be
a9c264e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8acdbd0
a9c264e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8acdbd0
a9c264e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150e5be
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
```python
import requests
import time
import json
import csv
import sqlite3
import logging
import os
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Tuple
from urllib.parse import urljoin, urlparse
from urllib.robotparser import RobotFileParser
from dataclasses import dataclass, asdict
from pathlib import Path
import re
from bs4 import BeautifulSoup
import pandas as pd
try:
    from hazm import Normalizer, WordTokenizer, SentenceTokenizer
    from transformers import AutoTokenizer, AutoModel
    import torch
    import numpy as np
    from sklearn.metrics.pairwise import cosine_similarity
    NLP_AVAILABLE = True
except ImportError as e:
    NLP_AVAILABLE = False
    logging.warning(f"⚠️ NLP libraries not available: {e}")

# Create required directories
log_dir = '/app/logs'
data_dir = '/app/data'
cache_dir = '/app/cache'
os.makedirs(log_dir, exist_ok=True)
os.makedirs(data_dir, exist_ok=True)
os.makedirs(cache_dir, exist_ok=True)

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler(os.path.join(log_dir, 'legal_scraper.log')),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)

# Iranian legal sources
IRANIAN_LEGAL_SOURCES = [
    "https://rc.majlis.ir",
    "https://dolat.ir",
    "https://iribnews.ir",
    "https://www.irna.ir",
    "https://www.tasnimnews.com",
    "https://www.mehrnews.com",
    "https://www.farsnews.ir"
]

@dataclass
class LegalDocument:
    title: str
    content: str
    source_url: str
    document_type: str
    date_published: Optional[str] = None
    date_scraped: str = None
    category: Optional[str] = None
    tags: List[str] = None
    summary: Optional[str] = None
    importance_score: float = 0.0
    sentiment_score: float = 0.0
    keywords: List[str] = None
    legal_entities: List[str] = None
    embedding: Optional[List[float]] = None
    language: str = "fa"
    
    def __post_init__(self):
        if self.date_scraped is None:
            self.date_scraped = datetime.now().isoformat()
        if self.tags is None:
            self.tags = []
        if self.keywords is None:
            self.keywords = []
        if self.legal_entities is None:
            self.legal_entities = []
        if self.embedding is None:
            self.embedding = []

class PersianNLPProcessor:
    def __init__(self):
        self.normalizer = None
        self.tokenizer = None
        self.sentence_tokenizer = None
        self.model = None
        self.model_tokenizer = None
        if NLP_AVAILABLE:
            try:
                logger.info("Initializing Persian NLP components...")
                self.normalizer = Normalizer()
                self.tokenizer = WordTokenizer()
                self.sentence_tokenizer = SentenceTokenizer()
                if os.getenv("ENVIRONMENT") != "huggingface_free":
                    self.model = AutoModel.from_pretrained("HooshvareLab/bert-fa-base-uncased", cache_dir="/app/cache")
                    self.model_tokenizer = AutoTokenizer.from_pretrained("HooshvareLab/bert-fa-base-uncased", cache_dir="/app/cache")
                logger.info("Persian NLP components initialized")
            except Exception as e:
                logger.warning(f"Failed to initialize NLP components: {e}. Falling back to basic text processing.")
                self.model = None
                self.model_tokenizer = None
    
    def normalize_text(self, text: str) -> str:
        if self.normalizer:
            return self.normalizer.normalize(text)
        return text
    
    def extract_keywords(self, text: str, top_n: int = 10) -> List[str]:
        if not NLP_AVAILABLE or not self.tokenizer:
            return []
        try:
            normalized_text = self.normalize_text(text)
            tokens = self.tokenizer.tokenize(normalized_text)
            word_freq = {}
            for token in tokens:
                if len(token) > 2 and token not in self.tokenizer.separators:
                    word_freq[token] = word_freq.get(token, 0) + 1
            sorted_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)
            return [word for word, freq in sorted_words[:top_n] if not re.match(r'[\d\s.,!?]', word)]
        except Exception as e:
            logger.error(f"Keyword extraction failed: {e}")
            return []
    
    def generate_summary(self, text: str, max_length: int = 100) -> str:
        if not NLP_AVAILABLE or not self.sentence_tokenizer:
            return text[:max_length] + "..." if len(text) > max_length else text
        try:
            sentences = self.sentence_tokenizer.tokenize(text)
            if not sentences:
                return text[:max_length] + "..." if len(text) > max_length else text
            summary = sentences[0]
            current_length = len(summary)
            for sentence in sentences[1:]:
                if current_length + len(sentence) <= max_length:
                    summary += " " + sentence
                    current_length += len(sentence)
                else:
                    break
            return summary
        except Exception as e:
            logger.error(f"Summary generation failed: {e}")
            return text[:max_length] + "..." if len(text) > max_length else text
    
    def get_embedding(self, text: str) -> List[float]:
        if not NLP_AVAILABLE or not self.model or not self.model_tokenizer:
            return []
        try:
            inputs = self.model_tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
            with torch.no_grad():
                outputs = self.model(**inputs)
            embedding = outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy().tolist()
            return embedding
        except Exception as e:
            logger.error(f"Embedding generation failed: {e}")
            return []
    
    def calculate_sentiment(self, text: str) -> float:
        if not NLP_AVAILABLE:
            return 0.0
        try:
            positive_words = {'مثبت', 'خوب', 'عالی', 'موفق', 'قانونی', 'مفید'}
            negative_words = {'منفی', 'بد', 'ناکام', 'غیرقانونی', 'مضر'}
            tokens = set(self.tokenizer.tokenize(self.normalize_text(text)))
            pos_score = len(tokens & positive_words)
            neg_score = len(tokens & negative_words)
            total = pos_score + neg_score
            return (pos_score - neg_score) / total if total > 0 else 0.0
        except Exception as e:
            logger.error(f"Sentiment analysis failed: {e}")
            return 0.0
    
    def extract_legal_entities(self, text: str) -> List[str]:
        if not NLP_AVAILABLE:
            return []
        try:
            patterns = [
                r'قانون\s+[\w\s]+',  # Laws
                r'ماده\s+\d+',       # Articles
                r'دادگاه\s+[\w\s]+', # Courts
                r'[\w\s]+شورا'       # Councils
            ]
            entities = []
            normalized_text = self.normalize_text(text)
            for pattern in patterns:
                matches = re.findall(pattern, normalized_text)
                entities.extend(matches)
            return list(set(entities))
        except Exception as e:
            logger.error(f"Legal entity extraction failed: {e}")
            return []

class EnhancedLegalScraper:
    def __init__(self, delay: float = 2.0, db_path: str = "/app/data/legal_scraper.db"):
        self.nlp = PersianNLPProcessor() if NLP_AVAILABLE else None
        self.session = requests.Session()
        self.delay = delay
        self.last_request_time = 0
        self.db_path = db_path
        self.robots_cache = {}
        self.user_agent = "LegalDataCollector/2.0 (Educational Research; Contact: [email protected])"
        self.session.headers.update({
            'User-Agent': self.user_agent,
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'fa,en;q=0.9',
            'Accept-Encoding': 'gzip, deflate',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1'
        })
        self._init_database()
    
    def _init_database(self):
        try:
            Path(self.db_path).parent.mkdir(parents=True, exist_ok=True)
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()
            cursor.execute('''
                CREATE TABLE IF NOT EXISTS legal_documents (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    title TEXT NOT NULL,
                    content TEXT NOT NULL,
                    source_url TEXT UNIQUE NOT NULL,
                    document_type TEXT NOT NULL,
                    date_published TEXT,
                    date_scraped TEXT NOT NULL,
                    category TEXT,
                    tags TEXT,
                    summary TEXT,
                    importance_score REAL DEFAULT 0.0,
                    sentiment_score REAL DEFAULT 0.0,
                    keywords TEXT,
                    legal_entities TEXT,
                    embedding TEXT,
                    language TEXT DEFAULT 'fa'
                )
            ''')
            cursor.execute('CREATE INDEX IF NOT EXISTS idx_source_url ON legal_documents(source_url)')
            cursor.execute('CREATE INDEX IF NOT EXISTS idx_document_type ON legal_documents(document_type)')
            cursor.execute('CREATE INDEX IF NOT EXISTS idx_date_published ON legal_documents(date_published)')
            conn.commit()
            conn.close()
            logger.info(f"Database initialized: {self.db_path}")
        except Exception as e:
            logger.error(f"Database initialization failed: {e}")
            raise
    
    def _can_fetch(self, url: str) -> bool:
        try:
            domain = urlparse(url).netloc
            if domain not in self.robots_cache:
                robots_url = f"https://{domain}/robots.txt"
                rp = RobotFileParser()
                rp.set_url(robots_url)
                try:
                    rp.read()
                    self.robots_cache[domain] = rp
                except Exception as e:
                    logger.warning(f"Could not read robots.txt for {domain}: {e}")
                    self.robots_cache[domain] = None
            rp = self.robots_cache[domain]
            if rp is None:
                return True
            return rp.can_fetch(self.user_agent, url)
        except Exception as e:
            logger.error(f"Error checking robots.txt for {url}: {e}")
            return True
    
    def _respect_delay(self):
        current_time = time.time()
        time_since_last = current_time - self.last_request_time
        if time_since_last < self.delay:
            time.sleep(self.delay - time_since_last)
        self.last_request_time = time.time()
    
    def _fetch_page(self, url: str, timeout: int = 30) -> Optional[BeautifulSoup]:
        try:
            if not self._can_fetch(url):
                logger.warning(f"Robots.txt disallows fetching: {url}")
                return None
            self._respect_delay()
            logger.info(f"Fetching: {url}")
            response = self.session.get(url, timeout=timeout)
            response.raise_for_status()
            response.encoding = response.apparent_encoding
            return BeautifulSoup(response.text, 'html.parser')
        except requests.RequestException as e:
            logger.error(f"Request failed for {url}: {e}")
            return None
        except Exception as e:
            logger.error(f"Error parsing {url}: {e}")
            return None
    
    def _extract_article_title(self, soup: BeautifulSoup) -> str:
        selectors = [
            'h1.title', 'h1', '.article-title', '.post-title',
            '.news-title', 'title', '.headline'
        ]
        for selector in selectors:
            elem = soup.select_one(selector)
            if elem:
                title = elem.get_text(strip=True)
                if title and len(title) > 10:
                    return title
        return "Unknown Title"
    
    def _extract_article_content(self, soup: BeautifulSoup) -> str:
        for unwanted in soup(['script', 'style', 'nav', 'header', 'footer', 'aside']):
            unwanted.decompose()
        selectors = [
            '.article-content', '.post-content', '.news-content',
            '.content', 'article', '.main-content', 'main'
        ]
        for selector in selectors:
            elem = soup.select_one(selector)
            if elem:
                content = elem.get_text(strip=True)
                if len(content) > 200:
                    return content
        body = soup.find('body')
        if body:
            return body.get_text(strip=True)
        return soup.get_text(strip=True)
    
    def _extract_article_date(self, soup: BeautifulSoup) -> Optional[str]:
        date_meta = soup.find('meta', {'name': 'date'}) or soup.find('meta', {'property': 'article:published_time'})
        if date_meta:
            return date_meta.get('content')
        date_selectors = ['.date', '.published', '.timestamp', '.article-date']
        for selector in date_selectors:
            elem = soup.select_one(selector)
            if elem:
                date_text = elem.get_text(strip=True)
                patterns = [
                    r'(\d{4}/\d{1,2}/\d{1,2})',
                    r'(\d{1,2}/\d{1,2}/\d{4})',
                    r'(\d{4}-\d{1,2}-\d{1,2})'
                ]
                for pattern in patterns:
                    match = re.search(pattern, date_text)
                    if match:
                        return match.group(1)
        return None
    
    def _calculate_importance(self, doc_type: str, content: str) -> float:
        if not self.nlp:
            return 0.5
        keywords = self.nlp.extract_keywords(content)
        important_terms = {'قانون', 'ماده', 'دادگاه', 'حکم', 'آیین‌نامه', 'مصوبه'}
        score = 0.5
        if doc_type == 'law' or doc_type == 'ruling':
            score += 0.3
        if any(term in keywords for term in important_terms):
            score += 0.2
        return min(score, 1.0)
    
    def scrape_real_sources(self, source_urls: List[str] = None, max_docs: int = 10) -> List[LegalDocument]:
        if not source_urls:
            source_urls = IRANIAN_LEGAL_SOURCES
        documents = []
        max_docs_per_source = max_docs // len(source_urls) + 1
        for base_url in source_urls:
            try:
                is_majlis = 'rc.majlis.ir' in base_url
                if is_majlis:
                    # Scrape laws from Majlis
                    law_urls = [f"{base_url}/fa/law/show/{i}" for i in range(100000, 100000 + max_docs_per_source)]
                    for url in law_urls[:max_docs_per_source]:
                        try:
                            soup = self._fetch_page(url)
                            if not soup:
                                continue
                            title = self._extract_article_title(soup)
                            content = self._extract_article_content(soup)
                            if len(content) < 100:
                                continue
                            date_published = self._extract_article_date(soup)
                            doc = LegalDocument(
                                title=title,
                                content=content,
                                source_url=url,
                                document_type="law",
                                date_published=date_published,
                                category="legislation",
                                tags=["قانون", "مجلس"]
                            )
                            if self.nlp:
                                doc.summary = self.nlp.generate_summary(content)
                                doc.keywords = self.nlp.extract_keywords(content)
                                doc.sentiment_score = self.nlp.calculate_sentiment(content)
                                doc.legal_entities = self.nlp.extract_legal_entities(content)
                                doc.embedding = self.nlp.get_embedding(content)
                                doc.importance_score = self._calculate_importance("law", content)
                            documents.append(doc)
                            self.save_document(doc)
                            logger.info(f"Scraped law: {title[:50]}...")
                        except Exception as e:
                            logger.error(f"Error scraping law {url}: {e}")
                            continue
                else:
                    # Scrape news articles
                    soup = self._fetch_page(base_url)
                    if not soup:
                        continue
                    article_links = []
                    for link in soup.find_all('a', href=True):
                        href = link['href']
                        full_url = urljoin(base_url, href)
                        if any(keyword in href.lower() for keyword in ['news', 'article', 'post', 'اخبار']):
                            article_links.append(full_url)
                    article_links = article_links[:max_docs_per_source]
                    for article_url in article_links:
                        try:
                            article_soup = self._fetch_page(article_url)
                            if not article_soup:
                                continue
                            title = self._extract_article_title(article_soup)
                            content = self._extract_article_content(article_soup)
                            if len(content) < 100:
                                continue
                            date_published = self._extract_article_date(article_soup)
                            doc = LegalDocument(
                                title=title,
                                content=content,
                                source_url=article_url,
                                document_type="news",
                                date_published=date_published,
                                category="legal_news",
                                tags=["اخبار", "حقوقی"]
                            )
                            if self.nlp:
                                doc.summary = self.nlp.generate_summary(content)
                                doc.keywords = self.nlp.extract_keywords(content)
                                doc.sentiment_score = self.nlp.calculate_sentiment(content)
                                doc.legal_entities = self.nlp.extract_legal_entities(content)
                                doc.embedding = self.nlp.get_embedding(content)
                                doc.importance_score = self._calculate_importance("news", content)
                            documents.append(doc)
                            self.save_document(doc)
                            logger.info(f"Scraped news: {title[:50]}...")
                        except Exception as e:
                            logger.error(f"Error scraping news {article_url}: {e}")
                            continue
            except Exception as e:
                logger.error(f"Error scraping source {base_url}: {e}")
                continue
        return documents[:max_docs]
    
    def save_document(self, doc: LegalDocument) -> bool:
        try:
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()
            cursor.execute('''
                INSERT OR REPLACE INTO legal_documents 
                (title, content, source_url, document_type, date_published, 
                 date_scraped, category, tags, summary, importance_score, 
                 sentiment_score, keywords, legal_entities, embedding, language)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            ''', (
                doc.title,
                doc.content,
                doc.source_url,
                doc.document_type,
                doc.date_published,
                doc.date_scraped,
                doc.category,
                json.dumps(doc.tags, ensure_ascii=False),
                doc.summary,
                doc.importance_score,
                doc.sentiment_score,
                json.dumps(doc.keywords, ensure_ascii=False),
                json.dumps(doc.legal_entities, ensure_ascii=False),
                json.dumps(doc.embedding, ensure_ascii=False),
                doc.language
            ))
            conn.commit()
            conn.close()
            return True
        except Exception as e:
            logger.error(f"Failed to save document {doc.source_url}: {e}")
            return False
    
    def _text_search(self, query: str, limit: int = 20) -> List[Dict]:
        try:
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()
            normalized_query = self.nlp.normalize_text(query) if self.nlp else query
            query_words = normalized_query.split()
            like_clauses = [f"content LIKE '%{word}%'" for word in query_words]
            query_sql = f'''
                SELECT title, content, source_url, document_type, date_published, 
                       date_scraped, category, tags, summary, importance_score, 
                       sentiment_score, keywords, legal_entities, embedding, language
                FROM legal_documents 
                WHERE {' AND '.join(like_clauses)}
                ORDER BY importance_score DESC, date_scraped DESC
                LIMIT ?
            '''
            cursor.execute(query_sql, (limit,))
            rows = cursor.fetchall()
            columns = [description[0] for description in cursor.description]
            results = []
            for row in rows:
                doc_dict = dict(zip(columns, row))
                doc_dict['tags'] = json.loads(doc_dict['tags']) if doc_dict['tags'] else []
                doc_dict['keywords'] = json.loads(doc_dict['keywords']) if doc_dict['keywords'] else []
                doc_dict['legal_entities'] = json.loads(doc_dict['legal_entities']) if doc_dict['legal_entities'] else []
                doc_dict['embedding'] = json.loads(doc_dict['embedding']) if doc_dict['embedding'] else []
                results.append(doc_dict)
            conn.close()
            return results
        except Exception as e:
            logger.error(f"Text search failed: {e}")
            return []
    
    def search_with_similarity(self, query: str, limit: int = 20) -> List[Dict]:
        if not self.nlp or not NLP_AVAILABLE:
            return self._text_search(query, limit)
        try:
            query_embedding = self.nlp.get_embedding(query)
            if not query_embedding:
                return self._text_search(query, limit)
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()
            cursor.execute('''
                SELECT title, content, source_url, document_type, date_published, 
                       date_scraped, category, tags, summary, importance_score, 
                       sentiment_score, keywords, legal_entities, embedding, language
                FROM legal_documents
                ORDER BY importance_score DESC, date_scraped DESC
            ''')
            rows = cursor.fetchall()
            columns = [description[0] for description in cursor.description]
            documents = []
            for row in rows:
                doc_dict = dict(zip(columns, row))
                doc_dict['tags'] = json.loads(doc_dict['tags']) if doc_dict['tags'] else []
                doc_dict['keywords'] = json.loads(doc_dict['keywords']) if doc_dict['keywords'] else []
                doc_dict['legal_entities'] = json.loads(doc_dict['legal_entities']) if doc_dict['legal_entities'] else []
                doc_dict['embedding'] = json.loads(doc_dict['embedding']) if doc_dict['embedding'] else []
                documents.append(doc_dict)
            conn.close()
            if not documents:
                return []
            results = []
            query_embedding = np.array(query_embedding).reshape(1, -1)
            for doc in documents:
                if not doc['embedding']:
                    continue
                doc_embedding = np.array(doc['embedding']).reshape(1, -1)
                similarity = cosine_similarity(query_embedding, doc_embedding)[0][0]
                doc['similarity_score'] = float(similarity)
                results.append(doc)
            results.sort(key=lambda x: (x['similarity_score'], x['importance_score']), reverse=True)
            return results[:limit]
        except Exception as e:
            logger.error(f"Similarity search failed: {e}")
            return self._text_search(query, limit)
    
    def export_to_csv(self, filename: str = None) -> bool:
        if filename is None:
            filename = f"/app/data/legal_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
        try:
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()
            cursor.execute('SELECT * FROM legal_documents ORDER BY date_scraped DESC')
            rows = cursor.fetchall()
            columns = [description[0] for description in cursor.description]
            df = pd.DataFrame(rows, columns=columns)
            for col in ['tags', 'keywords', 'legal_entities', 'embedding']:
                if col in df.columns:
                    df[col] = df[col].apply(lambda x: json.loads(x) if x else [])
            df.to_csv(filename, index=False, encoding='utf-8')
            conn.close()
            logger.info(f"Data exported to {filename}")
            return True
        except Exception as e:
            logger.error(f"Export failed: {e}")
            return False
    
    def get_enhanced_statistics(self) -> Dict:
        try:
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()
            stats = {}
            cursor.execute('SELECT COUNT(*) FROM legal_documents')
            stats['total_documents'] = cursor.fetchone()[0]
            cursor.execute('SELECT document_type, COUNT(*) FROM legal_documents GROUP BY document_type')
            stats['by_type'] = dict(cursor.fetchall())
            cursor.execute('SELECT category, COUNT(*) FROM legal_documents GROUP BY category')
            stats['by_category'] = dict(cursor.fetchall())
            cursor.execute('''
                SELECT DATE(date_scraped) as day, COUNT(*) 
                FROM legal_documents 
                GROUP BY DATE(date_scraped) 
                ORDER BY day DESC 
                LIMIT 7
            ''')
            stats['recent_activity'] = dict(cursor.fetchall())
            cursor.execute('SELECT keywords FROM legal_documents WHERE keywords IS NOT NULL')
            all_keywords = []
            for row in cursor.fetchall():
                keywords = json.loads(row[0]) if row[0] else []
                all_keywords.extend(keywords)
            keyword_freq = {}
            for kw in all_keywords:
                keyword_freq[kw] = keyword_freq.get(kw, 0) + 1
            stats['top_keywords'] = dict(sorted(keyword_freq.items(), key=lambda x: x[1], reverse=True)[:10])
            cursor.execute('''
                SELECT 
                    SUM(CASE WHEN importance_score > 0.7 THEN 1 ELSE 0 END) as high,
                    SUM(CASE WHEN importance_score BETWEEN 0.3 AND 0.7 THEN 1 ELSE 0 END) as medium,
                    SUM(CASE WHEN importance_score < 0.3 THEN 1 ELSE 0 END) as low
                FROM legal_documents
            ''')
            imp_dist = cursor.fetchone()
            stats['importance_distribution'] = {
                'high': imp_dist[0] or 0,
                'medium': imp_dist[1] or 0,
                'low': imp_dist[2] or 0
            }
            conn.close()
            return stats
        except Exception as e:
            logger.error(f"Statistics failed: {e}")
            return {}
```