0

This is a super simple question, but I can't find an easy answer:

for petid in X['PetID']:
    sentiment_file = datapath + '/train_sentiment/' + petid + '.json'
    if os.path.isfile(sentiment_file):
        json_data = json.loads(open(sentiment_file).read())
        print(petid, sentiment_file, json_data.sentences.len)

That's my code. My JSON file has:

{
  "sentences": [
    {
      "text": {
        "content": "Fenny was rescued from Old Klang Rd market when she was only 2 months old and I have fostered her since then.",
        "beginOffset": -1
      },
      "sentiment": {
        "magnitude": 0.1,
        "score": -0.1
      }
    },
    {
      "text": {
        "content": "She is 5 mths old now and I hope she can get a good home.",
        "beginOffset": -1
      },
      "sentiment": {
        "magnitude": 0.7,
        "score": 0.7
      }
    },
    {
      "text": {
        "content": "She looks like a mixed breed of local mongrel and Dalmation.",
        "beginOffset": -1
      },
      "sentiment": {
        "magnitude": 0.1,
        "score": 0.1
      }
    },
    {
      "text": {
        "content": "She is a very quiet girl and does not make too much noise, that makes her a very good companion for children or elderly couple.",
        "beginOffset": -1
      },
      "sentiment": {
        "magnitude": 0.9,
        "score": 0.9
      }
    },
    {
      "text": {
        "content": "Please call Mrs Lai of Paws Mission for more adoption details.",
        "beginOffset": -1
      },
      "sentiment": {
        "magnitude": 0,
        "score": 0
      }
    }
  ],
  "tokens": [],
  "entities": [
    {
      "name": "Fenny",
      "type": "PERSON",
      "metadata": {},
      "salience": 0.7863105,
      "mentions": [
        {
          "text": {
            "content": "Fenny",
            "beginOffset": -1
          },
          "type": "PROPER"
        },
        {
          "text": {
            "content": "girl",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "market",
      "type": "OTHER",
      "metadata": {},
      "salience": 0.08208243,
      "mentions": [
        {
          "text": {
            "content": "market",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "Old Klang Rd",
      "type": "LOCATION",
      "metadata": {},
      "salience": 0.04985573,
      "mentions": [
        {
          "text": {
            "content": "Old Klang Rd",
            "beginOffset": -1
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "home",
      "type": "LOCATION",
      "metadata": {},
      "salience": 0.013762235,
      "mentions": [
        {
          "text": {
            "content": "home",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "Lai of Paws Mission",
      "type": "PERSON",
      "metadata": {},
      "salience": 0.011584155,
      "mentions": [
        {
          "text": {
            "content": "Lai of Paws Mission",
            "beginOffset": -1
          },
          "type": "PROPER"
        },
        {
          "text": {
            "content": "Mrs",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "breed",
      "type": "OTHER",
      "metadata": {},
      "salience": 0.01073034,
      "mentions": [
        {
          "text": {
            "content": "breed",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "mongrel",
      "type": "OTHER",
      "metadata": {},
      "salience": 0.009851005,
      "mentions": [
        {
          "text": {
            "content": "mongrel",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "companion",
      "type": "PERSON",
      "metadata": {},
      "salience": 0.00740921,
      "mentions": [
        {
          "text": {
            "content": "companion",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "children",
      "type": "PERSON",
      "metadata": {},
      "salience": 0.00740921,
      "mentions": [
        {
          "text": {
            "content": "children",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "couple",
      "type": "PERSON",
      "metadata": {},
      "salience": 0.00740921,
      "mentions": [
        {
          "text": {
            "content": "couple",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "Dalmation",
      "type": "PERSON",
      "metadata": {},
      "salience": 0.0058382954,
      "mentions": [
        {
          "text": {
            "content": "Dalmation",
            "beginOffset": -1
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "adoption details",
      "type": "OTHER",
      "metadata": {},
      "salience": 0.00517885,
      "mentions": [
        {
          "text": {
            "content": "adoption details",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "noise",
      "type": "OTHER",
      "metadata": {},
      "salience": 0.0025788217,
      "mentions": [
        {
          "text": {
            "content": "noise",
            "beginOffset": -1
          },
          "type": "COMMON"
        }
      ]
    }
  ],
  "documentSentiment": {
    "magnitude": 1.9,
    "score": 0.3
  },
  "language": "en",
  "categories": []
}

However, I get the error:

AttributeError: 'dict' object has no attribute 'sentences'

1 Answer 1

3

Attributes and elements are different things. Also, your data doesn't have a len element, so I assume you want the built-in Python len() function to get the length of the list of sentences.

So where you have:

json_data.sentences.len

You want:

len(json_data["sentences"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.