<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Task
Step1:  There are only very few RST spans that match to non-S/CS nodes
Step3:  Are there any 'S'/'CS' that are not sentence root nodes?
subordinate clauses etc.
<ASSISTANT_TASK:>
Python Code:
import os
import discoursegraphs as dg
ddg = dg.corpora.pcc.get_document('maz-6728')
dg.DATA_ROOT_DIR
# dg.corpora.pcc.get_files_by_layer('syntax')
tdg = dg.read_tiger(os.path.join(
        dg.DATA_ROOT_DIR,
        'potsdam-commentary-corpus-2.0.0/syntax/maz-11766.xml'))
# dg.info(tdg)
# %load_ext gvmagic
# %dotstr dg.print_dot(tdg)
syntax_nodes = set(dg.select_nodes_by_layer(ddg, 'tiger:syntax'))
cat_nodes = set(dg.select_nodes_by_attribute(ddg, 'tiger:cat'))
# token_map = dg.util.TokenMapper(ddg)
# token_map.id2index['rst:rst:9_0']
token_map = dg.util.TokenMapper(ddg)
cat_spans = {}
for cat_node in cat_nodes:
#     print ddg.node[cat_node]['tiger:cat'], dg.get_span_offsets(ddg, cat_node)
    first_token_index = token_map.id2index[dg.get_span(ddg, cat_node)[0]]
    last_token_index = token_map.id2index[dg.get_span(ddg, cat_node)[-1]]
    
#     print ddg.node[cat_node]['tiger:cat'], token_map.id2index[first_token_id], token_map.id2index[last_token_id]
    cat_spans[(first_token_index, last_token_index)] = {
        'node': cat_node, 'cat': ddg.node[cat_node]['tiger:cat']}
def get_syntax_spans(docgraph):
    token_map = dg.util.TokenMapper(docgraph)
    cat_nodes = set(dg.select_nodes_by_attribute(docgraph, 'tiger:cat'))
    syntax_spans = {}
    for cat_node in cat_nodes:
        first_token_index = token_map.id2index[dg.get_span(docgraph, cat_node)[0]]
        last_token_index = token_map.id2index[dg.get_span(docgraph, cat_node)[-1]]
        syntax_spans[(first_token_index, last_token_index)] = {
            'node': cat_node, 'cat': docgraph.node[cat_node]['tiger:cat']}
    return syntax_spans
# cat_spans
def get_rst_span_map(docgraph):
    rst_spans = {}
    for rel_id, span_type, rel_type, tok_onset, tok_offset in dg.readwrite.rst.rs3.get_rst_spans(docgraph):
        rst_spans[(tok_onset, tok_offset)] = {
            'rel_id': rel_id, 'span_type': span_type, 'rel_type': rel_type}
    
    return rst_spans
Explanation: Task: match syntax spans with RST spans
Subtask 1: get all syntax spans (with string/token offsets)
End of explanation
for doc_id in dg.corpora.pcc.document_ids:
    ddg = dg.corpora.pcc.get_document(doc_id)
    syntax_spans = get_syntax_spans(ddg)
    
    for syntax_span in syntax_spans:
        if syntax_spans[syntax_span]['cat'] not in ('S', 'CS'):
            if syntax_span in rst_spans:
                print doc_id, syntax_span, syntax_spans[syntax_span]['cat']
from collections import Counter
cat_counter = Counter()
subord_counter = Counter()
for doc_id in dg.corpora.pcc.document_ids:
    ddg = dg.corpora.pcc.get_document(doc_id)
    syntax_spans = get_syntax_spans(ddg)
    
    subord_nodes = get_subordinate_clauses(ddg)
    
    for syntax_span in syntax_spans:
        if syntax_span in rst_spans:
#             print doc_id, syntax_span, syntax_spans[syntax_span]['cat']
            cat = syntax_spans[syntax_span]['cat']
            cat_counter[cat] += 1
        
            if syntax_spans[syntax_span]['node'] in subord_nodes:
                subord_counter[cat] += 1
cat_counter.most_common()
subord_counter
Explanation: There are only very few RST spans that match to non-S/CS nodes
End of explanation
def get_subordinate_clauses(tiger_docgraph):
    
    given a document graph of a TIGER syntax tree, return all
    node IDs of nodes representing subordinate clause constituents.
    Parameters
    ----------
    tiger_docgraph : dg.DiscourseDocumentGraph
        document graph from which subordinate clauses will be extracted
    
    Returns
    -------
    subord_clause_nodes : list(str)
        list of node IDs of nodes directly dominating subordinate clauses
    
    subord_clause_rels = \
        dg.select_edges_by_attribute(
            tiger_docgraph, attribute='tiger:label',
            value=['MO', 'RC', 'SB'])
    
    subord_clause_nodes = []
    for src_id, target_id in subord_clause_rels:
        src_cat = tiger_docgraph.node[src_id].get('tiger:cat')
        if src_cat == 'S' and not dg.istoken(tiger_docgraph, target_id):
            subord_clause_nodes.append(target_id)
    return subord_clause_nodes
for node_id in get_subordinate_clauses(ddg):
    print node_id, ddg.node[node_id]
# rst_spans
for cat_span in cat_spans:
    if cat_span in rst_spans:
        print cat_span, cat_spans[cat_span][1], rst_spans[cat_span][1:]
Explanation: Are there any 'S'/'CS' that are not sentence root nodes?
subordinate clauses etc.
End of explanation
<END_TASK>