
    v_i                         d Z ddlmZmZ ddlmZmZmZ ddlm	Z	 efde
de
defd	Zd
ee   dee   fdZdee   deeee   f   fdZdee   dee   fdZy)z5Cross-source merging and scoring for trending topics.    )ListDict)MAX_ITEMS_PER_SOURCECROSS_SOURCE_BOOSTMIN_SOURCES_FOR_BOOST   )deduplicate_topicsrankmax_rankreturnc                     d| dz
  |z  z
  S )z
    Convert rank to a 0-1 score (higher = better).

    Args:
        rank: Position in source list (1 = best)
        max_rank: Maximum possible rank

    Returns:
        Normalized score between 0 and 1
    g      ?r    )r
   r   s     ;/home/sietch6/trending-topics-pipeline/aggregator/merger.pynormalize_scorer   	   s     4!8x'((    itemsc                 6   | sg S | D ]  }t        |d         |d<    t        |       }|D ]>  }|d   }|j                  dd      }|t        k\  r|t        z  |d<   d|d<   5||d<   d|d<   @ |j                  d	 d
       t        |d      D ]
  \  }}||d<    |S )a  
    Merge items from all sources with equal weighting and cross-source boosting.

    1. Normalize ranks to 0-1 scores per source
    2. Deduplicate with fuzzy matching
    3. Apply cross-source boost for topics in 2+ sources
    4. Sort by final score

    Args:
        items: List of trend item dictionaries from all sources

    Returns:
        Merged, deduplicated, and scored list
    r
   normalized_scoresource_countr   final_scoreTboostedFc                     | d   S )Nr   r   )xs    r   <lambda>z!merge_and_score.<locals>.<lambda>>   s    q/ r   )keyreverse)start
final_rank)r   r	   getr   r   sort	enumerate)r   itemdeduped
base_scorer   is         r   merge_and_scorer&      s     	  A#24<#@ A !'G  
$,-
xx2 00",/A"AD"DO",D#DO
$ LL/L> WA. 4\ Nr   	all_itemsc                     i }| D ]$  }|d   }||vrg ||<   ||   j                  |       & |j                         D ci c]  \  }}|t        |       c}}S c c}}w )z
    Merge items grouped by region.

    Args:
        all_items: List of all trend items from all sources and regions

    Returns:
        Dictionary mapping region to merged/scored trend list
    region)appendr   r&   )r'   	by_regionr"   r)   r   s        r   merge_by_regionr,   G   su     (*I  'h" "If&  &	' AJ@QR}vuFOE**RRRs   Ac                     t        |       S )a  
    Merge all items across all regions into a single list.

    Useful for seeing overall trending topics regardless of region.

    Args:
        all_items: List of all trend items from all sources and regions

    Returns:
        Single merged/scored trend list
    )r&   )r'   s    r   merge_all_regionsr.   ^   s     9%%r   N)__doc__typingr   r   configr   r   r   deduplicatorr	   intfloatr   dictr&   strr,   r.   r   r   r   <module>r7      s    ;  R R , 0D )# ) ) )-4: -$t* -`StDz Sd3T
?.C S.&d &T
 &r   