text_extensions_for_pandas/io/watson/nlu.py
def parse_response(response: Dict[str, Any],
                   original_text: str = None,
                   apply_standard_schema: bool = False) -> Dict[str, pd.DataFrame]:
    """
    Parse a Watson NLU response as a decoded JSON string, e.g. dictionary containing
    requested features and convert into a dict of Pandas DataFrames. The following
    features in the response will be converted:

        * entities
        * entity_mentions (elements of the "mentions" field of `response["entities"]`)
        * keywords
        * relations
        * semantic_roles
        * syntax

    For information on getting started with Watson Natural Language Understanding on
    IBM Cloud, see
    https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-getting-started.
    A Python SDK for authentication and making requests to the service is provided at
    https://github.com/watson-developer-cloud/python-sdk.  Details on the supported
    features and available options when making the request can be found at
    https://cloud.ibm.com/apidocs/natural-language-understanding?code=python#analyze-text.

    .. note:: Additional feature data in response will not be processed

    >>> response = natural_language_understanding.analyze(
    ...     url="https://raw.githubusercontent.com/CODAIT/text-extensions-for-pandas/master/resources/holy_grail.txt",
    ...         return_analyzed_text=True,
    ...         features=Features(
    ...         entities=EntitiesOptions(sentiment=True),
    ...         keywords=KeywordsOptions(sentiment=True, emotion=True),
    ...         relations=RelationsOptions(),
    ...         semantic_roles=SemanticRolesOptions(),
    ...         syntax=SyntaxOptions(sentences=True, tokens=SyntaxOptionsTokens(lemma=True, part_of_speech=True))
    ...     )).get_result()
    >>> dfs = parse_response(response)
    >>> dfs.keys()
    dict_keys(['syntax', 'entities', 'keywords', 'relations', 'semantic_roles'])
    >>> dfs["syntax"].head()
       span part_of_speech      lemma  \
    0  [0, 5): 'Monty'          PROPN    None
    1  [6, 12): 'Python'        PROPN  python
    <BLANKLINE>
                                                sentence
    0  [0, 273): 'Monty Python and the Holy Grail is ...
    1  [0, 273): 'Monty Python and the Holy Grail is ...

    :param response: A dictionary of features from the IBM Watson NLU response
    :param original_text: Optional original text sent in request, if None will
                          look for "analyzed_text" keyword in response
    :param apply_standard_schema: Return DataFrames with a set schema, whether data
                                  was present in the response or not

    :return: A dictionary mapping feature name to a Pandas DataFrame
    """
    dfs = {}

    if original_text is None and "analyzed_text" in response:
        original_text = response["analyzed_text"]

    # Create the syntax DataFrame
    syntax_response = response.get("syntax", {})
    token_df, sentence_df = _make_syntax_dataframes(syntax_response, original_text)
    sentence_series = sentence_df.get("sentence_span")
    if sentence_series is not None:
        syntax_df = _merge_syntax_dataframes(token_df, sentence_series)
    else:
        syntax_df = pd.concat([token_df, sentence_df], axis=1)
    dfs["syntax"] = util.apply_schema(syntax_df, _syntax_schema, apply_standard_schema)

    if original_text is None and "span" in dfs["syntax"].columns:
        char_span = dfs["syntax"]["span"]
        if isinstance(char_span, SpanArray):
            original_text = dfs["syntax"]["span"].target_text
        else:
            warnings.warn("Did not receive and could not build original text")

    # Create the entities DataFrames
    entities = response.get("entities", [])
    entities_df, entity_mentions_df = _make_entity_dataframes(entities, original_text)
    dfs["entities"] = util.apply_schema(entities_df, _entities_schema,
                                        apply_standard_schema)
    dfs["entity_mentions"] = util.apply_schema(entity_mentions_df, _entity_mentions_schema,
                                               apply_standard_schema)

    # Create the keywords DataFrame
    keywords = response.get("keywords", [])
    keywords_df = util.make_dataframe(keywords)
    dfs["keywords"] = util.apply_schema(keywords_df, _keywords_schema,
                                        apply_standard_schema)

    # Create the relations DataFrame
    relations = response.get("relations", [])
    relations_df = _make_relations_dataframe(relations, original_text, sentence_series)
    dfs["relations"] = util.apply_schema(relations_df, _relations_schema,
                                         apply_standard_schema)

    # Create the semantic roles DataFrame
    semantic_roles = response.get("semantic_roles", [])
    semantic_roles_df = util.make_dataframe(semantic_roles)
    dfs["semantic_roles"] = util.apply_schema(semantic_roles_df, _semantic_roles_schema,
                                              apply_standard_schema)

    if "warnings" in response:
        # TODO: check structure of warnings and improve message
        warnings.warn(str(response["warnings"]))

    return dfs

    Parse a Watson NLU response as a decoded JSON string, e.g. dictionary containing
    requested features and convert into a dict of Pandas DataFrames. The following
    features in the response will be converted:

        * entities
        * entity_mentions (elements of the "mentions" field of `response["entities"]`)
        * keywords
        * relations
        * semantic_roles
        * syntax

    For information on getting started with Watson Natural Language Understanding on
    IBM Cloud, see
    https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-getting-started.
    A Python SDK for authentication and making requests to the service is provided at
    https://github.com/watson-developer-cloud/python-sdk.  Details on the supported
    features and available options when making the request can be found at
    https://cloud.ibm.com/apidocs/natural-language-understanding?code=python#analyze-text.

    .. note:: Additional feature data in response will not be processed

    >>> response = natural_language_understanding.analyze(
    ...     url="https://raw.githubusercontent.com/CODAIT/text-extensions-for-pandas/master/resources/holy_grail.txt",
    ...         return_analyzed_text=True,
    ...         features=Features(
    ...         entities=EntitiesOptions(sentiment=True),
    ...         keywords=KeywordsOptions(sentiment=True, emotion=True),
    ...         relations=RelationsOptions(),
    ...         semantic_roles=SemanticRolesOptions(),
    ...         syntax=SyntaxOptions(sentences=True, tokens=SyntaxOptionsTokens(lemma=True, part_of_speech=True))
    ...     )).get_result()
    >>> dfs = parse_response(response)
    >>> dfs.keys()
    dict_keys(['syntax', 'entities', 'keywords', 'relations', 'semantic_roles'])
    >>> dfs["syntax"].head()
       span part_of_speech      lemma  \
    0  [0, 5): 'Monty'          PROPN    None
    1  [6, 12): 'Python'        PROPN  python
    <BLANKLINE>
                                                sentence
    0  [0, 273): 'Monty Python and the Holy Grail is ...
    1  [0, 273): 'Monty Python and the Holy Grail is ...

    :param response: A dictionary of features from the IBM Watson NLU response
    :param original_text: Optional original text sent in request, if None will
                          look for "analyzed_text" keyword in response
    :param apply_standard_schema: Return DataFrames with a set schema, whether data
                                  was present in the response or not

    :return: A dictionary mapping feature name to a Pandas DataFrame
    
Parse a Watson NLU response as a decoded JSON string, e.g. dictionary containing
requested features and convert into a dict of Pandas DataFrames. The following
features in the response will be converted.

entities
entity_mentions (elements of the "mentions" field of `response["entities"]`)
keywords
relations
semantic_roles
syntax



: Additional feature data in response will not be processed


Parse a Watson NLU response as a decoded JSON string e . g . dictionary containing requested features and convert into a dict of Pandas DataFrames . The following features in the response will be converted . entities entity_mentions ( elements of the " mentions " field of ` response [ " entities " ] ` ) keywords relations semantic_roles syntax : Additional feature data in response will not be processed
def parse_response(response: Dict[str, Any],
                   original_text: str = None,
                   apply_standard_schema: bool = False) -> Dict[str, pd.DataFrame]:
    dfs = {}
    if original_text is None and "analyzed_text" in response:
        original_text = response["analyzed_text"]
    syntax_response = response.get("syntax", {})
    token_df, sentence_df = _make_syntax_dataframes(syntax_response, original_text)
    sentence_series = sentence_df.get("sentence_span")
    if sentence_series is not None:
        syntax_df = _merge_syntax_dataframes(token_df, sentence_series)
    else:
        syntax_df = pd.concat([token_df, sentence_df], axis=1)
    dfs["syntax"] = util.apply_schema(syntax_df, _syntax_schema, apply_standard_schema)
    if original_text is None and "span" in dfs["syntax"].columns:
        char_span = dfs["syntax"]["span"]
        if isinstance(char_span, SpanArray):
            original_text = dfs["syntax"]["span"].target_text
        else:
            warnings.warn("Did not receive and could not build original text")
    entities = response.get("entities", [])
    entities_df, entity_mentions_df = _make_entity_dataframes(entities, original_text)
    dfs["entities"] = util.apply_schema(entities_df, _entities_schema,
                                        apply_standard_schema)
    dfs["entity_mentions"] = util.apply_schema(entity_mentions_df, _entity_mentions_schema,
                                               apply_standard_schema)
    keywords = response.get("keywords", [])
    keywords_df = util.make_dataframe(keywords)
    dfs["keywords"] = util.apply_schema(keywords_df, _keywords_schema,
                                        apply_standard_schema)
    relations = response.get("relations", [])
    relations_df = _make_relations_dataframe(relations, original_text, sentence_series)
    dfs["relations"] = util.apply_schema(relations_df, _relations_schema,
                                         apply_standard_schema)
    semantic_roles = response.get("semantic_roles", [])
    semantic_roles_df = util.make_dataframe(semantic_roles)
    dfs["semantic_roles"] = util.apply_schema(semantic_roles_df, _semantic_roles_schema,
                                              apply_standard_schema)
    if "warnings" in response:
        warnings.warn(str(response["warnings"]))
    return dfs
def parse_response ( response : Dict [ str , Any ] , original_text : str = None , apply_standard_schema : bool = False ) -> Dict [ str , pd . DataFrame ] : dfs = { } if original_text is None and "analyzed_text" in response : original_text = response [ "analyzed_text" ] syntax_response = response . get ( "syntax" , { } ) token_df , sentence_df = _make_syntax_dataframes ( syntax_response , original_text ) sentence_series = sentence_df . get ( "sentence_span" ) if sentence_series is not None : syntax_df = _merge_syntax_dataframes ( token_df , sentence_series ) else : syntax_df = pd . concat ( [ token_df , sentence_df ] , axis = 1 ) dfs [ "syntax" ] = util . apply_schema ( syntax_df , _syntax_schema , apply_standard_schema ) if original_text is None and "span" in dfs [ "syntax" ] . columns : char_span = dfs [ "syntax" ] [ "span" ] if isinstance ( char_span , SpanArray ) : original_text = dfs [ "syntax" ] [ "span" ] . target_text else : warnings . warn ( "Did not receive and could not build original text" ) entities = response . get ( "entities" , [ ] ) entities_df , entity_mentions_df = _make_entity_dataframes ( entities , original_text ) dfs [ "entities" ] = util . apply_schema ( entities_df , _entities_schema , apply_standard_schema ) dfs [ "entity_mentions" ] = util . apply_schema ( entity_mentions_df , _entity_mentions_schema , apply_standard_schema ) keywords = response . get ( "keywords" , [ ] ) keywords_df = util . make_dataframe ( keywords ) dfs [ "keywords" ] = util . apply_schema ( keywords_df , _keywords_schema , apply_standard_schema ) relations = response . get ( "relations" , [ ] ) relations_df = _make_relations_dataframe ( relations , original_text , sentence_series ) dfs [ "relations" ] = util . apply_schema ( relations_df , _relations_schema , apply_standard_schema ) semantic_roles = response . get ( "semantic_roles" , [ ] ) semantic_roles_df = util . make_dataframe ( semantic_roles ) dfs [ "semantic_roles" ] = util . apply_schema ( semantic_roles_df , _semantic_roles_schema , apply_standard_schema ) if "warnings" in response : warnings . warn ( str ( response [ "warnings" ] ) ) return dfs
Parse a Watson NLU response as a decoded JSON string, e.g.
Parse a Watson NLU response as a decoded JSON string e . g .
"""
    Parse a Watson NLU response as a decoded JSON string, e.g. dictionary containing
    requested features and convert into a dict of Pandas DataFrames. The following
    features in the response will be converted:

        * entities
        * entity_mentions (elements of the "mentions" field of `response["entities"]`)
        * keywords
        * relations
        * semantic_roles
        * syntax

    For information on getting started with Watson Natural Language Understanding on
    IBM Cloud, see
    https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-getting-started.
    A Python SDK for authentication and making requests to the service is provided at
    https://github.com/watson-developer-cloud/python-sdk.  Details on the supported
    features and available options when making the request can be found at
    https://cloud.ibm.com/apidocs/natural-language-understanding?code=python#analyze-text.

    .. note:: Additional feature data in response will not be processed

    >>> response = natural_language_understanding.analyze(
    ...     url="https://raw.githubusercontent.com/CODAIT/text-extensions-for-pandas/master/resources/holy_grail.txt",
    ...         return_analyzed_text=True,
    ...         features=Features(
    ...         entities=EntitiesOptions(sentiment=True),
    ...         keywords=KeywordsOptions(sentiment=True, emotion=True),
    ...         relations=RelationsOptions(),
    ...         semantic_roles=SemanticRolesOptions(),
    ...         syntax=SyntaxOptions(sentences=True, tokens=SyntaxOptionsTokens(lemma=True, part_of_speech=True))
    ...     )).get_result()
    >>> dfs = parse_response(response)
    >>> dfs.keys()
    dict_keys(['syntax', 'entities', 'keywords', 'relations', 'semantic_roles'])
    >>> dfs["syntax"].head()
       span part_of_speech      lemma  \
    0  [0, 5): 'Monty'          PROPN    None
    1  [6, 12): 'Python'        PROPN  python
    <BLANKLINE>
                                                sentence
    0  [0, 273): 'Monty Python and the Holy Grail is ...
    1  [0, 273): 'Monty Python and the Holy Grail is ...

    :param response: A dictionary of features from the IBM Watson NLU response
    :param original_text: Optional original text sent in request, if None will
                          look for "analyzed_text" keyword in response
    :param apply_standard_schema: Return DataFrames with a set schema, whether data
                                  was present in the response or not

    :return: A dictionary mapping feature name to a Pandas DataFrame
    """ # Create the syntax DataFrame # Create the entities DataFrames # Create the keywords DataFrame # Create the relations DataFrame # Create the semantic roles DataFrame # TODO: check structure of warnings and improve message
A dictionary mapping feature name to a Pandas DataFrame
A dictionary mapping feature name to a Pandas DataFrame
response
Dict[str, Any]
A dictionary of features from the IBM Watson NLU response
A dictionary of features from the IBM Watson NLU response
original_text
str
Optional original text sent in request, if None will
look for "analyzed_text" keyword in response
Optional original text sent in request if None will look for " analyzed_text " keyword in response
apply_standard_schema
bool
Return DataFrames with a set schema, whether data
was present in the response or not
Return DataFrames with a set schema whether data was present in the response or not