لقد قمت بإعداد برنامج روبوت لـ Discord الذي يأخذ رسائل كل مستخدم ويستخدم مصنف تصنيف للمشاعر لتحديد ما إذا كانت الرسالة إيجابية أو سلبية. راجع طلب POST إلى text-processing.com
API أدناه.
أعطيت رسالة 'I am extremely happy'
، ستعرض واجهة برمجة التطبيقات استجابةً عبارة عن سلسلة تشبه JSON:
{"probability": {"neg": 0.32404484915164478, "neutral": 0.021768879244280313, "pos": 0.67595515084835522}, "label": "pos"}
كيف يمكنني تحويل كائن JSON هذا إلى str ، حتى أتمكن من التفاعل بسهولة مع بياناته؟
@bot.event
async def on_message(message):
if message.author == bot.user:
return
with open('data.txt', 'a') as f:
print(repr(message.content), file=f)
response = requests.post('http://text-processing.com/api/sentiment/', {
'text': message.content
}).json()
print(response, file=f)
d = json.loads(response)
print(d["probability"]["pos"], file=f)
f.close()
await bot.process_commands(message)
رمز الخطأ الذي أتلقاه هو ...
File "/Users/enzoromano/PycharmProjects/NewDiscordBot/NewBot.py", line 44, in on_message
d = json.loads(response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 348, in loads
'not {!r}'.format(s.__class__.__name__))
TypeError: the JSON object must be str, bytes or bytearray, not 'dict'