""" Coreference Chain Annotation Layout Generates the UI for creating and managing coreference chains — groupings of text spans that refer to the same entity. This schema type works in conjunction with a span annotation schema. A coreference chain is an n-ary undirected link where span_ids lists all mentions of the same entity. It leverages the existing SpanLink infrastructure with a specialized chain management UI. """ import logging import json from .identifier_utils import ( safe_generate_layout, escape_html_content, ) logger = logging.getLogger(__name__) # Default colors for coreference chains CHAIN_COLOR_PALETTE = [ "#6E56CF", # Purple "#EF4444", # Red "#22C55E", # Green "#3B82F6", # Blue "#F59E0B", # Amber "#EC4899", # Pink "#06B6D4", # Cyan "#F97316", # Orange "#8B5CF6", # Violet "#10B981", # Emerald "#DC2626", # Dark red "#A855F7", # Light purple "#14B8A6", # Teal "#F43F5E", # Rose "#84CC16", # Lime ] def _generate_coreference_layout_internal(annotation_scheme, horizontal=False): """ Internal function to generate coreference chain layout. Args: annotation_scheme: Configuration dictionary containing: - name: Schema name - description: Description shown to user - span_schema: Name of the span schema providing mentions - entity_types: Optional list of entity types for chain classification - allow_singletons: Whether single-mention chains are allowed (default: True) - visual_display: - highlight_mode: "bracket" | "background" | "underline" (default: "background") Returns: tuple: (HTML string, key bindings list) """ scheme_name = annotation_scheme["name"] description = annotation_scheme.get("description", "Create coreference chains") span_schema = annotation_scheme.get("span_schema", "") entity_types = annotation_scheme.get("entity_types", []) allow_singletons = annotation_scheme.get("allow_singletons", True) visual_display = annotation_scheme.get("visual_display", {}) highlight_mode = visual_display.get("highlight_mode", "background") # Build entity type selector HTML entity_types_html = "" if entity_types: for i, etype in enumerate(entity_types): if isinstance(etype, dict): etype_name = etype.get("name", f"Entity_{i}") etype_color = etype.get("color", CHAIN_COLOR_PALETTE[i % len(CHAIN_COLOR_PALETTE)]) else: etype_name = str(etype) etype_color = CHAIN_COLOR_PALETTE[i % len(CHAIN_COLOR_PALETTE)] entity_types_html += f"""
""" # Config data for JS config_data = json.dumps({ "schemaName": scheme_name, "spanSchema": span_schema, "entityTypes": entity_types if entity_types else [], "allowSingletons": allow_singletons, "highlightMode": highlight_mode, "colors": CHAIN_COLOR_PALETTE, }) entity_type_section = "" if entity_types: entity_type_section = f"""
{entity_types_html}
""" schematic = f"""

{escape_html_content(description)}

0 chains
{entity_type_section}

No coreference chains created yet. Select spans and click "New Chain" to start.

""" key_bindings = [] return schematic, key_bindings def generate_coreference_layout(annotation_scheme, horizontal=False): """ Generate coreference chain layout HTML. Args: annotation_scheme (dict): The annotation scheme configuration horizontal (bool): Whether to display horizontally Returns: tuple: (HTML string, key bindings list) """ return safe_generate_layout( annotation_scheme, _generate_coreference_layout_internal, horizontal )