`
mushme
  • 浏览: 777717 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

ruby版的wobot机器人

 
阅读更多
这两天戳了下ruby的代码,感觉还不错,写起来很容易,索性把wobot用ruby重写下,算是练习了。
puts "I'm wobot,欢迎和我随便聊点什么。"
time = Time.new
questionAliasSetting = Hash.new #声明一个hash
#开始初始化问题和答案
questionAliasSetting.store("你好,很高兴见到你。",Array["你好","hello","您好","你好啊","hi"])
questionAliasSetting.store("我是leo的小兄弟heo。",Array["名字","你叫什么","你是谁","你的名字是什么","姓名","name","我问你是谁","我问你叫什么"])
questionAliasSetting.store("鹅鹅鹅, 曲项向天歌。 白毛浮绿水, 红掌拨清波",Array["王洛宾鹅","关于鹅的诗","鹅","鹅鹅鹅","eee"])
questionAliasSetting.store(time.strftime("%Y-%m-%d"),Array["日期","当前日期","今天","date","today"])
questionAliasSetting.store(time.strftime("%Y-%m-%d %H:%M:%S"),Array["时间","当前时间","现在是","now","time"])

qSetting=Hash.new
questionAliasSetting.each_key do |qamswer|
	questionAliasSetting[qamswer].each do |qquestion|
		qSetting.store(qquestion,qamswer);
	end
end

noAnswer = Array["你问的问题我不知道哪","没听明白你说的","我搞不懂你说的哪","不清楚","不知道"]#没有匹配答案的时候,显示这个

loop{
qput = gets.chomp()
qput = qput.encode("UTF-8")
if qput=="exit" 
	puts "输入了exit,程序即将退出";
	break;
else
	if qSetting[qput]!= nil
		puts "  ->"+qSetting[qput]
	else
		puts "  ->"+noAnswer[rand(noAnswer.length)]
		
	end
end
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics