class_docstr: Class for constituency graph construction.
  
  ...
  
  Attributes
  ----------
  embedding_styles : (dict)
      Specify embedding styles including ``single_token_item``, ``emb_strategy``,
      ``num_rnn_layers``, ``bert_model_name`` and ``bert_lower_case``.
  
  vocab: (set, optional)
      Vocabulary including all words appeared in graphs.
  
  Methods
  -------
  
  topology(raw_text_data, nlp_processor, merge_strategy=None, edge_strategy=None)
      Generate graph structure with nlp parser like ``CoreNLP`` etc.
  
  _construct_static_graph(parsed_object, sub_sentence_id, edge_strategy=None)
      Construct a single static graph from a single sentence,
      to be called by ``topology`` function.
  
  _graph_connect(nx_graph_list, merge_strategy=None)
      Construct a merged graph from a list of graphs, to be called by ``topology`` function.
  
  embedding(node_attributes, edge_attributes)
      Generate node/edge embeddings from node/edge attributes through an embedding layer.
  
  forward(raw_text_data, nlp_parser)
      Generate graph topology and embeddings.
class_docstr_tok: ["Class","for","constituency","graph","construction",".","...","Attributes","--","--","--","--","--","embedding_styles",":","(","dict",")","Specify","embedding","styles","including","``","single_token_item","``",",","``","emb_strategy","``",",","``","num_rnn_layers","``",",","``","bert_model_name","``","and","``","bert_lower_case","``",".","vocab",":","(","set",",","optional",")","Vocabulary","including","all","words","appeared","in","graphs",".","Methods","--","--","--","-","topology","(","raw_text_data",",","nlp_processor",",","merge_strategy=None",",","edge_strategy=None",")","Generate","graph","structure","with","nlp","parser","like","``","CoreNLP","``","etc",".","_construct_static_graph","(","parsed_object",",","sub_sentence_id",",","edge_strategy=None",")","Construct","a","single","static","graph","from","a","single","sentence",",","to","be","called","by","``","topology","``","function",".","_graph_connect","(","nx_graph_list",",","merge_strategy=None",")","Construct","a","merged","graph","from","a","list","of","graphs",",","to","be","called","by","``","topology","``","function",".","embedding","(","node_attributes",",","edge_attributes",")","Generate","node\/edge","embeddings","from","node\/edge","attributes","through","an","embedding","layer",".","forward","(","raw_text_data",",","nlp_parser",")","Generate","graph","topology","and","embeddings","."]
docstr: topology This function generate a graph strcuture from a raw text data.
  
  Parameters
  ----------
  raw_text_data : string
      A string to be used to construct a static graph, can be composed of multiple strings
  
  nlp_processor : object
      A parser used to parse sentence string to parsing trees like dependency parsing tree
      or constituency parsing tree
  
  merge_strategy : None or str, option=[None, "tailhead", "user_define"]
      Strategy to merge sub-graphs into one graph
      ``None``: It will be the default option. We will do as ``"tailhead"``.
      ``"tailhead"``: Link the sub-graph  ``i``'s tail node with ``i+1``'s head node
      ``"user_define"``: We will give this option to the user. User can override the
                          method ``_graph_connnect`` to define your merge strategy.
  
  edge_strategy: None or str, option=[None, "homogeneous", "heterogeneous", "as_node"]
      Strategy to process edge.
      ``None``: It will be the default option. We will do as ``"homogeneous"``.
      ``"homogeneous"``: We will drop the edge type information.
                         If there is a linkage among node ``i`` and node ``j``, we will
                         add an edge whose weight is ``1.0``. Otherwise there is no edge.
      ``heterogeneous``: We will keep the edge type information.
                         An edge will have type information like ``n_subj``.
                         It is not implemented yet.
      ``as_node``: We will view the edge as a graph node.
                   If there is an edge whose type is ``k`` between node ``i`` and node ``j``,
                   we will insert a node ``k`` into the graph and link
                   node (``i``, ``k``) and (``k``, ``j``). It is not implemented yet.
  
  sequential_link : int, option=[0,1,2,3]
      Strategy to add sequential links between word nodes.
      ``0``: Do not add sequential links.
      ``1``: Add unidirectional links.
      ``2``: Add bidirectional links.
      ``3``: Do not add sequential links inside each sentence and add bidirectional links
             between adjacent sentences.
  
  top_down : bool
      If true, edges in constituency tree are from root nodes to leaf nodes. Otherwise,
      from leaf nodes to root nodes.
  
  prune : int, option=[0,1,2]
      Strategies for pruning constituency trees
      ``0``: No pruning.
      ``1``: Prune pos nodes.
      ``2``: Prune nodes with both in-degree and out-degree of 1.
  
  verbose : bool
      A boolean option to decide whether to print out the graph construction process.
  
  Returns
  -------
  GraphData
      A customized graph data structure
docstr_tok: ["topology","This","function","generate","a","graph","strcuture","from","a","raw","text","data",".","Parameters","--","--","--","--","--","raw_text_data",":","string","A","string","to","be","used","to","construct","a","static","graph",",","can","be","composed","of","multiple","strings","nlp_processor",":","object","A","parser","used","to","parse","sentence","string","to","parsing","trees","like","dependency","parsing","tree","or","constituency","parsing","tree","merge_strategy",":","None","or","str",",","option=","[","None",",","``","tailhead","''",",","``","user_define","''","]","Strategy","to","merge","sub-graphs","into","one","graph","``","None","``",":","It","will","be","the","default","option",".","We","will","do","as","``","``","tailhead","''","``",".","``","``","tailhead","''","``",":","Link","the","sub-graph","``","i","``","'s","tail","node","with","``","i+1","``","'s","head","node","``","``","user_define","''","``",":","We","will","give","this","option","to","the","user",".","User","can","override","the","method","``","_graph_connnect","``","to","define","your","merge","strategy",".","edge_strategy",":","None","or","str",",","option=","[","None",",","``","homogeneous","''",",","``","heterogeneous","''",",","``","as_node","''","]","Strategy","to","process","edge",".","``","None","``",":","It","will","be","the","default","option",".","We","will","do","as","``","``","homogeneous","''","``",".","``","``","homogeneous","''","``",":","We","will","drop","the","edge","type","information",".","If","there","is","a","linkage","among","node","``","i","``","and","node","``","j","``",",","we","will","add","an","edge","whose","weight","is","``","1.0","``",".","Otherwise","there","is","no","edge",".","``","heterogeneous","``",":","We","will","keep","the","edge","type","information",".","An","edge","will","have","type","information","like","``","n_subj","``",".","It","is","not","implemented","yet",".","``","as_node","``",":","We","will","view","the","edge","as","a","graph","node",".","If","there","is","an","edge","whose","type","is","``","k","``","between","node","``","i","``","and","node","``","j","``",",","we","will","insert","a","node","``","k","``","into","the","graph","and","link","node","(","``","i","``",",","``","k","``",")","and","(","``","k","``",",","``","j","``",")",".","It","is","not","implemented","yet",".","sequential_link",":","int",",","option=","[","0,1,2,3","]","Strategy","to","add","sequential","links","between","word","nodes",".","``","0","``",":","Do","not","add","sequential","links",".","``","1","``",":","Add","unidirectional","links",".","``","2","``",":","Add","bidirectional","links",".","``","3","``",":","Do","not","add","sequential","links","inside","each","sentence","and","add","bidirectional","links","between","adjacent","sentences",".","top_down",":","bool","If","true",",","edges","in","constituency","tree","are","from","root","nodes","to","leaf","nodes",".","Otherwise",",","from","leaf","nodes","to","root","nodes",".","prune",":","int",",","option=","[","0,1,2","]","Strategies","for","pruning","constituency","trees","``","0","``",":","No","pruning",".","``","1","``",":","Prune","pos","nodes",".","``","2","``",":","Prune","nodes","with","both","in-degree","and","out-degree","of","1.","verbose",":","bool","A","boolean","option","to","decide","whether","to","print","out","the","graph","construction","process",".","Returns","--","--","--","-","GraphData","A","customized","graph","data","structure"]
code: def static_topology(
      cls,
      raw_text_data,
      nlp_processor,
      processor_args,
      merge_strategy=None,
      edge_strategy=None,
      sequential_link=3,
      top_down=False,
      prune=2,
      verbose=True,
  ):
      """topology This function generate a graph strcuture from a raw text data.
  
      Parameters
      ----------
      raw_text_data : string
          A string to be used to construct a static graph, can be composed of multiple strings
  
      nlp_processor : object
          A parser used to parse sentence string to parsing trees like dependency parsing tree
          or constituency parsing tree
  
      merge_strategy : None or str, option=[None, "tailhead", "user_define"]
          Strategy to merge sub-graphs into one graph
          ``None``: It will be the default option. We will do as ``"tailhead"``.
          ``"tailhead"``: Link the sub-graph  ``i``'s tail node with ``i+1``'s head node
          ``"user_define"``: We will give this option to the user. User can override the
                              method ``_graph_connnect`` to define your merge strategy.
  
      edge_strategy: None or str, option=[None, "homogeneous", "heterogeneous", "as_node"]
          Strategy to process edge.
          ``None``: It will be the default option. We will do as ``"homogeneous"``.
          ``"homogeneous"``: We will drop the edge type information.
                             If there is a linkage among node ``i`` and node ``j``, we will
                             add an edge whose weight is ``1.0``. Otherwise there is no edge.
          ``heterogeneous``: We will keep the edge type information.
                             An edge will have type information like ``n_subj``.
                             It is not implemented yet.
          ``as_node``: We will view the edge as a graph node.
                       If there is an edge whose type is ``k`` between node ``i`` and node ``j``,
                       we will insert a node ``k`` into the graph and link
                       node (``i``, ``k``) and (``k``, ``j``). It is not implemented yet.
  
      sequential_link : int, option=[0,1,2,3]
          Strategy to add sequential links between word nodes.
          ``0``: Do not add sequential links.
          ``1``: Add unidirectional links.
          ``2``: Add bidirectional links.
          ``3``: Do not add sequential links inside each sentence and add bidirectional links
                 between adjacent sentences.
  
      top_down : bool
          If true, edges in constituency tree are from root nodes to leaf nodes. Otherwise,
          from leaf nodes to root nodes.
  
      prune : int, option=[0,1,2]
          Strategies for pruning constituency trees
          ``0``: No pruning.
          ``1``: Prune pos nodes.
          ``2``: Prune nodes with both in-degree and out-degree of 1.
  
      verbose : bool
          A boolean option to decide whether to print out the graph construction process.
  
      Returns
      -------
      GraphData
          A customized graph data structure
      """
      output_graph_list = []
      parsed_output = cls.parsing(
          raw_text_data, nlp_processor, processor_args
      )
      if prune == 0:
          add_pos_nodes = True
          cut_line_node = False
      elif prune == 1:
          add_pos_nodes = False
          cut_line_node = False
      elif prune == 2:
          add_pos_nodes = False
          cut_line_node = True
      else:
          raise ValueError("``prune`` should be chosen from [0,1,2].")
  
      if sequential_link == 0:
          seq_link = False
          biseq_link = False
      elif sequential_link == 1:
          seq_link = True
          biseq_link = False
      elif sequential_link == 2:
          seq_link = True
          biseq_link = True
      elif sequential_link == 3:
          seq_link = False
          biseq_link = True
      else:
          raise ValueError(
              "``sequential_link`` should be chosen from [0,1,2,3]."
          )
  
      if cut_line_node and sequential_link == 1:
          raise ValueError(
              "``cut_line_node`` should not be used when edges\
                   between word nodes are unidirectional."
          )
  
      for index in range(len(parsed_output)):
          # print(parsed_output[index]['parse'])
          output_graph_list.append(
              cls._construct_static_graph(
                  parsed_output[index],
                  index,
                  sequential_link=seq_link,
                  bisequential_link=biseq_link,
                  top_down=top_down,
                  add_pos_node=add_pos_nodes,
                  cut_line_node=cut_line_node,
              )
          )
      ret_graph = cls._graph_connect(
          output_graph_list, bisequential_link=biseq_link
      )
      # for edge_item in ret_graph.get_all_edges():
      #     print(ret_graph.node_attributes[edge_item[0]]['token'], "->",
      #       ret_graph.node_attributes[edge_item[1]]['token'])
      if verbose:
          print("--------------------------------------")
          for _edge in ret_graph.get_all_edges():
              print(
                  ret_graph.node_attributes[_edge[0]]["token"],
                  "\t---\t",
                  ret_graph.node_attributes[_edge[1]]["token"],
              )
          print("--------------------------------------")
      return ret_graph
  
file_path: graph4nlp/graph4nlp/pytorch/modules/graph_construction/constituency_graph_construction.py
imports: import copy
  import json
  from pythonds.basic.stack import Stack
  from graph4nlp.pytorch.data.data import GraphData
  from .base import StaticGraphConstructionBase
  from .utils import CORENLP_TIMEOUT_SIGNATURE
label_desc: Use image node_id 3 for calling the ConstituencyBasedGraphConstruction obj's underlying member method code with example usage: obj.static_topology(cls, raw_text_data, nlp_processor, processor_args, merge_strategy, edge_strategy, sequential_link, top_down, prune, verbose) and returns: ret_graph