Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the solution to "string indices must be integers"?

I want to access items from a new dictionary called ‘conversations’ by implementing a for loop.

{" conversations": [
{"tag": "greeting",
"user": ["Hi", " What's your name?", " How are you?", "Hello", "Good day"],
"response": ["Hello, my name is Rosie. Nice to see you", "Good to see you again", " How can I help you?"],
"context_set": ""
},
{"tag": "Good-bye",
"user": ["Bye", "See you", "Goodbye"],
"response": ["Thanks for visiting our company", "Have a nice day", "Good-bye."]
},
{"tag": "thanks",
"user": ["Thanks", "Thank you", "That's helpful", "Appreciated your service" ],
"response": ["Glad to help!", "My pleasure", "You’re welcome."]
}
]
}

The code I use to load the dictionary in a notebook is

    file_name = 'dialogue.txt'
    with open(file_name, encoding='utf8') as f:
        for line in f:
        print(line.strip())
        dialogue_text = f.read()

This line of code does not return any results when trying to access the dictionary.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  for k in dialogue_text:
  print(k)

My intention is to write this code by implementing tokenization and stemming, but it returned an error

words = []
labels = []
docs_x = []
docs_y = []

for conversation in dialogue_text["conversations"]:
    for user in dialogue_text["user"]:
        words = nltk.word_tokenize(user)
        words.extend(words)
        docs_x.append(words)
        docs_y.append(intent["tag"])if intent["tag"] not in labels:
        labels.append(intent["tag"])words = [stemmer.stemWord(w.lower()) for w in words if w 
     != "?"]
     words = sorted(list(set(words)))labels = sorted(labels)

Error Message:

     TypeError                                 Traceback (most recent call last)
     <ipython-input-12-d42234f8e809> in <module>()
     10 docs_y = []
     11 
     ---> 12 for conversation in dialogue_text["conversations"]:
     13     for user in dialogue_text["user"]:
     14         words = nltk.word_tokenize(user)

     TypeError: string indices must be integers

What code should I write to resolve this issue?

>Solution :

The Reason for this Error is the type of dialogue_text[" conversations"] is a "list"

Try this out:

import json

dialogue_text = json.load(open("dialogue.txt", encoding='utf8'))

for conversation in dialogue_text[" conversations"]:
    for user in conversation['user']:
        print(user)

Output:

Hi
What's your name?
 How are you?
Hello
Good day
Bye
See you
Goodbye
Thanks
Thank you
That's helpful
Appreciated your service
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading