1、功能需求

本实例完成的是人工智能作业--简单动物识别专家系统。要求能输入一个或者多个特征推理出结果,能有简单的界面,对知识库的修改等。需要的基础知识有C#,winform, 以及对文本的简单处理,数组结构用的是数组和字典。

2、界面设计

程序界面有两个Form,一个是主窗口如图1,一个是修改知识库的窗口如图2,设计都非常简单,在此不用详细给出具体啦。

图1 Main.Form

图2 Update.Form

3、数据准备

因为只是个简单的案例,数据不多,因此数据采用的文本的形式如图3,每行是一条规则,中间以空格隔开,最后一个是结果。

图3 知识库

4、代码实现

我的思想是,以每行数据的特征为key,结果为value,然后获取输入的特征,去遍历知识库,如果输入的特征有几个在全部在一行知识里,那么把这几个特征移除并把这行知识的value添加到输入的features中,按这个步骤,直到遍历完知识库,得到最后的结果。代码如下:

文件读写类:FileUtil.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsAppAnimalRegonize
{
    class FileUtils
    {
        //读取文件
        public static string read_data(string data_path)
        {
            string text = null;
            //【1】创建文件流
            FileStream fs = new FileStream(data_path, FileMode.Open);
            //【2】创建读取器
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            //【3】以流的方式读取数据
            text = sr.ReadToEnd();
            //【4】关闭读取器
            sr.Close();
            //【5】关闭文件流
            fs.Close();
           return text;
        }
        //写入文件
        public static void save_update(string data_path,string text)
        {
            FileStream fs = new FileStream(data_path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.Write(text.Trim());
            sw.Close();
            fs.Close();
        }
    }

}

修改知识类:FormUpdate.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsAppAnimalRegonize
{
    public partial class FormUpdate : Form
    {
        private string data_path = Environment.CurrentDirectory + "\\rule.txt";
        public FormUpdate()
        {
            Console.WriteLine(data_path);
            InitializeComponent();
            textBox_update.Text = FileUtils.read_data(data_path);
        }
        private void button_save_Click(object sender, EventArgs e)
        {
            FileUtils.save_update(data_path,textBox_update.Text);
            this.Close();
        }
    }

}

主类:Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsAppAnimalRegonize

{

//一行事实bean类

class RowFact
    {
        private List<string> ls = null;
        public RowFact(List<string> ls)
        {
            this.ls = ls;
        }
        public void setLs(List<string> ls)
        {
            this.ls = ls;
        }
        public List<string> getLs()
        {
            return this.ls;
        }
    }
    public partial class Form1 : Form
    {
        private string data_path = Environment.CurrentDirectory + "\\rule.txt";

public Form1()
        {
            InitializeComponent();
        }

private void button_update_Click(object sender, EventArgs e)
        {
            FormUpdate formUpdate = new FormUpdate();
            formUpdate.Show();
        }
        //解析知识库
        private Dictionary<RowFact, string> parse_text(string text)
        {
            var array_facts = text.Split(Environment.NewLine.ToCharArray());
            var dic_result = new Dictionary<RowFact, string>();
            for (int i = 0; i < array_facts.Length; i++)
            {
                var fact_items = array_facts[i].Split(" ".ToCharArray());
                var row_fact_list = new List<string>();
                for (int j = 0; j < fact_items.Length; j++)
                {
                    if (j == fact_items.Length - 1)
                    {
                        var row_fact = new RowFact(row_fact_list);
                        dic_result.Add(row_fact,fact_items[j]);
                    }
                    else
                    {
                        row_fact_list.Add(fact_items[j]);
                    }
                }
            }
            return dic_result;
        }
        //推理过程
        private void reasoning(List<string> features,Dictionary<RowFact,string> dict)
        {
            foreach (KeyValuePair<RowFact, string> pair in dict)
            {
                if (list_contains_list( pair.Key.getLs(),features) )
                {
                    foreach (var item in pair.Key.getLs())
                        if (features.Contains(item))
                        {
                            features.Remove(item);
                            textBox_process.AppendText(item +"->");
                        }
                    if (!features.Contains(pair.Value))
                    {
                        features.Add(pair.Value);
                        textBox_process.AppendText(pair.Value);
                        textBox_process.AppendText("\n");
                    }
                }
            }
            var temp_item = features[features.Count - 1];
            foreach (var item in dict.Values)
            {
                if (item == temp_item)
                {
                    label_result.Text = item;
                    return;
                }
            }
            label_result.Text = "推理不出来";
        }
        //规则库中的一行规则的特征是否满足输入的特征中的几条
        private bool list_contains_list(List<string> list1, List<string> list2)
        {
            bool is_contains = true;
            if (list1.Count>list2.Count)
            {
                foreach (var item in list2)
                {
                    if (!list1.Contains(item))
                    {
                        is_contains = false;
                        return is_contains;
                    }
                }
            }
            else
            {
                foreach (var item in list1)
                {
                    if (!list2.Contains(item))
                    {
                        is_contains = false;
                        return is_contains;
                    }
                }
            }
            return is_contains;
        }
        private void button_search_Click(object sender, EventArgs e)
        {
            var input_features = textBox_features.Text.Split(Environment.NewLine.ToCharArray());
            reasoning(input_features.ToList<string>(), parse_text(FileUtils.read_data(data_path)));
        }
    }
}

5、结果

修改和推理都能正确进行。输入例子如图4,每行为一个特征。

图4 测试结果

C# winform 动物识别专家系统相关推荐

  1. 动物识别专家系统c语言代码,动物识别专家系统(C++版)

    --------------------- 本文来自 Robin_just 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/shaguabufadai/article/ ...

  2. 动物识别系统代码python_人工智能-动物识别专家系统算法Python + Pyqt 实现

    #-*- coding: utf-8 -*- #Form implementation generated from reading ui file '动物识别专家系统.ui'# #Created b ...

  3. python动物专家系统_动物识别专家系统 实验报告

    学 号 XXXXX 姓 名 XXXXX 实验名称 动物识别专家系统 实验目的 本实验的主要目的是熟练使用推理方法, 进行编程完成相应的功 能. 本次试验的预期功能是在系统可以像使用者提出问题, 然后系 ...

  4. 人工智能导论-动物识别专家系统

    动物识别专家系统实验 这个人工智能导论实验可算是折磨了我许久,关键是我一直想不明白该如何推理. 在某天的晚上,我去对象那里上晚自习,突然灵感大开.由于电脑没有电了,我就手绘了一个系统结构图,并且想到了 ...

  5. 动物识别系统代码python_动物识别专家系统课程设计

    title: 动物识别专家系统课程设计 date: 2017-12-23 18:44:13 tags: categories: python 设计一个可以识别7种动物的专家系统,可以根据前提推导出结论 ...

  6. JavaScript 实现动物识别专家系统交互演示

    本文首发且更新于个人博客: https://www.xerrors.fun/Animal-Identification-Expert-System/ 前言:本篇文章主要是介绍什么是专家系统,接下来会使 ...

  7. matlab——一个简单的动物识别专家系统

    本实验中用matlab实现一个简单的动物识别专家系统,该系统是用来识7种动物的,但在规则库中形成了15条规则.首先将动物分成哺乳动物.鸟.蹄类动物.肉食动物4大类,然后通过继续询问得到要的结果.本程序 ...

  8. 动物识别专家系统 Java 简单实现

    不再BB什么是专家系统了,自行百度,这篇博客专门帮助写作业的,人工智能导论课要求写一个动物识别专家系统,这就是一个很好的实现,编了2天,有界面,有功能,分享给需要的同学. 直接上源代码,开箱即用,包括 ...

  9. 动物识别专家系统PYTHON

    能搜到这个文章的,估计你就是为了想找篇现成的系统抄抄了,我也废话不多说好叭. 下面这些是我们当时的要求和加分项: 1.建立一个动物识别系统的规则库,用以识别虎.豹.斑马.长颈鹿.企鹅.鸵鸟.信天翁等7 ...

最新文章

  1. 一生受益的三个小故事
  2. 朱晔的互联网架构实践心得S1E2:屡试不爽的架构三马车
  3. 64xWin7Orcale安装
  4. 【PC工具】创客、arduino爱好者必备,最最简单好用硬件电路连线绘图工具Fritzing...
  5. matlab中if语句的用法_if语句的基本用法
  6. flutter dart Md5加密
  7. 二十、App爬虫环境搭建并测试监听微信
  8. Java集合(4)--List接口及其实现类ArrayList、LinkedList和Vector
  9. PlayMaker 不支持过渡条件
  10. grep和正则表达式
  11. sqlite or svn 错误 The database disk image is malformed 可解决
  12. office visio连接线控制方法
  13. MySQL 索引原理 图文讲解
  14. js将两张图片合成一张图片
  15. SAP MM采购信息记录作用与浅析
  16. 数据开源 | 跨境电商场景中日平行语料1000
  17. 身份证号码中间某几位数字隐藏
  18. 某电商客户数据价值分析项目
  19. Docker buil提示https://registry-1.docker.io/v2/: read tcp 10.221->:443: read: connection reset by peer
  20. JAVA编写PTA(10分)

热门文章

  1. 云呐|企事业单位如何选择固定资产盘点管理系统
  2. jetpack compose原理解析
  3. cf Educational Codeforces Round 42 C. Make a Square
  4. 论坛回帖时同样离不开验证码识别软件
  5. 【ML】随机森林(Random Forest) 从入门到放弃再到掌握
  6. Python表白比心
  7. BUPT OJ144 SmallTalk
  8. Node 之 node-schedule 定时器
  9. select转insert语句
  10. 搞定暴利产品之门户广告篇