import json
import re

# Load all data
with open(r'C:\temp\scoring_data.json', encoding='utf-8') as f:
    data = json.load(f)

pairs = data['pairs']
req_map = data['req_map']

# Scoring function - this is where the actual judgment happens
def score_pair(id_a, id_b, req_map):
    """Score confusability of two requirements (0-100)"""
    
    req_a = req_map.get(id_a, {})
    req_b = req_map.get(id_b, {})
    
    title_a = (req_a.get('title') or '').lower()
    title_b = (req_b.get('title') or '').lower()
    doc_a = (req_a.get('doc') or '').lower()
    doc_b = (req_b.get('doc') or '').lower()
    
    # Extract key concepts from titles (first ~100 chars often has the core promise)
    title_a_short = req_a.get('title', '')[:150]
    title_b_short = req_b.get('title', '')[:150]
    
    # Some heuristics to help organize the thinking:
    # 1. Are they about completely different subsystems? (Network vs Picker, for example)
    # 2. Do they share terminology? (Same words != same requirement, but relevant context)
    # 3. Do they describe present vs absent sides of one guarantee?
    # 4. Do they describe the same concept at different layers?
    
    # Extract key terms
    def extract_key_terms(text):
        # Remove common words and extract domain terms
        words = re.findall(r'\b[a-z_-]+\b', text[:300])
        # Filter out very common words
        common = {'the', 'a', 'an', 'is', 'are', 'of', 'to', 'in', 'on', 'at', 'by', 'with', 'and', 'or', 'not', 'must', 'does', 'when', 'never', 'always', 'be', 'as'}
        return [w for w in words if w not in common and len(w) > 2]
    
    terms_a = set(extract_key_terms(title_a + ' ' + doc_a))
    terms_b = set(extract_key_terms(title_b + ' ' + doc_b))
    
    overlap = len(terms_a & terms_b)
    union = len(terms_a | terms_b)
    
    # Basic Jaccard-like similarity (but NOT the score - just context)
    term_overlap = overlap / max(union, 1) if union > 0 else 0
    
    # Now the actual judgment (this requires reading the requirements):
    # This is the human judgment part - I need to think about what each req actually means
    
    # I'll use a combination of factors but the ultimate score comes from understanding
    # what each requirement is actually saying
    
    # For now, return the raw similarity for analysis
    return {
        'id_a': id_a,
        'id_b': id_b,
        'title_a': title_a_short,
        'title_b': title_b_short,
        'term_overlap_ratio': term_overlap,
        'shared_terms': list(terms_a & terms_b)[:10],
        'needs_human_judgment': True
    }

# Analyze all pairs
analysis = []
for i, pair in enumerate(pairs[:10]):  # Start with first 10
    result = score_pair(pair['a'], pair['b'], req_map)
    result['index'] = i
    analysis.append(result)

# Display analysis
for a in analysis:
    print(f"\n[{a['index']}] {a['id_a']} <-> {a['id_b']}")
    print(f"  A: {a['title_a'][:80]}...")
    print(f"  B: {a['title_b'][:80]}...")
    print(f"  Term overlap: {a['term_overlap_ratio']:.2f}, shared: {a['shared_terms'][:5]}")

