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.
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