当前位置:   article > 正文

python3.8使用aiml总结_alice_path

alice_path

model_aiml--aiml对话执行主文件

  1. # -*- coding: utf-8 -*-
  2. import aiml
  3. import sys
  4. import os
  5. def get_module_dir(name):
  6. path = getattr(sys.modules[name], '__file__', None)
  7. if not path:
  8. raise AttributeError('module %s has not attribute __file__' % name)
  9. return os.path.dirname(os.path.abspath(path))
  10. # alice_path = get_module_dir('aiml') + '/botdata/alice'
  11. alice_path = '/Users/admin/Desktop/Study_aiml'
  12. # 切换到.aiml所在工作目录
  13. os.chdir(alice_path)
  14. alice = aiml.Kernel()
  15. # 通过std-startup.xml启动aiml
  16. alice.learn("std-startup.xml")
  17. # aiml文件有修改时可以通过load aiml b(在xml中pattern配置)进行修改
  18. alice.respond('LOAD AIML B')
  19. while True:
  20. # 对话接入位置
  21. print(alice.respond(input("Enter your message >> ")))
std-startup.xml--固定写法,可配置多个aiml
  1. <aiml version="1.0.1" encoding="UTF-8">
  2. <!-- std-startup.xml -->
  3. <!-- Category是一个自动的AIML单元 -->
  4. <category>
  5. <!-- Pattern用来匹配用户输入 -->
  6. <!-- 如果用户输入 "LOAD AIML B" -->
  7. <pattern>LOAD AIML B</pattern>
  8. <!-- Template是模式的响应 -->
  9. <!-- 这里学习一个aiml文件 -->
  10. <template>
  11. <learn>basic_chat.aiml</learn>
  12. <learn>stude.aiml</learn>
  13. <!-- 你可以在这里添加更多的aiml文件 -->
  14. <!--<learn>more_aiml.aiml</learn>-->
  15. </template>
  16. </category>
  17. </aiml>

basic_chat.aiml--机器人对话内容储存文件

  1. <aiml version="1.0.1" encoding="UTF-8">
  2. <!-- basic_chat.aiml -->
  3. <category>
  4. <pattern>HELLO</pattern>
  5. <template>
  6. Well, hello!
  7. </template>
  8. </category>
  9. <!--that使用方法,在于承上启下,当前回复内容查找aiml中所有that,有则可以根据输入内容进行pattern匹配,无则进行新一轮问答-->
  10. <category>
  11. <pattern>你是男生吗?</pattern>
  12. <template>
  13. 先告诉我你是么?
  14. </template>
  15. </category>
  16. <category>
  17. <pattern></pattern>
  18. <that>先告诉我你是么?</that>
  19. <template>
  20. 我也是
  21. </template>
  22. </category>
  23. <category>
  24. <pattern>不是</pattern>
  25. <that>先告诉我你是么?</that>
  26. <template>
  27. 我也不是
  28. </template>
  29. </category>
  30. <!--srai使用方法:srai会根据整句去查找整个aiml中的pattern,匹配到后template返回-->
  31. <category>
  32. <pattern>张的性别是</pattern>
  33. <template>
  34. </template>
  35. </category>
  36. <category>
  37. <pattern>李的性别是</pattern>
  38. <template>
  39. </template>
  40. </category>
  41. <category>
  42. <pattern>你知道*的性别是*</pattern>
  43. <template>
  44. <srai><star index="1"/>的性别是</srai>
  45. </template>
  46. </category>
  47. <!--random+li标签随机选取一个进行回复-->
  48. <category>
  49. <pattern>你好</pattern>
  50. <template>
  51. <random>
  52. <li>你好1</li>
  53. <li>你好2</li>
  54. <li>你好3</li>
  55. <li>你好4</li>
  56. <li>你好5</li>
  57. <li>你好6</li>
  58. </random>
  59. </template>
  60. </category>
  61. <!--set,get标签get根据name获取set的值-->
  62. <category>
  63. <pattern>我是*</pattern>
  64. <template>
  65. <set name = "name1"> <star/></set>你好
  66. </template>
  67. </category>
  68. <category>
  69. <pattern>我的姓名是*</pattern>
  70. <template>
  71. 你的姓名是<get name = "name1"/>
  72. </template>
  73. </category>
  74. <!--think标签,记录变量值不返回给用户-->
  75. <category>
  76. <pattern>think我是*</pattern>
  77. <template>
  78. <think><set name = "name"> <star/></set></think>你好
  79. </template>
  80. </category>
  81. <category>
  82. <pattern>我的姓名是*</pattern>
  83. <template>
  84. 你的姓名是<get name = "name"/>
  85. </template>
  86. </category>
  87. <!--condition使用方法,根据输入信息返回不同的结果-->
  88. <category>
  89. <pattern>我*</pattern>
  90. <template>
  91. <think>
  92. <set name = "state"><star index = "1"/></set>
  93. </think>
  94. <condition name = "state" value = "开心">
  95. 我也开心
  96. </condition>
  97. <condition name = "state" value = "不开心">
  98. 我也不开心
  99. </condition>
  100. </template>
  101. </category>
  102. <category>
  103. <pattern>WHAT ARE YOU</pattern>
  104. <template>
  105. I'm a bot, silly!
  106. </template>
  107. </category>
  108. </aiml>

 

stude.aiml --机器人自我学习

  1. <aiml version="1.0.1" encoding="UTF-8">
  2. <!-- std-startup.xml -->
  3. <!-- Category是一个自动的AIML单元 -->
  4. <category>
  5. <!-- Pattern用来匹配用户输入 -->
  6. <!-- 如果用户输入 "LOAD AIML B" -->
  7. <pattern>* 答案是 *</pattern>
  8. <!-- Template是模式的响应 -->
  9. <!-- 这里学习一个aiml文件 -->
  10. <template>
  11. <!--system可以调用python命令,从而往aiml中写入对话-->
  12. <system>python3 learn.py '<star index="1"/>' '<star index="2"/>'</system>
  13. <learn>basic_chat.aiml</learn>
  14. 好的我学会了
  15. <!-- 你可以在这里添加更多的aiml文件 -->
  16. <!--<learn>more_aiml.aiml</learn>-->
  17. </template>
  18. </category>
  19. </aiml>

learn.py--写入aiml方法

  1. # -*- coding: utf-8 -*-
  2. import sys
  3. # print(sys.argv)
  4. template = """
  5. <category>
  6. <pattern>{}</pattern>
  7. <template>
  8. {}
  9. </template>
  10. </category>"""
  11. file = open("basic_chat.aiml","rb+")
  12. content = file.read()
  13. # seek定位至aiml中最后一行之前,删除</aiml>
  14. file.seek(-7,1)
  15. file.truncate()
  16. file.close()
  17. file1 = open("basic_chat.aiml","a")
  18. file1.write(template.format(sys.argv[1],sys.argv[2])+"\n"+"</aiml>")
  19. file1.close()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/374668
推荐阅读
相关标签
  

闽ICP备14008679号