(
    data: Sequence[str] | Sequence[Sequence[str]],
    edge_weighting: str,
)
return graph
Transform a sequence of strings (or a sequence of such sequences) into a graph,
    where each element of the top-level sequence is represented by a node
    with edges linking it to all other elements weighted by their pairwise similarity.

    Input ``data`` can take a variety of forms. For example, as a ``Sequence[str]``
    where elements are sentence texts from a single document:

    .. code-block:: pycon

        >>> texts = [
        ...     "Mary had a little lamb. Its fleece was white as snow.",
        ...     "Everywhere that Mary went the lamb was sure to go.",
        ... ]
        >>> docs = [make_spacy_doc(text, lang="en_core_web_sm") for text in texts]
        >>> data = [sent.text.lower() for sent in docs[0].sents]
        >>> graph = build_similarity_network(data, "levenshtein")
        >>> sorted(graph.adjacency())[0]
        ('its fleece was white as snow.',
         {'mary had a little lamb.': {'weight': 0.24137931034482762}})

    Or as a ``Sequence[str]`` where elements are full texts from multiple documents:

    .. code-block:: pycon

        >>> data = [doc.text.lower() for doc in docs]
        >>> graph = build_similarity_network(data, "jaro")
        >>> sorted(graph.adjacency())[0]
        ('everywhere that mary went the lamb was sure to go.',
         {'mary had a little lamb. its fleece was white as snow.': {'weight': 0.6516002795248078}})

    Or as a ``Sequence[Sequence[str]]`` where elements are tokenized texts from
    multiple documents:

    .. code-block:: pycon

        >>> data = [[tok.lower_ for tok in doc] for doc in docs]
        >>> graph = build_similarity_network(data, "jaccard")
        >>> sorted(graph.adjacency())[0]
        (('everywhere', 'that', 'mary', 'went', 'the', 'lamb', 'was', 'sure', 'to', 'go', '.'),
         {('mary', 'had', 'a', 'little', 'lamb', '.', 'its', 'fleece', 'was', 'white', 'as', 'snow', '.'): {'weight': 0.21052631578947367}})

    Args:
        data
        edge_weighting: Similarity metric to use for weighting edges between elements
            in ``data``, represented as the name of a function available in
            :mod:`textacy.similarity`.

            .. note:: Different metrics are suited for different forms and contexts
               of ``data``. You'll have to decide which method makes sense. For example,
               when comparing a sequence of short strings, "levenshtein" is often
               a reasonable bet; when comparing a sequence of sequences of somewhat
               noisy strings (e.g. includes punctuation, cruft tokens), you might try
               "matching_subsequences_ratio" to help filter out the noise.

    Returns:
        Graph whose nodes correspond to top-level sequence elements in ``data``,
        connected by edges to all other nodes with weights determined by
        their pairwise similarity.

    Reference:
        https://en.wikipedia.org/wiki/Semantic_similarity_network -- this is *not*
        the same as what's implemented here, but they're similar in spirit.
Transform a sequence of strings (or a sequence of such sequences) into a graph,
    where each element of the top-level sequence is represented by a node
    with edges linking it to all other elements weighted by their pairwise similarity.
Transform a sequence of strings ( or a sequence of such sequences ) into a graph where each element of the top - level sequence is represented by a node with edges linking it to all other elements weighted by their pairwise similarity .
def build_similarity_network(
    data: Sequence[str] | Sequence[Sequence[str]],
    edge_weighting: str,
) -> nx.Graph:
    """
    Transform a sequence of strings (or a sequence of such sequences) into a graph,
    where each element of the top-level sequence is represented by a node
    with edges linking it to all other elements weighted by their pairwise similarity.

    Input ``data`` can take a variety of forms. For example, as a ``Sequence[str]``
    where elements are sentence texts from a single document:

    .. code-block:: pycon

        >>> texts = [
        ...     "Mary had a little lamb. Its fleece was white as snow.",
        ...     "Everywhere that Mary went the lamb was sure to go.",
        ... ]
        >>> docs = [make_spacy_doc(text, lang="en_core_web_sm") for text in texts]
        >>> data = [sent.text.lower() for sent in docs[0].sents]
        >>> graph = build_similarity_network(data, "levenshtein")
        >>> sorted(graph.adjacency())[0]
        ('its fleece was white as snow.',
         {'mary had a little lamb.': {'weight': 0.24137931034482762}})

    Or as a ``Sequence[str]`` where elements are full texts from multiple documents:

    .. code-block:: pycon

        >>> data = [doc.text.lower() for doc in docs]
        >>> graph = build_similarity_network(data, "jaro")
        >>> sorted(graph.adjacency())[0]
        ('everywhere that mary went the lamb was sure to go.',
         {'mary had a little lamb. its fleece was white as snow.': {'weight': 0.6516002795248078}})

    Or as a ``Sequence[Sequence[str]]`` where elements are tokenized texts from
    multiple documents:

    .. code-block:: pycon

        >>> data = [[tok.lower_ for tok in doc] for doc in docs]
        >>> graph = build_similarity_network(data, "jaccard")
        >>> sorted(graph.adjacency())[0]
        (('everywhere', 'that', 'mary', 'went', 'the', 'lamb', 'was', 'sure', 'to', 'go', '.'),
         {('mary', 'had', 'a', 'little', 'lamb', '.', 'its', 'fleece', 'was', 'white', 'as', 'snow', '.'): {'weight': 0.21052631578947367}})

    Args:
        data
        edge_weighting: Similarity metric to use for weighting edges between elements
            in ``data``, represented as the name of a function available in
            :mod:`textacy.similarity`.

            .. note:: Different metrics are suited for different forms and contexts
               of ``data``. You'll have to decide which method makes sense. For example,
               when comparing a sequence of short strings, "levenshtein" is often
               a reasonable bet; when comparing a sequence of sequences of somewhat
               noisy strings (e.g. includes punctuation, cruft tokens), you might try
               "matching_subsequences_ratio" to help filter out the noise.

    Returns:
        Graph whose nodes correspond to top-level sequence elements in ``data``,
        connected by edges to all other nodes with weights determined by
        their pairwise similarity.

    Reference:
        https://en.wikipedia.org/wiki/Semantic_similarity_network -- this is *not*
        the same as what's implemented here, but they're similar in spirit.
    """
    if not data:
        LOGGER.warning("input `data` is empty, so output graph is also empty")
        return nx.Graph()

    sim_func = getattr(similarity, edge_weighting)

    if isinstance(data[0], str):
        ele_pairs = itertools.combinations(data, 2)
    elif isinstance(data[0], Sequence) and isinstance(data[0][0], str):
        # nx graph nodes need to be *hashable*, so Sequence => Tuple
        ele_pairs = (
            (tuple(ele1), tuple(ele2)) for ele1, ele2 in itertools.combinations(data, 2)
        )
    else:
        raise TypeError(
            errors.type_invalid_msg(
                "data", data, Union[Sequence[str], Sequence[Sequence[str]]]
            )
        )

    graph = nx.Graph()
    graph.add_edges_from(
        (ele1, ele2, {"weight": sim_func(ele1, ele2)}) for ele1, ele2 in ele_pairs
    )
    return graph
def build_similarity_network ( data : Sequence [ str ] | Sequence [ Sequence [ str ] ] , edge_weighting : str , ) -> nx . Graph : if not data : LOGGER . warning ( "input `data` is empty, so output graph is also empty" ) return nx . Graph ( ) sim_func = getattr ( similarity , edge_weighting ) if isinstance ( data [ 0 ] , str ) : ele_pairs = itertools . combinations ( data , 2 ) elif isinstance ( data [ 0 ] , Sequence ) and isinstance ( data [ 0 ] [ 0 ] , str ) : # nx graph nodes need to be *hashable*, so Sequence => Tuple ele_pairs = ( ( tuple ( ele1 ) , tuple ( ele2 ) ) for ele1 , ele2 in itertools . combinations ( data , 2 ) ) else : raise TypeError ( errors . type_invalid_msg ( "data" , data , Union [ Sequence [ str ] , Sequence [ Sequence [ str ] ] ] ) ) graph = nx . Graph ( ) graph . add_edges_from ( ( ele1 , ele2 , { "weight" : sim_func ( ele1 , ele2 ) } ) for ele1 , ele2 in ele_pairs ) return graph