Метод анализа ключевых слов является простейшим методом
построения интерфейса на естественном языке. В качестве примера
рассмотрим программу работы с динамической базой знаний FOOTBALL.
Факты расположены в файле FOOTBALL.DBa и объявлены в разделе
database программы FOOTBALL. Факты определяются при помощи преди-
ката
playez(p_name, t_name, p_number, pos, height
weight,nfl_exp,college),
аргументы которого описывают соответственно имя игрока, назва-
ние команды, номер, позиция на поле, рост, вес игрока, стаж иг-
рока в лиге, источник финансирования.
Программа EX1 на языке ТП имеет следующий вид.
#M
domains
str_list = symbol*
str = string
p_name, t_name, pos, height, college = string
p_number, weight, nfl_exp = integer
database
player(p_name, t_name, p_number, pos,
height, weight, nfl_exp, college)
predicates
do_query
repeat
clear_database
convers(string, str_list)
first_element(str_list, string)
last_element(str_list, string)
search_player(string)
do_right_form(string, string)
keyword(string)
goal
do_query.
clauses
do_query :-
consult("football.dba"),
makewindow(1,$11,$11,"",0,0,25,80),
makewindow(2,$1E,$31,"",1,3,3,73),
cursor(0,8),
write("*************** PRO FOOTBALL DATABASE ***************"),
makewindow(3,$1E,$31," Input Info ",5,3,6,38),
repeat,
write(" Please type in your question."),
cursor(2,2), readln(Sentence),
makewindow(4,$1E,$31," List Maker ",5,43,6,33),
convers(Sentence, List),
first_element(List, Kname),
last_element(List, Lname),
do_right_form(Kname, Lname),
clear_database.
include "football.rul" #P
Правила, используемые при работе с базой знаний расположены
в файле FOOTBALL.RUL. Ниже приводится текст этого файла. Студен-
там предлагается самостоятельно разобраться в назначении и после-
довательности работы предикатов.
#M
predicates
access_database(string)
clauses
repeat.
repeat :- repeat.
clear_database :-
retract( player(_,_,_,_,_,_,_,_) ),
fail.
clear_database :-!.
convers(Str,[Head|Tail]):-
fronttoken(Str, Head, Str1),!,
convers(Str1, Tail).
convers(_,[]).
first_element([Head|_], Fname):-
Fname = Head.
last_element([Head], Last_element):-
Last_element=Head.
last_element([_|Tail], Last_element):-
last_element(Tail, Last_element).
access_database(Lname) :-
search_player(Lname).
search_player(Lname) :-
player(Name, Team, Number, Position,
Height, Weight, NFL_Exp, College),
Team_name = Name,
convers(Team_name, Dlist),
last_element(Dlist, Dname),
Lname = Dname,
write(" Available information:"),
nl, nl, write(" ", Name," is a player "),
write("on the ", Team," team."),
nl, write(" His number is ",Number),
write(" and his position is ",Position,"."),
nl, write(" His weight and height are ",Weight),
write(" lb. and ",Height," ft-inch."),
nl, write(" He has ",NFL_Exp,
" years of NFL experience."),
nl, write(" He is a graduate of ",College,"."),
nl, !.
search_player(_) :-
nl, nl,
write("That player is not in the database."),
nl.
do_right_form(Kname, Lname) :-
keyword(Kname),
write(" THE KEY WORDS ARE "), nl,
nl, write(" ",Kname," ",Lname),
makewindow(5,$1A,$31," Player Info ",12,3,12,73),
access_database(Lname),
nl, write(" ANY KEY CONTINUES."),
readchar(_),
removewindow,
gotowindow(4),
removewindow,
gotowindow(3),
clearwindow,!,
fail.
do_right_form(Kname,_) :-
Kname="Done",!,
exit.
do_right_form(Kname,_) :-
nl, write(" This keyword is unknown:"), nl,
nl, write(" ",Kname),
nl, write(" Press any key to try again."),
readchar(_),
removewindow,
clearwindow,!,
fail.
keyword(Key) :-
upper_lower(Upkey, Key),
Upkey = "SHOW",!.
keyword(Key) :-
upper_lower(Upkey, Key),
Upkey = "WHO",!.
keyword(Key) :-
upper_lower(Upkey, Key),
Upkey = "TELL",!. #P
|