質問編集履歴

2

2021/09/14 23:36

投稿

orangekun
orangekun

スコア0

test CHANGED
File without changes
test CHANGED
File without changes

1

2021/09/14 23:36

投稿

orangekun
orangekun

スコア0

test CHANGED
File without changes
test CHANGED
@@ -10,278 +10,270 @@
10
10
 
11
11
 
12
12
 
13
- ``` chatbot-data.json
13
+ chatbot-data.jsonに単語が記録されません
14
+
14
-
15
+ ### 該当のソースコード
16
+
17
+
18
+
19
+ ```python
20
+
21
+ import discord
22
+
23
+ import os, re, json, random
24
+
25
+ from janome.tokenizer import Tokenizer
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+ dict_file = "chatbot-data.json"
36
+
15
- {}
37
+ dic = {}
38
+
16
-
39
+ tokenizer = Tokenizer()
40
+
41
+
42
+
17
-
43
+ if os.path.exists(dict_file):
44
+
45
+ di = json.load(open(dict_file, "r"))
46
+
47
+
48
+
49
+
50
+
51
+ def register_dic(words):
52
+
53
+ global di
54
+
55
+ if list(words) == 0: return
56
+
57
+ tmp = ["@"]
58
+
59
+ for i in words:
60
+
61
+ word = i.surface
62
+
63
+ if word == "" or word == "\r\n" or word == "\n":
64
+
65
+ continue
66
+
67
+ tmp.append(word)
68
+
69
+ if len(tmp) < 3: continue
70
+
71
+ if len(tmp) > 3: tmp = tmp[1:]
72
+
73
+ set_word3(dic, tmp)
74
+
75
+ if word == "。" or word == "?":
76
+
77
+ tmp = ["@"]
78
+
79
+ continue
80
+
81
+ #辞書更新毎にファイル保存
82
+
83
+ with open(dict_file, "w", encoding="utf-8") as f:
84
+
85
+ json.dump(di, f)
86
+
87
+
88
+
89
+ def set_word3(di, s3):
90
+
91
+ w1, w2, w3 = s3
92
+
93
+ if not w1 in dic: dic[w1] = {}
94
+
95
+ if not w2 in dic[w1]: dic[w1][w2] = {}
96
+
97
+ if not w3 in dic[w1][w2]: dic[w1][w2][w3] = 0
98
+
99
+ dic[w1][w2][w3] += 1
100
+
101
+
102
+
103
+
104
+
105
+ def make_sentence(head):
106
+
107
+ ret = []
108
+
109
+ if head not in dic: return ""
110
+
111
+ top = dic[head]
112
+
113
+ w1 = word_choice(top)
114
+
115
+ w2 = word_choice(top[w1])
116
+
117
+ ret.append(w1)
118
+
119
+ ret.append(w2)
120
+
121
+ while True:
122
+
123
+ if w1 in dic and w2 in dic[w1]:
124
+
125
+ w3 = word_choice(dic[w1][w2])
126
+
127
+ else:
128
+
129
+ w3 = ""
130
+
131
+ ret.append(w3)
132
+
133
+ if w3 == "。" or w3 == "?" or w3 == "": break
134
+
135
+ w1, w2 = w2, w3
136
+
137
+ return "".join(ret)
138
+
139
+
140
+
141
+
142
+
143
+ def word_choice(sel):
144
+
145
+ keys = sel.keys()
146
+
147
+ return random.choice(list(keys))
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+ # botに返答させる
156
+
157
+ def make_reply(text):
158
+
159
+ # まず単語を学習する
160
+
161
+ if text[-1] != "。": text += "。"
162
+
163
+ words = tokenizer.tokenize(text)
164
+
165
+ register_dic(words)
166
+
167
+ # 辞書に単語があれば、そこから話す
168
+
169
+ for w in words:
170
+
171
+ face = w.surface
172
+
173
+ ps = w.part_of_speech.split(',')[0]
174
+
175
+ if ps == "感動詞":
176
+
177
+ return face + "。"
178
+
179
+ if ps == "名詞" or ps == "形容詞":
180
+
181
+ if face in dic: return make_sentence(face)
182
+
183
+ return make_sentence("@")
184
+
185
+
186
+
187
+ #ここからメッセージ取得&返信
188
+
189
+
190
+
191
+ #
192
+
193
+ #
194
+
195
+ #以下、discord処理
196
+
197
+ #
198
+
199
+ #
200
+
201
+
202
+
203
+ client = discord.Client()
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+ @client.event
212
+
213
+ async def on_ready():
214
+
215
+ print('Logged in as')
216
+
217
+ print(client.user.name)
218
+
219
+ print(client.user.id)
220
+
221
+ print('------')
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+
231
+ @client.event
232
+
233
+
234
+
235
+ async def on_message(message):
236
+
237
+ if not message.channel.id==884383014199656448:
238
+
239
+ return
240
+
241
+
242
+
243
+ if message.attachments:
244
+
245
+ pass
246
+
247
+
248
+
249
+ if message.author.bot:
250
+
251
+ return
252
+
253
+
254
+
255
+ if message:
256
+
257
+ text = message.content
258
+
259
+ res = make_reply(text)
260
+
261
+ if res == null:
262
+
263
+ return
264
+
265
+ else:
266
+
267
+ await message.channel.send(res)
268
+
269
+
270
+
271
+
272
+
273
+ client.run(os.getenv('token'))
18
274
 
19
275
  ```
20
276
 
21
- chatbot-data.jsonに単語が記録されません
22
-
23
- ### 該当のソースコード
24
-
25
-
26
-
27
- ```python
28
-
29
- import discord
30
-
31
- import os, re, json, random
32
-
33
- from janome.tokenizer import Tokenizer
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
- dict_file = "chatbot-data.json"
44
-
45
- dic = {}
46
-
47
- tokenizer = Tokenizer()
48
-
49
-
50
-
51
- if os.path.exists(dict_file):
52
-
53
- di = json.load(open(dict_file, "r"))
54
-
55
-
56
-
57
-
58
-
59
- def register_dic(words):
60
-
61
- global di
62
-
63
- if list(words) == 0: return
64
-
65
- tmp = ["@"]
66
-
67
- for i in words:
68
-
69
- word = i.surface
70
-
71
- if word == "" or word == "\r\n" or word == "\n":
72
-
73
- continue
74
-
75
- tmp.append(word)
76
-
77
- if len(tmp) < 3: continue
78
-
79
- if len(tmp) > 3: tmp = tmp[1:]
80
-
81
- set_word3(dic, tmp)
82
-
83
- if word == "。" or word == "?":
84
-
85
- tmp = ["@"]
86
-
87
- continue
88
-
89
- #辞書更新毎にファイル保存
90
-
91
- with open(dict_file, "w", encoding="utf-8") as f:
92
-
93
- json.dump(di, f)
94
-
95
-
96
-
97
- def set_word3(di, s3):
98
-
99
- w1, w2, w3 = s3
100
-
101
- if not w1 in dic: dic[w1] = {}
102
-
103
- if not w2 in dic[w1]: dic[w1][w2] = {}
104
-
105
- if not w3 in dic[w1][w2]: dic[w1][w2][w3] = 0
106
-
107
- dic[w1][w2][w3] += 1
108
-
109
-
110
-
111
-
112
-
113
- def make_sentence(head):
114
-
115
- ret = []
116
-
117
- if head not in dic: return ""
118
-
119
- top = dic[head]
120
-
121
- w1 = word_choice(top)
122
-
123
- w2 = word_choice(top[w1])
124
-
125
- ret.append(w1)
126
-
127
- ret.append(w2)
128
-
129
- while True:
130
-
131
- if w1 in dic and w2 in dic[w1]:
132
-
133
- w3 = word_choice(dic[w1][w2])
134
-
135
- else:
136
-
137
- w3 = ""
138
-
139
- ret.append(w3)
140
-
141
- if w3 == "。" or w3 == "?" or w3 == "": break
142
-
143
- w1, w2 = w2, w3
144
-
145
- return "".join(ret)
146
-
147
-
148
-
149
-
150
-
151
- def word_choice(sel):
152
-
153
- keys = sel.keys()
154
-
155
- return random.choice(list(keys))
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
- # botに返答させる
164
-
165
- def make_reply(text):
166
-
167
- # まず単語を学習する
168
-
169
- if text[-1] != "。": text += "。"
170
-
171
- words = tokenizer.tokenize(text)
172
-
173
- register_dic(words)
174
-
175
- # 辞書に単語があれば、そこから話す
176
-
177
- for w in words:
178
-
179
- face = w.surface
180
-
181
- ps = w.part_of_speech.split(',')[0]
182
-
183
- if ps == "感動詞":
184
-
185
- return face + "。"
186
-
187
- if ps == "名詞" or ps == "形容詞":
188
-
189
- if face in dic: return make_sentence(face)
190
-
191
- return make_sentence("@")
192
-
193
-
194
-
195
- #ここからメッセージ取得&返信
196
-
197
-
198
-
199
- #
200
-
201
- #
202
-
203
- #以下、discord処理
204
-
205
- #
206
-
207
- #
208
-
209
-
210
-
211
- client = discord.Client()
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
- @client.event
220
-
221
- async def on_ready():
222
-
223
- print('Logged in as')
224
-
225
- print(client.user.name)
226
-
227
- print(client.user.id)
228
-
229
- print('------')
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
- @client.event
240
-
241
-
242
-
243
- async def on_message(message):
244
-
245
- if not message.channel.id==884383014199656448:
246
-
247
- return
248
-
249
-
250
-
251
- if message.attachments:
252
-
253
- pass
254
-
255
-
256
-
257
- if message.author.bot:
258
-
259
- return
260
-
261
-
262
-
263
- if message:
264
-
265
- text = message.content
266
-
267
- res = make_reply(text)
268
-
269
- if res == null:
270
-
271
- return
272
-
273
- else:
274
-
275
- await message.channel.send(res)
276
-
277
-
278
-
279
-
280
-
281
- client.run(os.getenv('token'))
282
-
283
- ```
284
-
285
277
 
286
278
 
287
279
  ### 試したこと