3.教えた言葉を話せるようになる
言葉を教えて~それをしゃべらせるようにしてみようと思う。
流れとしては
言葉を教える
↓
教えた言葉を話す
という流れ
初回にやったオウム返しの流れを何度も繰り返して言葉を蓄積していく
import random
sentences=[]
while 1:
sentence = input()
if sentence not in '話して':
sentences.append(sentence)
print('覚えたよ!')
else:
print('えっとね…。')
print(random.choice(sentences))
しかしこのままでは同じ言葉を覚えてしまうため
もう 少し改良を加える
import random
sentences=[]
print(sentences)
while 1:
sentence = input()
if sentence not in '話して':
if sentence not in sentences:
sentences.append(sentence)
print('覚えたよ!')
else:
print('あ!聞いたことあるかも!')
else:
print('えっとね…。')
print(random.choice(sentences))
今日は寒い
覚えたよ!
今日は暑い
覚えたよ!
今日はお腹がすいた
覚えたよ!
お腹すいた
覚えたよ!
お腹すいた
あ!聞いたことあるかも!
話して
えっとね…。
今日は暑い
最後に覚えたことを外部出力
次回話す際にはCSVを読みこんで学習(?)させていく
import random
import csv
import os
sentences=[]
if os.path.exists("Path/learn.csv") != False:
with open("Path/learn.csv", 'r', newline='') as read:
reader = csv.reader(read)
for cow in reader:
sentences.append(cow)
print(sentences)
while 1:
sentence = input()
if sentence == 'バイバイ':
with open("Path/learn.csv", 'a', newline='') as writefile:
for cow in sentences:
writefile.write(cow + '\n')
break
elif sentence not in '話して':
if sentence not in sentences:
sentences.append(sentence)
print('覚えたよ!')
else:
print('あ!聞いたことあるかも!')
else:
print('えっとね…。')
print(random.choice(sentences))
なんだかAIっぽいね