当前位置:   article > 正文

毕业设计:Vue3+FastApi+Python+Neo4j实现主题知识图谱网页应用——前后端:知识问答_python,知识图谱实现智能问答机器人的web界面

python,知识图谱实现智能问答机器人的web界面

简介

完整项目资源链接:https://download.csdn.net/download/m0_46573428/87796553

项目详细信息请看:https://blog.csdn.net/m0_46573428/article/details/130071302

知识问答几乎是所有知识图谱应用里的重头戏,也花了我非常多的精力去写。

功能设计:输入问题,后台检索答案并返回。

主要代码

前端

Html

这段代码是一个Vue.js组件的模板(template),其中包含了网页界面的HTML代码。

该界面主要分为三个部分:

1. 头部信息 在div.head元素中,有一个标题"小军智能问答助手"

2. 中间问答部分 在div.dialogue元素中,包含了对话框。具体而言,它包含了两个子元素:

  • div.dialogue1:用于展示已有的对话内容。其中包含了一个div.left素,用于显示机器人的自我介绍,以及若干对话记录。

  • div.dialogue2:用于编辑新的对话内容。其中包含了一个textarea元素和一个按钮,分别用于编辑问题和发送问题。

3. 右侧栏 在div元素中,定义了一个宽度为29%、高度为490px的元素。它包含了两个子元素:

  • 第一个子元素:包含了一个标题"h2"和一个无序列表,用于列出常见的热点问题;

  • 第二个子元素:包含了一个标题"h2"和一个无序列表,用于推荐相关的文章或内容。

  1. <template>
  2. <div style="padding-top: 8%">
  3. <!-- 小军智能问答助手 -->
  4. <div class="main">
  5. <!-- head -->
  6. <div class="head">
  7. <span>小军智能问答助手</span>
  8. </div>
  9. <!-- 对话框 -->
  10. <div class="dialogue">
  11. <!-- 对话展示 -->
  12. <div class="dialogue1" ref="messageContent">
  13. <div class="left" style="margin-top: 20px">我是智能问答助手小军</div>
  14. <div v-for="(item, index) in dialogue" :key="index" :class="item[0]">
  15. {{ item[1] }}
  16. </div>
  17. </div>
  18. <!-- 对话编辑 -->
  19. <div class="dialogue2">
  20. <textarea
  21. v-model="question"
  22. style="background: rgb(255 255 255 / 0%)"
  23. ></textarea>
  24. <button @click="addRight()">&nbsp;&nbsp;&nbsp;</button>
  25. </div>
  26. </div>
  27. <!-- 右边栏 -->
  28. <div style="height: 490px; width: 29%; float: left">
  29. <div style="height: 50%">
  30. <h2>热点问题</h2>
  31. <ul>
  32. <li>歼-11有哪些缺点?</li>
  33. <li>歼-11有哪些缺点?</li>
  34. <li>歼-11有哪些缺点?</li>
  35. <li>歼-11有哪些缺点?</li>
  36. </ul>
  37. </div>
  38. <div style="height: 50%">
  39. <h2>相关推荐</h2>
  40. <ul>
  41. <li>歼-11</li>
  42. <li>歼-20</li>
  43. <li>XX战斗机</li>
  44. <li>XX战斗机</li>
  45. <li>XX战斗机</li>
  46. </ul>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. </template>

Script

这段代码是一个Vue.js的组件定义,主要实现了以下功能:

1. 定义了一个名称为"Query"的Vue组件,且不依赖于任何外部数据或属性(props为空对象)。

2. 在data()函数中定义了该组件的局部数据,包括:

  • question: 用于绑定问题输入框的值;

  • dialogue: 用于保存对话记录的数组。

  • ans: 用于保存服务器返回的问题回答结果。

3. 在methods中定义了一个名为addRight()的方法,用于在点击"发送"按钮后,将新的问题发送给服务器,并更新对话记录和回答结果。具体而言,该方法会:

  • 将当前问题添加到dialogue数组中,显示在右侧(即"right"类)。

  • 使用axios库向服务器发送GET请求,并传递问题字符串作为查询参数。

  • 当接收到响应时,将其中的回答结果保存在ans变量中,并将其添加到dialogue数组中,显示在左侧(即"left"类)。

  • 当请求失败时,弹出提示窗口:"Query failed!"

  • 清空问题输入框的值

  • 最后使用nextTick()方法以确保滚动条在新消息到来时可以自动滚动到最底部。

  1. <script>
  2. export default {
  3. name: "Query",
  4. props: {},
  5. data() {
  6. return {
  7. question: "",
  8. dialogue: [
  9. ["right", "瓷器中的“罐丞”是什么?"],
  10. [
  11. "left",
  12. "罐丞是一种可以用来保存食物或饮料的瓷器器皿。",
  13. ],
  14. ],
  15. ans: "",
  16. };
  17. },
  18. methods: {
  19. addRight: function () {
  20. this.dialogue.push(["right", this.question]);
  21. this.axios
  22. .get("http://localhost:8000/Query?question=" + this.question)
  23. .then((res) => {
  24. this.ans = res.data;
  25. this.dialogue.push(["left", this.ans]);
  26. console.log(this.ans);
  27. })
  28. .catch((error) => {
  29. alert("Query failed!");
  30. });
  31. this.question = "";
  32. // 让滚动条始终在最底部
  33. this.$nextTick(() => {
  34. this.$refs.messageContent.scrollTop =
  35. this.$refs.messageContent.scrollHeight;
  36. });
  37. },
  38. },
  39. };
  40. </script>

Css

  1. <style scoped>
  2. .main {
  3. height: 550px;
  4. width: 50%;
  5. background: rgb(255 255 255 / 70%);
  6. padding-top: 0%;
  7. border-radius: 5px;
  8. margin: auto;
  9. box-shadow: rgb(0 0 0 / 20%) 0px 3px 10px;
  10. }
  11. li {
  12. list-style: none;
  13. }
  14. .head {
  15. border-radius: 5px 5px 0px 0px;
  16. float: left;
  17. width: 100%;
  18. border-bottom: #e3e3e3 solid 1px;
  19. }
  20. .head > span {
  21. float: left;
  22. font-size: 20px;
  23. line-height: 60px;
  24. color: #486e53;
  25. padding-left: 30px;
  26. font-weight: bold;
  27. }
  28. .dialogue {
  29. border-right: #e3e3e3 solid 1px;
  30. height: 490px;
  31. float: left;
  32. width: 70%;
  33. }
  34. .dialogue1 {
  35. height: 70%;
  36. float: left;
  37. width: 100%;
  38. overflow-y: auto;
  39. }
  40. .dialogue1::-webkit-scrollbar {
  41. width: 5px;
  42. }
  43. .dialogue1::-webkit-scrollbar-thumb {
  44. background-color: #ccc;
  45. border-radius: 5px;
  46. }
  47. .dialogue2 {
  48. border-top: #e3e3e3 solid 1px;
  49. height: 30%;
  50. float: left;
  51. width: 100%;
  52. }
  53. h2 {
  54. color: #486e53;
  55. margin: 15px;
  56. float: left;
  57. clear: both;
  58. }
  59. li {
  60. width: 90%;
  61. float: left;
  62. padding-left: 15px;
  63. padding-bottom: 5px;
  64. color: #486e53;
  65. }
  66. .dialogue > div > button {
  67. height: 35px;
  68. width: 100px;
  69. background: #eee;
  70. float: right;
  71. border: none;
  72. border-radius: 5px;
  73. color: #6da97e;
  74. font-weight: bold;
  75. margin-right: 20px;
  76. }
  77. .dialogue > div > textarea {
  78. width: 93%;
  79. height: 60px;
  80. border: none;
  81. resize: none;
  82. padding: 15px;
  83. outline: none;
  84. }
  85. .left {
  86. float: left;
  87. padding: 10px;
  88. background: #486e53;
  89. margin: 10px 20px 10px 20px;
  90. clear: both;
  91. color: white;
  92. border-radius: 5px;
  93. max-width: 400px;
  94. font-size: 16px;
  95. }
  96. .right {
  97. float: right;
  98. padding: 10px;
  99. background: #017db3;
  100. margin: 10px 20px 10px 20px;
  101. color: white;
  102. border-radius: 5px;
  103. text-align: right;
  104. font-size: 16px;
  105. max-width: 400px;
  106. clear: both;
  107. }
  108. </style>

后端

这段代码实现了一个问题回答的功能,主要包括如下两部分:

  1. 属性类问题的回答 当输入的query中不含有问号"?"时,会解析出问题的主语(例如"歼-11")和属性(例如"翼长"), 然后从数据库中读取相关数据(即./data/china.txt文件),在其中查找与主语和属性均相符的记录。 若找到了符合条件的记录,则返回其中的结果(即瓷器制作瑕疵);否则返回"Oops,数据库中暂时没有相关数据。"

  2. QA 问答形式的问题回答 当输入的query中含有问号"?"时,会将其与事先存储在./data/QA.txt文件中的所有问题匹配,并计算匹配度。 若匹配度大于0.8,则返回对应的答案;否则返回"Oops,数据库中暂时没有相关数据。"

需要注意的是,对于属性类问题,该程序只能处理数据集中包含的内容,而对于QA形式的问题,其结果取决于词向量的匹配程度,因此可能存在不可靠或错误的答案。同时,该程序使用了一些具体的文件路径和数据格式,因此需要保证运行环境符合要求。

  1. def Query(query):
  2. """
  3. 作用:将query进行处理,进行neo4j查询
  4. 输入:问答的query
  5. 输出:neo4j查询后返回的答案
  6. """
  7. # 读取问答数据
  8. with open(r'./data/QA.txt', 'r') as f:
  9. lines = [line.strip() for line in f.readlines() if line.strip()]
  10. odd_lines, even_lines = [], []
  11. for i, line in enumerate(lines):
  12. if i % 2 == 0:
  13. even_lines.append(line)
  14. else:
  15. odd_lines.append(line)
  16. QA = []
  17. for i in range(len(even_lines)):
  18. QA.append([even_lines[i],odd_lines[i]])
  19. # 回答属性类问题
  20. if '?' not in query:
  21. ans = ''
  22. with open(r'./data/china.txt', 'r') as f:
  23. china_list = [line.strip().split(',') for line in f.readlines()]
  24. q = query.split('的')
  25. for i in china_list:
  26. if q[0] == i[0]:
  27. if q[1] == i[1]:
  28. return i[2]
  29. if ans == '':
  30. return 'Oops,数据库中暂时没有相关数据。'
  31. # 回答QA
  32. else:
  33. t = 0
  34. ans = ''
  35. for i in QA:
  36. rate = string_similar(query, i[0])
  37. if rate > t and rate > 0.8:
  38. t = rate
  39. ans = i[1]
  40. if ans == '':
  41. return 'Oops,数据库中暂时没有相关数据。'
  42. else:
  43. return ans
  44. # print(Query('歼-11有哪些缺点?'))
  45. # print(Query('歼-11的翼长'))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/459793
推荐阅读
相关标签
  

闽ICP备14008679号