質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

0回答

583閲覧

形態素解析ができない

orangekun

総合スコア0

Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2021/09/14 16:04

編集2021/09/14 23:36

前提・実現したいこと

janomeというライブラリを使って形態素解析をする人工無能botを作りたい

発生している問題

chatbot-data.jsonに単語が記録されません

該当のソースコード

python

1import discord 2import os, re, json, random 3from janome.tokenizer import Tokenizer 4 5 6 7 8dict_file = "chatbot-data.json" 9dic = {} 10tokenizer = Tokenizer() 11 12if os.path.exists(dict_file): 13 di = json.load(open(dict_file, "r")) 14 15 16def register_dic(words): 17 global di 18 if list(words) == 0: return 19 tmp = ["@"] 20 for i in words: 21 word = i.surface 22 if word == "" or word == "\r\n" or word == "\n": 23 continue 24 tmp.append(word) 25 if len(tmp) < 3: continue 26 if len(tmp) > 3: tmp = tmp[1:] 27 set_word3(dic, tmp) 28 if word == "。" or word == "?": 29 tmp = ["@"] 30 continue 31 #辞書更新毎にファイル保存 32 with open(dict_file, "w", encoding="utf-8") as f: 33 json.dump(di, f) 34 35def set_word3(di, s3): 36 w1, w2, w3 = s3 37 if not w1 in dic: dic[w1] = {} 38 if not w2 in dic[w1]: dic[w1][w2] = {} 39 if not w3 in dic[w1][w2]: dic[w1][w2][w3] = 0 40 dic[w1][w2][w3] += 1 41 42 43def make_sentence(head): 44 ret = [] 45 if head not in dic: return "" 46 top = dic[head] 47 w1 = word_choice(top) 48 w2 = word_choice(top[w1]) 49 ret.append(w1) 50 ret.append(w2) 51 while True: 52 if w1 in dic and w2 in dic[w1]: 53 w3 = word_choice(dic[w1][w2]) 54 else: 55 w3 = "" 56 ret.append(w3) 57 if w3 == "。" or w3 == "?" or w3 == "": break 58 w1, w2 = w2, w3 59 return "".join(ret) 60 61 62def word_choice(sel): 63 keys = sel.keys() 64 return random.choice(list(keys)) 65 66 67 68# botに返答させる 69def make_reply(text): 70 # まず単語を学習する 71 if text[-1] != "。": text += "。" 72 words = tokenizer.tokenize(text) 73 register_dic(words) 74 # 辞書に単語があれば、そこから話す 75 for w in words: 76 face = w.surface 77 ps = w.part_of_speech.split(',')[0] 78 if ps == "感動詞": 79 return face + "。" 80 if ps == "名詞" or ps == "形容詞": 81 if face in dic: return make_sentence(face) 82 return make_sentence("@") 83 84#ここからメッセージ取得&返信 85 86# 87# 88#以下、discord処理 89# 90# 91 92client = discord.Client() 93 94 95 96@client.event 97async def on_ready(): 98 print('Logged in as') 99 print(client.user.name) 100 print(client.user.id) 101 print('------') 102 103 104 105 106@client.event 107 108async def on_message(message): 109 if not message.channel.id==884383014199656448: 110 return 111 112 if message.attachments: 113 pass 114 115 if message.author.bot: 116 return 117 118 if message: 119 text = message.content 120 res = make_reply(text) 121 if res == null: 122 return 123 else: 124 await message.channel.send(res) 125 126 127client.run(os.getenv('token'))

試したこと

ここに問題に対して試したことを記載してください。

補足情報

Python3.9.7
discord.pyではなくpycordというラッパーを使っています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問