
"""
纹样基因值B22计算模块

纹样基因值B22 = (结构复杂度SC × 0.6 + 归一化信息熵H × 0.4) × 历史传承度HI × 10

"""

import math
from typing import Dict, List


class PatternGeneB22Calculator:
    """纹样基因值B22计算器"""
    
    def __init__(self):
        """初始化计算器"""
        pass
    
    def calculate_pattern_gene_b22(self,
                                  structure_complexity: float,
                                  normalized_entropy: float,
                                  historical_inheritance: float) -> float:
        """
        计算纹样基因值B22
        
        
        纹样基因值B22 = (结构复杂度SC × 0.6 + 归一化信息熵H × 0.4) × 历史传承度HI × 10
        
        args:
            structure_complexity: 结构复杂度SC (系统计算)
            normalized_entropy: 归一化信息熵H (系统计算)
            historical_inheritance: 历史传承度HI (用户填写)
            
        return:
            float: 纹样基因值B22
        """
        
        pattern_gene = ((structure_complexity * 0.6 + 
                        normalized_entropy * 0.4) * 
                       historical_inheritance * 10)
        
        return pattern_gene
    
    def calculate_structure_complexity(self, pattern_elements: List[Dict]) -> float:
        """
        计算结构复杂度SC
        
        结构复杂度 = Σ(元素权重 × 复杂度系数) / 总元素数
        
        args:
            pattern_elements: 纹样元素列表，每个元素包含 {type: str, weight: float, complexity: float} (用户填写)
            
        return:
            float: 结构复杂度SC
        """
        if not pattern_elements:
            return 0.0
        
        total_weighted_complexity = 0.0
        total_weight = 0.0
        
        for element in pattern_elements:
            weight = element.get('weight', 1.0)
            complexity = element.get('complexity', 0.0)
            total_weighted_complexity += weight * complexity
            total_weight += weight
        
        if total_weight == 0:
            return 0.0
        
        structure_complexity = total_weighted_complexity / total_weight
        return structure_complexity
    
    def calculate_normalized_entropy(self, pattern_data: List[float]) -> float:
        """
        计算归一化信息熵H
        
        归一化信息熵 = -Σ(p_i × log2(p_i)) / log2(n)
        
        args:
            pattern_data: 纹样数据列表 (用户填写)
            
        return:
            float: 归一化信息熵H
        """
        if not pattern_data or len(pattern_data) <= 1:
            return 0.0
        
        # 计算概率分布
        total = sum(pattern_data)
        if total == 0:
            return 0.0
        
        probabilities = [x / total for x in pattern_data if x > 0]
        
        # 计算信息熵
        entropy = 0.0
        for p in probabilities:
            if p > 0:
                entropy -= p * math.log2(p)
        
        # 归一化
        n = len(probabilities)
        if n <= 1:
            return 0.0
        
        normalized_entropy = entropy / math.log2(n)
        return normalized_entropy
    


# 示例使用
if __name__ == "__main__":

    calculator = PatternGeneB22Calculator()
    
    # 示例数据
    pattern_elements = [
        {'type': '几何图形', 'weight': 0.3, 'complexity': 0.7},
        {'type': '植物纹样', 'weight': 0.4, 'complexity': 0.8},
        {'type': '动物纹样', 'weight': 0.3, 'complexity': 0.6}
    ]
    entropy_data = [0.3, 0.4, 0.3]
    inheritance_years = 500  # 传承年数 (用户填写)
    cultural_significance = "国家级"  # 文化意义等级 (用户填写)
    preservation_status = "良好"  # 保护状况 (用户填写)
    historical_inheritance = 100.0
    
    # 计算各项指标
    structure_complexity = calculator.calculate_structure_complexity(pattern_elements)
    normalized_entropy = calculator.calculate_normalized_entropy(entropy_data)

    
    # 计算纹样基因值B22
    pattern_gene_b22 = calculator.calculate_pattern_gene_b22(
        1.5, 9, historical_inheritance
    )
    
    print(f"结构复杂度SC: {structure_complexity:.4f}")
    print(f"归一化信息熵H: {normalized_entropy:.4f}")
    print(f"历史传承度HI: {historical_inheritance:.4f}")
    print(f"纹样基因值B22: {pattern_gene_b22:.4f}")
