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: construct single syntactic graph
  
  Parameters
  ----------
  parsed_object :
      A parsed object of single sentence after external parser like ``CoreNLP``
  
  sub_sentence_id : int
      To specify which sentence the ``parsed_object`` is in the ``raw_text_data``
  
  bisequential_link : bool
      Add bi-directional edges between word nodes, do not add if ``False``
  
  top_down : bool
      Edge direction between nodes in constituency tree, if ``True``,
      add edges from top to down
  
  add_pos_node : bool
      Add part-of-speech nodes or not
  
  cut_line_node : bool
      Cut nodes which are organized in "line" format (degree = 2)
docstr_tok: ["construct","single","syntactic","graph","Parameters","--","--","--","--","--","parsed_object",":","A","parsed","object","of","single","sentence","after","external","parser","like","``","CoreNLP","``","sub_sentence_id",":","int","To","specify","which","sentence","the","``","parsed_object","``","is","in","the","``","raw_text_data","``","bisequential_link",":","bool","Add","bi-directional","edges","between","word","nodes",",","do","not","add","if","``","False","``","top_down",":","bool","Edge","direction","between","nodes","in","constituency","tree",",","if","``","True","``",",","add","edges","from","top","to","down","add_pos_node",":","bool","Add","part-of-speech","nodes","or","not","cut_line_node",":","bool","Cut","nodes","which","are","organized","in","``","line","''","format","(","degree","=","2",")"]
code: def _construct_static_graph(
      cls,
      parsed_object,
      sub_sentence_id,
      edge_strategy=None,
      sequential_link=False,
      bisequential_link=True,
      top_down=False,
      add_pos_node=False,
      cut_line_node=True,
  ):
      """construct single syntactic graph
  
      Parameters
      ----------
      parsed_object :
          A parsed object of single sentence after external parser like ``CoreNLP``
  
      sub_sentence_id : int
          To specify which sentence the ``parsed_object`` is in the ``raw_text_data``
  
      bisequential_link : bool
          Add bi-directional edges between word nodes, do not add if ``False``
  
      top_down : bool
          Edge direction between nodes in constituency tree, if ``True``,
          add edges from top to down
  
      add_pos_node : bool
          Add part-of-speech nodes or not
  
      cut_line_node : bool
          Cut nodes which are organized in "line" format (degree = 2)
      """
      assert (add_pos_node & cut_line_node) is False
      parsed_sentence_data = parsed_object["parse"]
      for punc in ["(", ")"]:
          parsed_sentence_data = parsed_sentence_data.replace(
              punc, " " + punc + " "
          )
      parse_list = (parsed_sentence_data.strip()).split()
      # cut root node
      if parse_list[0] == "(" and parse_list[1] == "ROOT":
          parse_list = parse_list[2:-1]
      # transform '.' to 'period'
      for index in range(len(parse_list)):
          if index <= len(parse_list) - 2:
              if (
                  parse_list[index] == "."
                  and parse_list[index + 1] == "."
              ):
                  parse_list[index] = "period"
      res_graph = GraphData()
      pstack = Stack()
      idx = 0
      cnt_word_node = -1
      while idx < len(parse_list):
          if parse_list[idx] == "(":
              res_graph.add_nodes(1)
              # res_graph.node_attributes[res_graph.get_node_num(
              # ) - 1] = {'token': parse_list[idx + 1], 'type': 1,
              # 'position_id': None, 'sentence_id': sub_sentence_id,
              #           'tail': False, 'head': False}
              res_graph.node_attributes[
                  res_graph.get_node_num() - 1
              ] = {
                  "token": parse_list[idx + 1],
                  "type": 1,
                  "position_id": None,
                  "sentence_id": sub_sentence_id,
                  "tail": False,
                  "head": False,
              }
              pstack.push(res_graph.get_node_num() - 1)
              if pstack.size() > 1:
                  node_2 = pstack.pop()
                  node_1 = pstack.pop()
                  if top_down:
                      res_graph.add_edge(node_1, node_2)
                  else:
                      res_graph.add_edge(node_2, node_1)
                  pstack.push(node_1)
                  pstack.push(node_2)
          elif parse_list[idx] == ")":
              pstack.pop()
          elif parse_list[idx + 1] == ")" and parse_list[idx] != ")":
              cnt_word_node += 1
              if add_pos_node:
                  res_graph.add_nodes(1)
              else:
                  assert (
                      res_graph.node_attributes[
                          res_graph.get_node_num() - 1
                      ]["type"]
                      != 0
                  )
              res_graph.node_attributes[
                  res_graph.get_node_num() - 1
              ] = {
                  "token": parse_list[idx],
                  "type": 0,
                  "position_id": cnt_word_node,
                  "sentence_id": sub_sentence_id,
                  "tail": False,
                  "head": False,
              }
              node_1 = pstack.pop()
              if add_pos_node:
                  assert node_1 != res_graph.get_node_num() - 1
                  if top_down:
                      res_graph.add_edge(
                          node_1, res_graph.get_node_num() - 1
                      )
                  else:
                      res_graph.add_edge(
                          res_graph.get_node_num() - 1, node_1
                      )
              else:
                  assert node_1 == res_graph.get_node_num() - 1
              pstack.push(node_1)
          idx += 1
  
      if sequential_link:
          _len_single_graph = res_graph.get_node_num()
          _cnt_node = 0
          while True:
              node_1_idx = -1
              node_2_idx = -1
              for idx_seq, node_attr in enumerate(
                  res_graph.node_attributes
              ):
                  if node_attr["position_id"] == _cnt_node:
                      node_1_idx = idx_seq
                  elif node_attr["position_id"] == _cnt_node + 1:
                      node_2_idx = idx_seq
              if node_1_idx != -1 and node_2_idx != -1:
                  res_graph.add_edge(node_1_idx, node_2_idx)
                  if bisequential_link:
                      res_graph.add_edge(node_2_idx, node_1_idx)
              if _cnt_node >= _len_single_graph:
                  break
              _cnt_node += 1
  
      max_pos = 0
      for n in res_graph.node_attributes:
          if n["type"] == 0 and n["position_id"] > max_pos:
              max_pos = n["position_id"]
  
      min_pos = 99999
      for n in res_graph.node_attributes:
          if n["type"] == 0 and n["position_id"] < min_pos:
              min_pos = n["position_id"]
  
      for n in res_graph.node_attributes:
          if n["type"] == 0 and n["position_id"] == max_pos:
              n["tail"] = True
          if n["type"] == 0 and n["position_id"] == min_pos:
              n["head"] = True
      # print(res_graph.edges)
      if cut_line_node:
          res_graph = cls._cut_line_node(res_graph)
      return res_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 4 for calling the ConstituencyBasedGraphConstruction obj's underlying member method code with example usage: obj._construct_static_graph(cls, parsed_object, sub_sentence_id, edge_strategy, sequential_link, bisequential_link, top_down, add_pos_node, cut_line_node) and returns: res_graph