音调分析器仅返回1个句子的分析

时间:2017-04-07 09:43:37

标签: ibm-watson watson

使用音调分析仪时,我只能检索1个结果。例如,如果我使用以下输入文本。

string m_StringToAnalyse = "The World Rocks ! I Love Everything !! Bananas are awesome! Old King Cole was a merry old soul!";

结果只返回文档级别和sentence_id = 0的分析,即。 "世界摇滚!"。未返回对后3个句子的分析。

知道我做错了什么或者我错过了什么?运行提供的示例代码时也是如此。

string m_StringToAnalyse = "This service enables people to discover and understand, and revise the impact of tone in their content. It uses linguistic analysis to detect and interpret emotional, social, and language cues found in text.";

使用上面提供的样本句子上的示例代码运行语音分析也会返回文档和第一句的结果。

我尝试过版本" 2016-02-19"以及" 2017-03-15"结果相同。

1 个答案:

答案 0 :(得分:0)

我相信如果你想逐句分析,你需要将每个单独的句子作为JSON对象发送。然后它将在数组中返回分析,其中id = SENTENCE_NUM。

以下是我使用多个YouTube评论(使用Python)所做的一个示例:

def get_comments(video):
    #Get the comments from the Youtube API using requests

    url = 'https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&maxResults=100&videoId='+ video +'&key=' + youtube_credentials['api_key']

    r = requests.get(url)
    comment_dict = list()

    # for item in comments, add an object to the list with the text of the comment

    for item in r.json()['items']:
        the_comment = {"text": item['snippet']['topLevelComment']['snippet']['textOriginal']}
        comment_dict.append(the_comment)

    # return the list as JSON to the sentiment_analysis function
    return json.dumps(comment_dict)

def sentiment_analysis(words):

    # Load Watson Credentials using Python SDK

    tone_analyzer = ToneAnalyzerV3(
    username=watson_credentials['username'], password=watson_credentials['password'], version='2016-02-11')

    # Get the tone, based on the JSON object that is passed to sentiment_analysis
    return_sentiment = json.dumps(tone_analyzer.tone(text=words), indent=2)
    return_sentiment = json.loads(return_sentiment)

之后,您可以使用JSON对象执行任何操作。我还要注意,如果你想对许多物体进行分析,那么任何看这个的人都可以在tone_analyzer.tone函数中添加句子= False。