此次实验的开发模式为前后端分离的网站开发模式。其中前端技术栈以及用到的框架为vue-cli 3.X以及element。后端使用的为python以及flask。

话不多说,直接贴码:

前端核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<template>
<div class="main">
<Headers></Headers>
<div style="width: 200px;float: left;">
<el-menu default-active="1-4-1" class="el-menu-vertical-demo">
<el-submenu index="1">
<template slot="title">
<i class="el-icon-location"></i>
<span slot="title">导航一</span>
</template>
<el-menu-item-group>
<span slot="title">规则库</span>
<el-menu-item index="1-1" @click="goRules">规则库查看</el-menu-item>
<el-menu-item index="1-2" @click="dialogFormVisible = true">规则库添加</el-menu-item>
</el-menu-item-group>
</el-submenu>
<el-menu-item index="4" @click="open">
<i class="el-icon-setting"></i>
<span slot="title">声明</span>
</el-menu-item>
</el-menu>
</div>
<div class="content">
<div class="box">
<el-input
placeholder="请输入内容"
v-model="input"
clearable style="margin-bottom: 20px">
</el-input>
<el-button type="primary " round size="small" @click="add">添加</el-button>
<el-button type="success " round size="small" @click="confirm">开始识别</el-button>
</div>
</div>
<div class="tag">
<el-tag
:key="tag"
v-for="tag in dynamicTags"
closable
:disable-transitions="false"
@close="handleClose(tag)" style="margin-bottom: 5px">
{{tag}}
</el-tag>
<el-input
class="input-new-tag"
v-if="inputVisible"
v-model="inputValue"
ref="saveTagInput"
size="small"
@keyup.enter.native="handleInputConfirm"
@blur="handleInputConfirm"
>
</el-input>
</div>

<div class="">
<el-dialog title="规则添加" :visible.sync="dialogFormVisible">
<el-form :model="form">
<el-form-item label="前提" :label-width="formLabelWidth">
<el-input v-model="form.rules" autocomplete="off"
placeholder="若存在多个前提,前提与前提之间请用中文逗号隔开"></el-input>
</el-form-item>
<el-form-item label="结论" :label-width="formLabelWidth">
<el-input v-model="form.result" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="结论类型" :label-width="formLabelWidth">
<el-select v-model="form.region" placeholder="请选择结果类型">
<el-option label="最终结论" value="2"></el-option>
<el-option label="中间结论" value="1"></el-option>
</el-select>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="addRules">确 定</el-button>
</div>
</el-dialog>
</div>
</div>
</template>

<script>
import Headers from "./animal/headers";

export default {
name: "animal",
components: {Headers},
data() {
return {
// 输入信息
input: '',
// 标签部分数据
// "吃肉", "有爪", "有犬齿", "眼盯前方", "有毛发", "有奶", "黄褐色", "黑色条纹"
dynamicTags: [],
inputVisible: false,
inputValue: '',

//记录数据的数组,向后端发送
features: [],

//弹出框
dialogFormVisible: false,

//存放添加信息
form: {
rules: '',
result: '',
region: '',
},
formLabelWidth: '120px'
}
},
methods: {
// 用于添加新的事实
add() {
let that = this;
let input = that.input;
//分解输入的字符串得到该点的经纬度坐标
if (input.length > 0) {
// 添加新的标签
that.dynamicTags.push(that.input);

//分别添加对应的坐标
that.features.push(that.input);
that.input = "";
} else {
that.$message({
message: '请将信息输入完整',
type: 'warning'
});
}
},
// 用于提交给后端数据
confirm() {
let that = this
if (that.features.length === 0) {
that.$message({
message: '你好像什么东西都没输入',
type: 'error'
});
} else {
// 将该数据信息传递给后端
that.$axios({
method: 'post',
url: "http://127.0.0.1:5000/test",
data: {
msg: that.features
}
}).then(function (response) {
//输出识别结果
console.log(response.data)
that.$alert(response.data, '识别结果', {
confirmButtonText: '确定',
})
}).catch(function (error) {
alert(error)
})
}
},

//取消指定的输入数据
handleClose(tag) {
this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);

//删除指定的元素
this.features.splice(this.features.indexOf(tag), 1);
},

//输入信息
handleInputConfirm() {
let inputValue = this.inputValue;
if (inputValue) {
this.dynamicTags.push(inputValue);
}
this.inputVisible = false;
this.inputValue = '';
},
//前往规则页面
goRules() {
this.$router.push('/rules')
},
//添加新的规则数据
addRules() {
let that = this;
// 分成数组
var strRules = that.form.rules.split(',');
if (strRules.length === 0 || that.form.result.length === 0 || that.form.region.length === 0) {
that.$message({
message: '请将所有信息填写完整再确定',
type: 'error'
});
} else {
//请求后端数据
// console.log(strRules);
// console.log(that.form.result);
// console.log(that.form.region);
that.$axios({
method: 'post',
url: "http://127.0.0.1:5000/add",
data: {
newrules: strRules,
newresult: that.form.result,
newtype: that.form.region
}
}).then(function (response) {
//输出识别结果
// console.log(response.data)
if (response.data.code === 200) {
that.dialogFormVisible = false;
that.$message({
message: '添加成功,可在规则库进行查看',
type: 'success'
});
}
}).catch(function (error) {
alert(error)
})
}
},
open(){
this.$alert('更多详情请进入我的个人博客进行查看!地址:http://liufanghan.coding.me/', '声明', {
confirmButtonText: '确定',
});
}
},
}
</script>

<style lang="scss">

body {
background-image: url(../assets/background.jpg), linear-gradient(#D3D3D3, #D3D3D3);
background-size: 1920px 1080px;
/*使得背景图片固定不动*/
background-attachment: fixed;
background-blend-mode: darken;
}

.content {
margin-top: 20px;
width: 30%;
}

.box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 450px;
background: lightgray;
padding: 40px;
box-sizing: border-box;
box-shadow: 0 15px 25px rgba(0, 0, 0, .1);
border-radius: 10px;
border: 1px solid #EBEEF5;
}


.tag {
position: absolute;
margin-top: 80px;
left: 50%;
transform: translate(-50%, -50%);
width: 450px;
}

/*标签*/
.el-tag + .el-tag {
margin-left: 10px;
}

.button-new-tag {
margin-left: 10px;
height: 32px;
line-height: 30px;
padding-top: 0;
padding-bottom: 0;
}

.input-new-tag {
width: 90px;
margin-left: 10px;
vertical-align: bottom;
}


.el-menu-vertical-demo:not(.el-menu--collapse) {
width: 200px;
min-height: 400px;
}

</style>

后端核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from flask import Flask
from flask import request
from flask import Response, json

# 解决跨域问题
from flask_cors import *

app = Flask(__name__)
CORS(app, supports_credentials=True)

# 特征数组,用于记录规则和结论
features = ["有毛发", "有奶", "有羽毛", "会飞", "下蛋",
"吃肉", "有犬齿", "有爪", "眼盯前方", "有蹄",
"嚼反刍动物", "黄褐色", "暗斑点", "黑色条纹", "长脖子",
"长腿", "不会飞", "会游泳", "黑白二色", "善飞",
"哺乳动物", "鸟", "食肉动物", "有蹄类动物", "金钱豹",
"虎", "长颈鹿", "斑马", "鸵鸟", "企鹅", "信天翁"]

# 根据特征数组分类,特征0,中间结论1,最终结论2
conclusion = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2]

# 定义规则数组
rule = [
[0],
[1],
[2],
[3, 4],
[5],
[6, 7, 8],
[20, 9],
[20, 10],
[20, 22, 11, 12],
[20, 22, 11, 13],
[23, 14, 15, 12],
[23, 13],
[21, 14, 15, 18, 16],
[21, 17, 16, 18],
[21, 19]
]

# 定义结论数组
result = [20, 20, 21, 21, 22, 22, 23, 23, 24, 25, 26, 27, 28, 29, 30]


# 返回所有的规则
@app.route('/getrules', methods=['POST', 'GET'])
def getrules():
global rule
global features
global result

# 将规则返回成汉字数组
# strRules = rule
strRules = [list() for i in range(len(rule))]
strResult = []
for i in range(len(rule)):
for j in range(len(rule[i])):
strRules[i].append(features[rule[i][j]])

for k in range(len(result)):
strResult.append(features[result[k]])
return Response(json.dumps({'rules': strRules, 'result': strResult}), content_type='application/json')


# 增加规则
@app.route('/add', methods=['POST', 'GET'])
def add():
# 得到传递过来的参数
data1 = request.get_json(silent=True)
newRules = data1['newrules']
newResult = data1['newresult']
newType = data1['newtype']

global rule
global features
global result
global conclusion

# 将其中未含有的前提加入
for i in range(len(newRules)):
if newRules[i] not in features:
features.append(newRules[i])
conclusion.append(0)

# 之后添加到规则中
newruleArr = []

for i in range(len(newRules)):
for j in range(len(features)):
if newRules[i] == features[j]:
# print(j)
newruleArr.append(j)
break

rule.append(newruleArr)
# 判断结论是否存在
if newResult not in features:
features.append(newResult)
conclusion.append(int(newType))
result.append(features.index(newResult))
else:
result.append(features.index(newResult))

return Response(json.dumps({'code': 200}), content_type='application/json')


# 分析得到结论
@app.route('/test', methods=['POST', 'GET'])
def test():
# 动物产生式识别系统
# str1 = request.get_data()
str1 = request.get_json(silent=True)
r = ""
global rule
global features
global result
global conclusion

# 这个地方要处理一下,防止出现很多得规则,找个变量承载
# 切片复制,只复制值,地址空间不同
R = rule[:]
res = result[:]
C = conclusion[:]
F = features[:]
# 扩充规则库
length = len(R)
for i in range(length):
# 将新规则添加到规则数组中
for j in range(len(res)):
if res[j] in R[i]:
# 一个规则的结论在另一条规则的前提中
arr = R[i] + R[j]
arr.remove(res[j])
# 将该规则添加到规则库中
R.append(arr)
# 将结论添加到结论库中
res.append(res[j])

# 扩充后规则数组长度
# print(len(rule))

# 思路,将各个功能转换为函数,传值时单独设计一位用于判断此次请求的操作是什么,根据不同的值返回不同的结果

# 输入相关信息
# msg = ["有犬齿", "眼盯前方", "有毛发", "吃肉", "有爪", "有奶", "黄褐色", "黑色条纹"]

msg = str1['msg']

# msg = ["有毛发", "有奶", "有蹄", "嚼反刍动物", "长脖子", "长腿", "暗斑点", ]
# msg = ["长脖子"]

# 将msg数组转换为数字数组
msgArr = []

for i in range(len(msg)):
for j in range(len(features)):
if msg[i] == features[j]:
# print(j)
msgArr.append(j)
break
# 一开始的输入信息
# print(msgArr)
# return Response(json.dumps({'msgArr': msgArr}),content_type='application/json')

num = 0
num2 = 0
# 开始匹配对应的规则
for i in range(len(R)):
d = [False for c in R[i] if c not in msgArr]
# 包含所有的前提规则
if not d:
# 将得到的结论加入msgArr数组
msgArr.append(res[i])
# 判断此时是否存在最终结论
for j in range(len(msgArr)):
if C[msgArr[j]] == 2:
num = 1
num2 = 1
# print("已判断出该动物")
# print(features[msgArr[j]])
r = features[msgArr[j]]
break
if num == 1:
break

if num == 0:
# 说明未有最终结论
for j in range(len(msgArr)):
if C[msgArr[j]] == 1:
# print(features[msgArr[j]])
r = features[msgArr[j]]
num2 = 1
break
if num2 == 0:
# print("条件不足,无法判断这是个啥东西")
r = "条件不足,无法判断这是个啥东西"
# print(msgArr)
return r


if __name__ == '__main__':
# app.debug = True
app.run()
相关文章
评论
分享
  • 编译原理知识点复习笔记

    马上要期末考试了,临考前紧急自救一下,嘻嘻嘻 有穷自动机转换图: 初始状态(开始状态):只有一个,用start箭头指向 终止状态(接受状态):可以有多个,用双圈表示 最长子串匹配原则:当输入串的多个前缀与一个或多个模式匹配时,总是选择...

    编译原理知识点复习笔记
  • Windows下neo4j的安装

    neo4j是一个高性能的NOSQL图形数据库,他将结构化数据存储在网络上而不是表中。他是一个嵌入式的、基于磁盘的、具备完全的事物特性的Java持久化引擎,neo4j也可看做是一个高性能的图引擎,该引擎具有成熟数据库的所有特性。——百度...

    Windows下neo4j的安装
  • java实现类FTP程序

    继承程序设计实验,实验说明如图所示: 集成程序设计实验 TCP实现首先说明下基于TCP实现的功能: (1)能够实现多用户的同时连接 (2)用户执行成功的命令会在其他用户终端上显式说明 (3)当前用户数以及在线情况会在服务端实时显...

    java实现类FTP程序
  • Java中的流

    流是个抽象的概念,是对输入输出设备的抽象,Java程序中,对于数据的输入输出都是以流的方式进行。设备可以是文件、网络、内存等。 I/O字节流InputStream字节输入流OutputStream字节输出流用于以字节的形式读取和写入数...

    Java中的流
  • eclipse使用

    Eclipse是一个开放源代码的、基于Java的可拓展开发平台。 常用快捷键 快捷键 作用 alt+/ 代码快速补全 ctrl+1 快速修复 ctrl+shift+f 代码格式化 ctrl+d 删除一行代码 ...

    eclipse使用
  • Python网络编程

    网络编程从大的方面说就是对信息的发送到接收,中间传输为物理线路的作用。 它的含义是使用套接字来达到进程间的通信。套接字可以看成是两个网络应用程序进行通信时,各自通信连接中的一个端点。 套接字Socket=(IP地址:端口号) 端口号是...

    Python网络编程
  • Centos安装

    虚拟机下载及安装1.进入VMware官网,转到下载页面 https://my.vmware.com/cn/web/vmware/info/slug/desktop_end_user_computing/vmware_workstati...

    Centos安装
  • JavaEE开发准备

    个人电脑硬件配置: Windows 10 64位家庭中文版 8G运行内存 Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz 1.Java JDK安装及配置(1)下载和安装首先进入oracle网站中Ja...

    JavaEE开发准备
  • Python进阶学习

    假期补习补习Python,防止以后用到炸锅。 闭包在Python语言中,一切皆对象。 闭包:一个函数定义中引用了函数外定义的变量,并且该函数可以在其定义环境外被执行。 闭包 = 函数 + 环境变量 123456789101...

    Python进阶学习
  • 推荐算法研究(一)

    推荐算法大体分为3类:基于系统过滤的推荐、基于内容的推荐、混合推荐 1.基于协同过滤的推荐系统(Collaborative Filtering)使用行为数据,利用集体智慧来推荐。属于有监督学习。基于用户的协同过滤(找和你兴趣相似的人所...

    推荐算法研究(一)
Please check the comment setting in config.yml of hexo-theme-Annie!