text_extensions_for_pandas/io/watson/tables.py
def parse_response(response: Dict[str, Any], select_table=None) -> Dict[str, pd.DataFrame]:
    """
    Parse a response from Watson Table Understanding as a decoded JSON string. e.g.
    dictionary containing requested features and convert into a dict of Pandas DataFrames.

    The following features will be converted from the response:

        * Row headers
        * Column headers
        * Body cells

    More information on using Watson Table Extraction or the Compare and Comply API, see
    https://cloud.ibm.com/docs/compare-comply?topic=compare-comply-understanding_tables
    More information about available features can be found at
    https://cloud.ibm.com/apidocs/compare-comply?code=python#extract-a-document-s-tables


    :param response: A dictionary of features returned by the IBM Watson Compare and
        Comply web service API or a comparable Watson Discovery API
    :param table_number: Defaults to analyzing the first table, input a number here to
        analyze the nth table

    :return: A dictionary mapping feature names ("row_headers", "col_headers", "body_cells")
        to Pandas DataFrames
    """
    return_dict = {}
    tables = response.get("tables", [])

    # if a tuple is used, of the form (feild, n) the nth table that meets the criterion is chosen
    if type(select_table) == tuple:
        select_no = select_table[1]
        select_table = select_table[0]
    else:  # otherwise take the first table that meets the criterion
        select_no = 0

    table_number = None
    if type(select_table) == int:
        table_number = select_table
    elif type(select_table) == str:
        select_no_temp = select_no
        for i, table in enumerate(tables):
            if table["title"] != {} and select_table.lower() in table["title"]["text"].lower():
                table_number = i
        if table_number is None:  # if that didn't work, look at section title
            select_no_temp = select_no
            for i, table in enumerate(tables):
                if table["section_title"] != {} and select_table.lower() in table["section_title"]["text"].lower():
                    table_number = i
                    if select_no_temp == 0:
                        break
                    else:
                        select_no_temp = select_no_temp - 1

    if table_number is None:
        table_number = 0
    table = tables[table_number]
    plain_text = table.get("text")
    table_title = table.get("text")

    # Create Row headers DataFrame
    row_headers = table.get("row_headers", [])
    if len(row_headers) != 0:
        row_headers_df = _make_headers_df(row_headers)
        return_dict["row_headers"] = row_headers_df
    else:
        return_dict["row_headers"] = None

    # Create Column headers DataFrame
    column_headers = table.get("column_headers", [])
    if len(column_headers) != 0:
        column_headers_df = _make_headers_df(column_headers)
        return_dict["col_headers"] = column_headers_df
    else:
        return_dict["col_headers"] = None

    # Create body cells dataframe
    body_cells = table.get("body_cells", [])
    body_cells_df = _make_body_cells_df(body_cells)
    body_cells_df["column_header_texts"] = body_cells_df["column_header_texts"].apply(_strip_list)
    body_cells_df["row_header_texts"] = body_cells_df["row_header_texts"].apply(_strip_list)
    return_dict["body_cells"] = body_cells_df
    return_dict["given_loc"] = table["location"]

    return return_dict

    Parse a response from Watson Table Understanding as a decoded JSON string. e.g.
    dictionary containing requested features and convert into a dict of Pandas DataFrames.

    The following features will be converted from the response:

        * Row headers
        * Column headers
        * Body cells

    More information on using Watson Table Extraction or the Compare and Comply API, see
    https://cloud.ibm.com/docs/compare-comply?topic=compare-comply-understanding_tables
    More information about available features can be found at
    https://cloud.ibm.com/apidocs/compare-comply?code=python#extract-a-document-s-tables


    :param response: A dictionary of features returned by the IBM Watson Compare and
        Comply web service API or a comparable Watson Discovery API
    :param table_number: Defaults to analyzing the first table, input a number here to
        analyze the nth table

    :return: A dictionary mapping feature names ("row_headers", "col_headers", "body_cells")
        to Pandas DataFrames
    
Parse a response from Watson Table Understanding as a decoded JSON string.

The following features will be converted from the response.

Row headers
Column headers
Body cells


Parse a response from Watson Table Understanding as a decoded JSON string . The following features will be converted from the response . Row headers Column headers Body cells
def parse_response(response: Dict[str, Any], select_table=None) -> Dict[str, pd.DataFrame]:
    return_dict = {}
    tables = response.get("tables", [])
    if type(select_table) == tuple:
        select_no = select_table[1]
        select_table = select_table[0]
    else:  
        select_no = 0
    table_number = None
    if type(select_table) == int:
        table_number = select_table
    elif type(select_table) == str:
        select_no_temp = select_no
        for i, table in enumerate(tables):
            if table["title"] != {} and select_table.lower() in table["title"]["text"].lower():
                table_number = i
        if table_number is None:  
            select_no_temp = select_no
            for i, table in enumerate(tables):
                if table["section_title"] != {} and select_table.lower() in table["section_title"]["text"].lower():
                    table_number = i
                    if select_no_temp == 0:
                        break
                    else:
                        select_no_temp = select_no_temp - 1
    if table_number is None:
        table_number = 0
    table = tables[table_number]
    plain_text = table.get("text")
    table_title = table.get("text")
    row_headers = table.get("row_headers", [])
    if len(row_headers) != 0:
        row_headers_df = _make_headers_df(row_headers)
        return_dict["row_headers"] = row_headers_df
    else:
        return_dict["row_headers"] = None
    column_headers = table.get("column_headers", [])
    if len(column_headers) != 0:
        column_headers_df = _make_headers_df(column_headers)
        return_dict["col_headers"] = column_headers_df
    else:
        return_dict["col_headers"] = None
    body_cells = table.get("body_cells", [])
    body_cells_df = _make_body_cells_df(body_cells)
    body_cells_df["column_header_texts"] = body_cells_df["column_header_texts"].apply(_strip_list)
    body_cells_df["row_header_texts"] = body_cells_df["row_header_texts"].apply(_strip_list)
    return_dict["body_cells"] = body_cells_df
    return_dict["given_loc"] = table["location"]
    return return_dict
def parse_response ( response : Dict [ str , Any ] , select_table = None ) -> Dict [ str , pd . DataFrame ] : return_dict = { } tables = response . get ( "tables" , [ ] ) if type ( select_table ) == tuple : select_no = select_table [ 1 ] select_table = select_table [ 0 ] else : select_no = 0 table_number = None if type ( select_table ) == int : table_number = select_table elif type ( select_table ) == str : select_no_temp = select_no for i , table in enumerate ( tables ) : if table [ "title" ] != { } and select_table . lower ( ) in table [ "title" ] [ "text" ] . lower ( ) : table_number = i if table_number is None : select_no_temp = select_no for i , table in enumerate ( tables ) : if table [ "section_title" ] != { } and select_table . lower ( ) in table [ "section_title" ] [ "text" ] . lower ( ) : table_number = i if select_no_temp == 0 : break else : select_no_temp = select_no_temp - 1 if table_number is None : table_number = 0 table = tables [ table_number ] plain_text = table . get ( "text" ) table_title = table . get ( "text" ) row_headers = table . get ( "row_headers" , [ ] ) if len ( row_headers ) != 0 : row_headers_df = _make_headers_df ( row_headers ) return_dict [ "row_headers" ] = row_headers_df else : return_dict [ "row_headers" ] = None column_headers = table . get ( "column_headers" , [ ] ) if len ( column_headers ) != 0 : column_headers_df = _make_headers_df ( column_headers ) return_dict [ "col_headers" ] = column_headers_df else : return_dict [ "col_headers" ] = None body_cells = table . get ( "body_cells" , [ ] ) body_cells_df = _make_body_cells_df ( body_cells ) body_cells_df [ "column_header_texts" ] = body_cells_df [ "column_header_texts" ] . apply ( _strip_list ) body_cells_df [ "row_header_texts" ] = body_cells_df [ "row_header_texts" ] . apply ( _strip_list ) return_dict [ "body_cells" ] = body_cells_df return_dict [ "given_loc" ] = table [ "location" ] return return_dict
Parse a response from Watson Table Understanding as a decoded JSON string.
Parse a response from Watson Table Understanding as a decoded JSON string .
"""
    Parse a response from Watson Table Understanding as a decoded JSON string. e.g.
    dictionary containing requested features and convert into a dict of Pandas DataFrames.

    The following features will be converted from the response:

        * Row headers
        * Column headers
        * Body cells

    More information on using Watson Table Extraction or the Compare and Comply API, see
    https://cloud.ibm.com/docs/compare-comply?topic=compare-comply-understanding_tables
    More information about available features can be found at
    https://cloud.ibm.com/apidocs/compare-comply?code=python#extract-a-document-s-tables


    :param response: A dictionary of features returned by the IBM Watson Compare and
        Comply web service API or a comparable Watson Discovery API
    :param table_number: Defaults to analyzing the first table, input a number here to
        analyze the nth table

    :return: A dictionary mapping feature names ("row_headers", "col_headers", "body_cells")
        to Pandas DataFrames
    """ # if a tuple is used, of the form (feild, n) the nth table that meets the criterion is chosen # otherwise take the first table that meets the criterion # if that didn't work, look at section title # Create Row headers DataFrame # Create Column headers DataFrame # Create body cells dataframe
A dictionary mapping feature names ("row_headers", "col_headers", "body_cells")
to Pandas DataFrames
A dictionary mapping feature names ( " row_headers " " col_headers " " body_cells " ) to Pandas DataFrames
response
Dict[str, Any]
A dictionary of features returned by the IBM Watson Compare and
Comply web service API or a comparable Watson Discovery API
A dictionary of features returned by the IBM Watson Compare and Comply web service API or a comparable Watson Discovery API
select_table
table_number
Defaults to analyzing the first table, input a number here to
analyze the nth table
Defaults to analyzing the first table input a number here to analyze the nth table