code: def generalized_connected_component_analysis_1d(
      sequence: List[Any],
      scoring_func: Callable[[Any, Any], int],
      aggregation_func: Callable[[List[Any]], Any] = None,
      default_score_value: int = 0,
  ) -> List[Any]:
      """Perform connected componenet analysis for any 1D sequence based on
      the scoring function and the aggregation function.
      It will generate the adjacency_matrix for the 1D sequence object using
      the provided `scoring_func` and find the connected componenets.
      The `aggregation_func` will be used to aggregate all elements within
      identified components (when not set, it will be the identity function).
  
      Args:
          sequence (List[Any]):
              The provided 1D sequence of objects.
          scoring_func (Callable[[Any, Any], int]):
              The scoring function used to construct the adjacency_matrix.
              It should take two objects in the sequence and produe a integer.
          aggregation_func (Callable[[List[Any]], Any], optional):
              The function used to aggregate the elements within an identified
              component.
              Defaults to the identify function: `lambda x: x`.
          default_score_value (int, optional):
              Used to set the default (background) score values that should be
              not considered when running connected component analysis.
              Defaults to 0.
  
      Returns:
          List[Any]: A list of length n - the number of the detected componenets.
      """
  
      if aggregation_func is None:
          aggregation_func = lambda x: x  # Identity Function
  
      seq_len = len(sequence)
      adjacency_matrix = (
          np.ones((seq_len, seq_len)) * default_score_value
      )
  
      for i in range(seq_len):
          for j in range(i + 1, seq_len):
              adjacency_matrix[i][j] = scoring_func(
                  sequence[i], sequence[j]
              )
  
      graph = csr_matrix(adjacency_matrix)
      n_components, labels = connected_components(
          csgraph=graph, directed=False, return_labels=True
      )
  
      grouped_sequence = []
      for comp_idx in range(n_components):
          element_idx = np.where(labels == comp_idx)[0]
          grouped_sequence.append(
              aggregation_func([sequence[i] for i in element_idx])
          )
  
      return grouped_sequence
  
file_path: layout-parser/src/layoutparser/tools/shape_operations.py
imports: from typing import List, Union, Any, Callable, Iterable
  from functools import partial, reduce
  import numpy
  from scipy.sparse import csr_matrix
  from scipy.sparse.csgraph import connected_components
  from ..elements import BaseLayoutElement, TextBlock
label_desc: Use image node_id 1 for calling a global function with example usage: generalized_connected_component_analysis_1d(sequence, scoring_func, aggregation_func, default_score_value) and returns: grouped_sequence