首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从音调分析器的JSON响应中从字典列表中提取数据

从音调分析器的JSON响应中从字典列表中提取数据
EN

Stack Overflow用户
提问于 2018-11-14 23:23:28
回答 1查看 288关注 0票数 0

我正在使用IBM的声调分析器分析文本,并试图提取与句子音调有关的所有信息(例如:sentence_idtexttonestone_idtone_namescore),并将其添加到数据帧中(有列、sentence_idtexttonestone_idscoretone_name)。这是我的输出的一个示例:

代码语言:javascript
复制
> [{'document_tone': {'tones': [{'score': 0.551743,
     'tone_id': 'analytical',
     'tone_name': 'Analytical'}]},
  'sentences_tone': [{'sentence_id': 0,
    'text': '@jozee25 race is the basis on which quotas are implemented.',
    'tones': []},
   {'sentence_id': 1, 'text': 'helloooooo', 'tones': []}]},
 {'document_tone': {'tones': []}},
 {'document_tone': {'tones': [{'score': 0.802429,
     'tone_id': 'analytical',
     'tone_name': 'Analytical'},
    {'score': 0.60167, 'tone_id': 'confident', 'tone_name': 'Confident'}]},
  'sentences_tone': [{'sentence_id': 0,
    'text': '@growawaysa @cricketandre i have the answer on top yard from dpw:it is not currently "surplus to govt requirements".it is still being used for garaging until a new facility is ready in maitland.the',
    'tones': [{'score': 0.631014,
      'tone_id': 'analytical',
      'tone_name': 'Analytical'}]},
   {'sentence_id': 1,
    'text': 'cost of the housing options will of course depend on prospects for cross subsidisation.',
    'tones': [{'score': 0.589295,
      'tone_id': 'analytical',
      'tone_name': 'Analytical'},
     {'score': 0.509368, 'tone_id': 'confident', 'tone_name': 'Confident'}]}]},
 {'document_tone': {'tones': [{'score': 0.58393,
     'tone_id': 'tentative',
     'tone_name': 'Tentative'},
    {'score': 0.641954, 'tone_id': 'analytical', 'tone_name': 'Analytical'}]}},
 {'document_tone': {'tones': [{'score': 0.817073,
     'tone_id': 'joy',
     'tone_name': 'Joy'},
    {'score': 0.920556, 'tone_id': 'analytical', 'tone_name': 'Analytical'},
    {'score': 0.808202, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]},
  'sentences_tone': [{'sentence_id': 0,
    'text': 'thanks @khayadlangaand colleagues for the fascinating tour yesterday.really',
    'tones': [{'score': 0.771305, 'tone_id': 'joy', 'tone_name': 'Joy'},
     {'score': 0.724236, 'tone_id': 'analytical', 'tone_name': 'Analytical'}]},
   {'sentence_id': 1,
    'text': 'eyeopening and i learnt a lot.',
    'tones': [{'score': 0.572756, 'tone_id': 'joy', 'tone_name': 'Joy'},
     {'score': 0.842108, 'tone_id': 'analytical', 'tone_name': 'Analytical'},
     {'score': 0.75152, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]}]},

这是我为获得这个输出而编写的代码:

代码语言:javascript
复制
result =[]
for i in helen['Tweets']:
   tone_analysis = tone_analyzer.tone(
       {'text': i},
       'application/json'
   ).get_result()
   result.append(tone_analysis)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-15 03:09:13

首先,由于您的JSON格式不太好,所以我使用的是来自音调分析器API引用的这里

使用API引用和Pandas json_normalize中的JSON,下面是我提出的代码

代码语言:javascript
复制
from pandas.io.json import json_normalize

jsonfile = {
  "document_tone": {
    "tones": [
      {
        "score": 0.6165,
        "tone_id": "sadness",
        "tone_name": "Sadness"
      },
      {
        "score": 0.829888,
        "tone_id": "analytical",
        "tone_name": "Analytical"
      }
    ]
  },
  "sentences_tone": [
    {
      "sentence_id": 0,
      "text": "Team, I know that times are tough!",
      "tones": [
        {
          "score": 0.801827,
          "tone_id": "analytical",
          "tone_name": "Analytical"
        }
      ]
    },
    {
      "sentence_id": 1,
      "text": "Product sales have been disappointing for the past three quarters.",
      "tones": [
        {
          "score": 0.771241,
          "tone_id": "sadness",
          "tone_name": "Sadness"
        },
        {
          "score": 0.687768,
          "tone_id": "analytical",
          "tone_name": "Analytical"
        }
      ]
    },
    {
      "sentence_id": 2,
      "text": "We have a competitive product, but we need to do a better job of selling it!",
      "tones": [
        {
          "score": 0.506763,
          "tone_id": "analytical",
          "tone_name": "Analytical"
        }
      ]
    }
  ]
}

mydata = json_normalize(jsonfile['sentences_tone'])
mydata.head(3)
print(mydata)

tones_data = json_normalize(data=jsonfile['sentences_tone'], record_path='tones')
tones_data.head(3)
print(tones_data)

输出数据将是

代码语言:javascript
复制
   sentence_id                        ...                                            tones
0            0                        ...[{'score': 0.801827, 'tone_id': 'analytical', ...
1            1                        ...[{'score': 0.771241, 'tone_id': 'sadness', 'to...
2            2                        ...[{'score': 0.506763, 'tone_id': 'analytical', ...

[3 rows x 3 columns]
      score     tone_id   tone_name
0  0.801827  analytical  Analytical
1  0.771241     sadness     Sadness
2  0.687768  analytical  Analytical
3  0.506763  analytical  Analytical

此外,我还为您创建了REPL,以更改输入并在browser - https://repl.it/@aficionado/DarkturquoiseUnnaturalDistributeddatabase上运行代码。

请参阅此Kaggle链接以了解有关使用Pandas在Python中扁平JSON的更多信息

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53310264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档