请给我一段能用于训练ai对话性格的代码
时间: 2024-03-13 17:47:48 浏览: 194
好的,以下是一段用于训练AI对话性格的基础代码:
```python
import random
# 定义一些不同的性格特点
personality = {
'introverted': ['quiet', 'reserved', 'thoughtful'],
'extroverted': ['outgoing', 'friendly', 'talkative'],
'optimistic': ['positive', 'upbeat', 'hopeful'],
'pessimistic': ['negative', 'downbeat', 'hopeless']
}
# 针对每个性格特点,定义一些问候语和回应语
greetings = {
'introverted': ['Hello.', 'Hi there.', 'Nice to see you.'],
'extroverted': ['Hey!', 'What’s up?', 'How are you doing?'],
'optimistic': ['Great to see you!', 'I’m so excited to chat with you!', 'What a beautiful day!'],
'pessimistic': ['Hello, I guess.', 'Not sure what’s so great about today.', 'I’m just here.']
}
responses = {
'introverted': ['Yes, I agree.', 'That’s an interesting point.', 'I can relate to that.'],
'extroverted': ['Absolutely!', 'You’re right!', 'I totally agree!'],
'optimistic': ['Yes, and it’s going to get even better!', 'I know, right? Things are looking up!', 'I’m so glad you feel that way!'],
'pessimistic': ['I don’t know about that.', 'I’m not so sure.', 'I hope you’re right.']
}
# 随机选择一个性格特点
selected_personality = random.choice(list(personality.keys()))
# 随机选择一个问候语
greeting = random.choice(greetings[selected_personality])
# 输出问候语
print(greeting)
# 循环,等待用户输入
while True:
user_input = input('> ').lower()
# 随机选择一个回应语
response = random.choice(responses[selected_personality])
# 输出回应语
print(response)
```
这段代码定义了四种不同的性格特点(内向、外向、乐观和悲观),以及针对每个性格特点的问候语和回应语。在开始对话时,随机选择一个性格特点和问候语,然后循环等待用户输入并输出随机选择的回应语。可以根据需要添加更多的性格特点、问候语和回应语,以及更复杂的逻辑。
阅读全文