ADS logo Algorithms & Data Structures

Expert System Overview

An artificial intelligence, an expert system is a computer system that emulates the decision-making ability of a human expert.

Assignment

Create a console program that can guess your invented word for a freely chosen topic:

  • Before you start you must think how to store your data and which data structure do you choose: Tree, Stack, Queue, List ect.
  • For e.g. you picked the topic animals: The system asks if this animal have beard? yes / no, we answer yes.
  • Does the animal is feathered? yes / no, we answer yes. The system answers i guess: Stork. yes / no, we answer no.
  • Then the system asks, what kind of animal is it? We introduce our answer e.g. Crane and additional question: Does this animal have a grey feather? The system list is appending with a new question and answer.
  • Next time, we’ll ask you an additional question that we recently added.

Expert system implementation example of Pascal’s programming language. You can freely use this code down bellow.

type  el = record
      info : string;
      id, lid, rid : integer;
end;

var
  F : text;
  data : array[1..100] of el;
  data_length : integer;
  i: integer;
  
procedure load;
begin
  Assign(F,'duom.txt');  Reset(F);
  readln(f, data_length);
  for i:=1 to data_length do
    begin
      Readln(F,
               data[i].id,
               data[i].lid,//lid
               data[i].rid,
               data[i].info);
    end;
  Close(F);
end;

procedure save;
begin
  Assign(F,'duom.txt');  Rewrite(F);
  writeln(f, data_length);
  for i:=1 to data_length do
    begin
      writeln(F,
               data[i].id, ' ',
               data[i].lid, ' ',    //lid
               data[i].rid, ' ',      //rid
               data[i].info);
    end;
  Close(F);
end;

procedure pildysim(i:integer); // append list with new question
var kl,s:string;
begin
  writeln('O koks tavo teisingas atsakymas ? '); // your answer is ????
  readln(s);
  writeln('Koks klausimas ? '); // Question ????
  readln(kl);
  data_length := data_length + 1;
  with data[data_length] do
    begin
      id := data_length;
      lid := 0;
      rid := 0;
      info := data[i].info;
      data[i].rid := data_length;   //lid
    end;
  data_length := data_length + 1;
  with data[data_length] do
    begin
      id := data_length;
      lid := 0;
      rid := 0;
      info := s;
      data[i].lid := data_length; //rid
    end;
  data[i].info := kl;
  
  save;
end;


procedure zaidimas; // game
var i:integer;
    c:char;
begin
 i:=1;
 writeln(data[i].lid, ' ', data[i].rid);   // lid rid
 while (data[i].lid <> 0) and  (data[i].rid <> 0) do
    begin
      writeln(data[i].info);
      writeln('Taip/Ne?');
      readln(c);
      if upcase(c) = 'T' then
         i := data[i].lid //rid  lid
       else
         i := data[i].rid; //lid    rid
   end;
 writeln('Spëju: ' +data[i].info); // Expert system guess
 writeln('Ar teisingai? Taip/Ne'); // Yes/No
 readln(c);
 if upcase(c) = 'N' then
    pildysim(i);
end;


begin
  load;
  zaidimas;
  readln;
end.

Data document where stored questions and answers list duom.txt. Expert system learns from it and appends this document with new questions and answers.

index represents 11 rows (remeber that list filling up when your expert system dosent know the right answer).

Index Yes No Question and Answer
1 2 3 Does the animal have a tail?
2 4 5 Does the animal flying?
3 0 0 Human
4 6 7 Does the animal feathered?
5 9 8 Does the animal gives milk?
6 0 0 Bird
7 0 0 Bat
8 11 10 Does the animal lives in jungle?
9 0 0 Cow
10 0 0 Dog
11 0 0 Lizard