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
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
"""
Intelligent Benchmark & Exam Scraper

Scrapes the web to find domain-specific questions, scenarios, and test content.
Automatically builds comprehensive benchmarks for any use case.
"""

import re
import json
import requests
from typing import List, Dict, Optional
from pathlib import Path
import time
from bs4 import BeautifulSoup
from urllib.parse import quote_plus


class IntelligentBenchmarkScraper:
    """
    Scrapes web sources to build domain-specific benchmarks and exams.

    Features:
    - Web search for relevant content
    - Multi-source scraping (Wikipedia, educational sites, forums, documentation)
    - Question extraction and generation
    - Quality scoring and filtering
    - Benchmark formatting
    """

    def __init__(self, api_key: Optional[str] = None):
        """
        Initialize scraper.

        Args:
            api_key: OpenAI/Anthropic key for question generation from scraped content
        """
        self.api_key = api_key
        self.session = requests.Session()
        self.session.headers.update({
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        })

    def search_web(self, query: str, num_results: int = 10) -> List[Dict]:
        """
        Search the web for relevant content using DuckDuckGo (no API key needed).

        Args:
            query: Search query
            num_results: Number of results to return

        Returns:
            List of search results with title, URL, snippet
        """
        results = []

        try:
            # Use DuckDuckGo HTML search (no API required)
            search_url = f"https://html.duckduckgo.com/html/?q={quote_plus(query)}"

            response = self.session.get(search_url, timeout=10)
            response.raise_for_status()

            soup = BeautifulSoup(response.text, 'html.parser')

            # Parse results
            for result_div in soup.find_all('div', class_='result')[:num_results]:
                title_elem = result_div.find('a', class_='result__a')
                snippet_elem = result_div.find('a', class_='result__snippet')

                if title_elem and snippet_elem:
                    results.append({
                        'title': title_elem.get_text(strip=True),
                        'url': title_elem['href'],
                        'snippet': snippet_elem.get_text(strip=True)
                    })

        except Exception as e:
            print(f"Search error: {e}")
            # Fallback: provide some generic sources
            results = self._get_fallback_sources(query)

        return results

    def _get_fallback_sources(self, query: str) -> List[Dict]:
        """Provide fallback educational sources when search fails."""
        domain_keywords = query.lower()

        sources = []

        # Wikipedia
        wiki_topic = query.replace(' ', '_')
        sources.append({
            'title': f"Wikipedia: {query}",
            'url': f"https://en.wikipedia.org/wiki/{wiki_topic}",
            'snippet': f"Comprehensive overview of {query}"
        })

        # Add domain-specific sources
        if 'financial' in domain_keywords or 'finance' in domain_keywords:
            sources.extend([
                {
                    'title': "Investopedia: Financial Certification Exams",
                    'url': "https://www.investopedia.com/",
                    'snippet': "Financial education and exam prep"
                },
                {
                    'title': "CFP Board Practice Questions",
                    'url': "https://www.cfp.net/",
                    'snippet': "CFP certification resources"
                }
            ])
        elif 'medical' in domain_keywords or 'health' in domain_keywords:
            sources.extend([
                {
                    'title': "NCBI Medical Resources",
                    'url': "https://www.ncbi.nlm.nih.gov/",
                    'snippet': "Medical knowledge base"
                },
                {
                    'title': "MedlinePlus Health Topics",
                    'url': "https://medlineplus.gov/",
                    'snippet': "Consumer health information"
                }
            ])
        elif 'legal' in domain_keywords or 'law' in domain_keywords:
            sources.extend([
                {
                    'title': "Cornell Legal Information Institute",
                    'url': "https://www.law.cornell.edu/",
                    'snippet': "Free legal resources and case law"
                }
            ])

        return sources

    def scrape_content(self, url: str) -> str:
        """
        Scrape text content from a URL.

        Args:
            url: URL to scrape

        Returns:
            Extracted text content
        """
        try:
            response = self.session.get(url, timeout=15)
            response.raise_for_status()

            soup = BeautifulSoup(response.text, 'html.parser')

            # Remove script and style elements
            for script in soup(['script', 'style', 'header', 'footer', 'nav']):
                script.decompose()

            # Get text
            text = soup.get_text()

            # Clean up
            lines = (line.strip() for line in text.splitlines())
            chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
            text = ' '.join(chunk for chunk in chunks if chunk)

            # Limit size
            return text[:10000]  # Max 10K chars per page

        except Exception as e:
            print(f"Scraping error for {url}: {e}")
            return ""

    def extract_questions_from_text(self, text: str, max_questions: int = 20) -> List[Dict]:
        """
        Extract questions from text using pattern matching.

        Args:
            text: Text content to analyze
            max_questions: Maximum questions to extract

        Returns:
            List of question dicts
        """
        questions = []

        # Pattern 1: Questions with answers
        # Example: "What is X? Y is..."
        qa_pattern = r'(?:^|\n)([^.!?]*\?)\s*([^.!?]+[.!?])'
        matches = re.findall(qa_pattern, text)

        for question, answer in matches[:max_questions//2]:
            question = question.strip()
            answer = answer.strip()

            if len(question) > 20 and len(answer) > 20:
                questions.append({
                    'question': question,
                    'answer': answer,
                    'type': 'extracted'
                })

        # Pattern 2: Numbered/bulleted questions
        numbered_pattern = r'(?:^|\n)\s*(?:\d+[\.\)]\s*|[•\-\*]\s*)([^.!?]*\?)'
        numbered_matches = re.findall(numbered_pattern, text)

        for question in numbered_matches[:max_questions//2]:
            question = question.strip()
            if len(question) > 20:
                questions.append({
                    'question': question,
                    'answer': "",  # Will be generated later
                    'type': 'extracted_no_answer'
                })

        return questions[:max_questions]

    def generate_questions_from_content(self, content: str, domain: str, num_questions: int = 10) -> List[Dict]:
        """
        Generate questions from content using LLM.

        Args:
            content: Source content
            domain: Domain/topic
            num_questions: Number of questions to generate

        Returns:
            List of generated questions
        """
        if not self.api_key:
            # Fallback: use simple templates
            return self._generate_template_questions(content, domain, num_questions)

        try:
            # Try OpenAI first
            if self.api_key.startswith('sk-'):
                from openai import OpenAI
                client = OpenAI(api_key=self.api_key)

                prompt = f"""Based on the following content about {domain}, generate {num_questions} test questions with answers.

Content:
{content[:3000]}

Format each question as JSON:
{{"question": "...", "answer": "...", "difficulty": "beginner|intermediate|advanced"}}

Return a JSON array of questions."""

                response = client.chat.completions.create(
                    model="gpt-3.5-turbo",
                    messages=[
                        {"role": "system", "content": "You are an expert test creator."},
                        {"role": "user", "content": prompt}
                    ],
                    temperature=0.7
                )

                # Parse JSON response
                content_text = response.choices[0].message.content
                json_match = re.search(r'\[.*\]', content_text, re.DOTALL)
                if json_match:
                    questions = json.loads(json_match.group())
                    return questions

            # Try Anthropic
            elif self.api_key.startswith('sk-ant-'):
                from anthropic import Anthropic
                client = Anthropic(api_key=self.api_key)

                prompt = f"""Based on the following content about {domain}, generate {num_questions} test questions with answers.

Content:
{content[:3000]}

Format each question as JSON:
{{"question": "...", "answer": "...", "difficulty": "beginner|intermediate|advanced"}}

Return a JSON array of questions."""

                response = client.messages.create(
                    model="claude-3-5-sonnet-20241022",
                    max_tokens=2000,
                    messages=[
                        {"role": "user", "content": prompt}
                    ]
                )

                # Parse JSON response
                content_text = response.content[0].text
                json_match = re.search(r'\[.*\]', content_text, re.DOTALL)
                if json_match:
                    questions = json.loads(json_match.group())
                    return questions

        except Exception as e:
            print(f"LLM generation error: {e}")

        # Fallback
        return self._generate_template_questions(content, domain, num_questions)

    def _generate_template_questions(self, content: str, domain: str, num_questions: int) -> List[Dict]:
        """Generate basic questions using templates when no API available."""
        questions = []

        # Extract key terms (simple approach)
        words = content.split()
        unique_words = list(set([w for w in words if len(w) > 5]))[:num_questions]

        templates = [
            ("What is {term}?", "answer_placeholder"),
            ("Explain the concept of {term}.", "answer_placeholder"),
            ("How does {term} work in the context of {domain}?", "answer_placeholder"),
            ("What are the key aspects of {term}?", "answer_placeholder"),
        ]

        for i, term in enumerate(unique_words[:num_questions]):
            template = templates[i % len(templates)]
            questions.append({
                'question': template[0].format(term=term, domain=domain),
                'answer': f"This question requires domain expertise in {domain} regarding {term}.",
                'difficulty': 'intermediate',
                'type': 'template_generated'
            })

        return questions

    def build_benchmark(
        self,
        domain: str,
        num_questions: int = 50,
        use_llm: bool = True
    ) -> Dict:
        """
        Build a comprehensive benchmark for a domain.

        Args:
            domain: Domain/topic (e.g., "financial planning", "medical diagnostics")
            num_questions: Target number of questions
            use_llm: Whether to use LLM for question generation

        Returns:
            Benchmark dict with questions
        """
        print(f"Building benchmark for: {domain}")
        print(f"Target questions: {num_questions}")

        all_questions = []

        # Step 1: Search for relevant content
        print("\n[1/4] Searching web for content...")
        search_queries = [
            f"{domain} practice questions",
            f"{domain} exam questions",
            f"{domain} test scenarios",
            f"{domain} certification study guide"
        ]

        all_sources = []
        for query in search_queries:
            sources = self.search_web(query, num_results=5)
            all_sources.extend(sources)
            time.sleep(1)  # Rate limiting

        print(f"Found {len(all_sources)} sources")

        # Step 2: Scrape content from sources
        print("\n[2/4] Scraping content from sources...")
        scraped_content = []
        for i, source in enumerate(all_sources[:10]):  # Limit to 10 sources
            print(f"  Scraping {i+1}/10: {source['title'][:50]}...")
            content = self.scrape_content(source['url'])
            if content:
                scraped_content.append({
                    'url': source['url'],
                    'title': source['title'],
                    'content': content
                })
            time.sleep(1)  # Be polite

        print(f"Successfully scraped {len(scraped_content)} pages")

        # Step 3: Extract existing questions
        print("\n[3/4] Extracting questions from content...")
        for item in scraped_content:
            extracted = self.extract_questions_from_text(item['content'])
            for q in extracted:
                q['source'] = item['url']
                q['source_title'] = item['title']
            all_questions.extend(extracted)

        print(f"Extracted {len(all_questions)} questions from sources")

        # Step 4: Generate additional questions if needed
        if use_llm and len(all_questions) < num_questions:
            print("\n[4/4] Generating additional questions using LLM...")
            remaining = num_questions - len(all_questions)

            # Use best content for generation
            best_content = max(scraped_content, key=lambda x: len(x['content']))['content'] if scraped_content else ""

            if best_content:
                generated = self.generate_questions_from_content(
                    best_content,
                    domain,
                    num_questions=remaining
                )
                all_questions.extend(generated)
                print(f"Generated {len(generated)} additional questions")

        # Build final benchmark
        benchmark = {
            'name': f"{domain.title()} Benchmark",
            'domain': domain,
            'description': f"Automatically generated benchmark for {domain} with {len(all_questions)} questions",
            'created_at': time.strftime('%Y-%m-%d %H:%M:%S'),
            'num_questions': len(all_questions),
            'sources': [s['url'] for s in scraped_content],
            'questions': all_questions[:num_questions]
        }

        print(f"\n[OK] Benchmark created with {len(benchmark['questions'])} questions")

        return benchmark

    def save_benchmark(self, benchmark: Dict, filepath: str):
        """Save benchmark to JSON file."""
        Path(filepath).parent.mkdir(parents=True, exist_ok=True)

        with open(filepath, 'w', encoding='utf-8') as f:
            json.dump(benchmark, f, indent=2, ensure_ascii=False)

        print(f"Saved benchmark to: {filepath}")


def create_scraped_benchmark(domain: str, num_questions: int = 50, api_key: Optional[str] = None) -> str:
    """
    Helper function to create a benchmark from web scraping.

    Args:
        domain: Domain/topic
        num_questions: Number of questions
        api_key: Optional API key for LLM generation

    Returns:
        Status message
    """
    scraper = IntelligentBenchmarkScraper(api_key=api_key)

    benchmark = scraper.build_benchmark(
        domain=domain,
        num_questions=num_questions,
        use_llm=bool(api_key)
    )

    # Save
    filename = domain.lower().replace(' ', '_')
    filepath = f"benchmarks/{filename}_benchmark.json"
    scraper.save_benchmark(benchmark, filepath)

    return filepath, benchmark


if __name__ == "__main__":
    # Test
    import sys

    domain = sys.argv[1] if len(sys.argv) > 1 else "financial planning"

    scraper = IntelligentBenchmarkScraper()
    benchmark = scraper.build_benchmark(domain, num_questions=20, use_llm=False)

    print("\nSample questions:")
    for i, q in enumerate(benchmark['questions'][:3], 1):
        print(f"\n{i}. {q['question']}")
        if q.get('answer'):
            print(f"   A: {q['answer'][:100]}...")