-Q
/sol
domains
s=symbol
predicates
identify(s)
it_is(s)
confirm(s,s)
denied(s,s)
check_if(s,s)
remember(s,s,s)
guess1
database
db_confirm(s,s)
db_denied(s,s)
clauses
guess1:-identify(X),write("Your animal is a(n) ",X),nl.
identify(eagle):-it_is(bird),it_is(carnivorous),confirm(has,use_as_national_symbol),confirm(does,fly),!.
identify(octopus):-not(it_is(mammal)),it_is(carnivorous),confirm(does,swim),confirm(has,tentacles),!.
identify(unknwon).
it_is(mammal):-confirm(has,hair),!.
it_is(mammal):-confirm(does,give_milk),!.
it_is(carnivorous):-confirm(has,pointed_teeth),!.
it_is(carnivorous):-confirm(does,eat_meat),!.
it_is(ungulate):-it_is(mammal),confirm(has,hooves),confirm(does,chew_cud),!.
it_is(bird):-confirm(has,feathers),confirm(does,lay_egg),!.
confirm(X,Y):-db_confirm(X,Y),!.
confirm(X,Y):-not(db_denied(X,Y)),!,check_if(X,Y).
denied(X,Y):-db_denied(X,Y),!.
check_if(X,Y):-write("computer : ",X," it ",Y,"\n","user : "),readln(R),remember(X,Y,R).
remember(X,Y,yes):-asserta(db_confirm(X,Y)).
remember(X,Y,no):-asserta(db_denied(X,Y)),fail.
goal:guess1.
Q-
Sol//
domains
s=string
sl=s*
database
dbre
predicates
helpme
repeat
getclauses(sl)
makeans(sl)
recognize (sl,integer)
contains(s,sl)
clauses
helpme:-write("Speak up, what your problem?"),nl,
repeat ,
getclause ( L ) ,
makeans ( L ) ,
fail .
makeans ( L ) : -
recognize ( L , 1 ) , write ( " hello Sir ? " ) , nl , ! .
makeans ( L ) :-
recognize ( L , 2 ) , write ( " yes Totally " ) , nl ,
makeans ( L ) :-
recognize ( L , 3 ) , write ( " my job is to help people . " ) , nl ,
makeans ( L ) :-
recognize ( L , 4 ) , write ( " thank you Sir . " ) , nl , !.
makeans ( L ) :-
recognize ( L , 5 ) , write ( " tell me more . " ) , nl , ! .
recognize ( L , 1 ) : - contains ( [ hello ] , L ) .
recognize ( L , 2 ) : - contains ( [ ready , exam ] , L ) .
recognize ( L , 3 ) : - contains ( [ job ] , L ) .
recognize ( L , 4 ) : - contains ( [ hope success ] , L recognize 5 ) . contains ( [ HT ] , L ) : - member ( H , L ) , contains ( [ IT ] , L ) : - contains ( T , L ) member ( H , [ HD ) : - !. member ( X , [ T ] ) : - member ( X , T ) .
goal.
helpme.
Q-
Sol//
domains
s=string
sl=s*
database
dbre
predicates
helpme
repeat
getclauses(sl)
makeans(sl)
recognize (sl,integer)
contains(s,sl)
next_to(s,s,sl)
convert(s,sl)
clauses
helpme:-write("Speak up, what your problem?"),nl,
repeat,
getclauses(L),
makeans(L),
fail.
repeat.
repeat:-repeat.
getclauses(L):-readln(S),
convert(S,L).
convert("",[ ]):-!.
convert(S,[H|T]):-fronttoken(S,H,R),
convert(R,T).
clauses
makeans(L):-recognize(L,1),
write("give me a suitable idea for hand question?"),nl,!.
makeans(L):-recognize(L,2),
write("what are you playing"),nl,!.
makeans(L):-recognize(L,3),
write("you must think before answer"),nl,!.
makeans(L):-recognize(L,4),
write("erlear your mention your playing"),nl,!.
makeans(L):-recognize(L,5),
write("Tell me more"),nl,!.
recognize(L,1):-next_to("suitable","idea",L).
recognize(L,2):-contains("playing",L),assert(dbre).
recognize(L,3):-contains("think",L);contains("belive",L).
recognize(_,4):-dbre.
recognize(_,5).
next_to(F,S,[F,S|_]):-!.
next_to(F,S,[_|T]):-next_to(F,S,T).
contains(E,[E|_]):-!.
contains(E,[_|T]):-contains(E,T).
goal
helpme.
Q: Consider the following pattern recognition system:
User Machine
… lonely … what about love?
… freed or sad … please do not use words like that.
… I hope … Tell me your hopes.
Write a complete Visual Prolog program using makeans and recognize
predicates that corresponds the above system. Make sure to return to the
third question or write "Tell me more" when no matching is found.
Sol//
domains
s=string
sl=s*
database
hope
predicates
helpme
repeat
getclauses(sl)
convert(s,sl)
makeans(sl)
recognize (sl,integer)
next_to(s,s,sl)
contains(s,sl)
clauses
helpme:-write("Speak up, what your problem?"),nl,
repeat,
getclauses(L),
makeans(L),
fail.
repeat.
repeat:-repeat.
getclauses(L):-readln(S),
convert(S,L).
convert("",[ ]):-!.
convert(S,[H|T]):-fronttoken(S,H,R),
convert(R,T).
makeans(L):-recognize(L,1),
write("what about love?"),nl,!.
makeans(L):-recognize(L,2),
write("please do not use words like that."),nl,!.
makeans(L):-recognize(L,3),
write("Tell me your hopes."),nl,!.
makeans(L):-recognize(L,4),
write("Earlier you mentioned your hopes!"),nl,!.
makeans(L):-recognize(L,5),
write("Tell me more."),nl,!.
recognize(L,1):- contains("lonely",L).
recognize(L,2):- contains("freed",L) ; contains("sad",L).
recognize(L,3):- next_to("I","hope",L), assert(hope).
recognize(_,4):-hope.
recognize(_,5).
next_to(F,S,[F,S|_]):-!.
next_to(F,S,[_|T]):-next_to(F,S,T).
contains(E,[E|_]):-!.
contains(E,[_|T]):-contains(E,T).
goal
helpme.
Q-
domains
s=string
predicates
conc(s,s,s)
clauses
conc(A,B,R ) : -fronttoken(R,A,B).
goal : conc("croco","dile",R).
Q-
Sol//
Predicates
strcount(string,integer).
Clauses
strcount("",0):-!.
strcount(X,Y):-fronttoken(X,Z,T),searchstring(Z,"ing",_),!,strcount(T,Y1),Y=Y1+1.
strcount(X,Y):-fronttoken(X,_,T),strcount(T,Y).
Goal:strcount("I am splitting a string",R).
STRING IN PROLOG
1. str_len (String,Length) (string,integer) (i,o): Determines the length of
String. Succeeds if the length could be matched with Len.
str_len(“prolog”,X)
X=6.
Str_len("ab",3) no
Str_len(X,3) X="---"
2. str_char (string,char) (i,o) (o,i): Converts a string (of one char) into a
character or vice versa. If string is bound, it must have length 1 for the
predicate to succeed.
str_char(“A”,X)
X=‟A‟.
3. str_int (string,integer) (i,o) (o,i): Converts a string of one character to
ASCII code or vice versa. If string is bound, it must contain a single
number for the predicate to succeed.
str_int(“A”,X)
X=65 .
Str_int(X,65)
X=”A”
Str_int(“33”,X)
X=33
Str_int(X,33)
X=”33”
4. str_real (string,real) (i,o) (o,i): Converts the string (of real) to real
and the opposit
Str_real("0.5",X)
X=0.5
Str_real(X,0.5)
X="0.5"
Str_real(“5”,X)
X=5.0
5. char_int (char,integer) (i,o) (o,i): Converts a character to ASCII code
or vice versa.
char_int(„A‟,X)
X=65.
6. isname (string) (i): Tests whether a string would match a prolog
symbol in other words test if the content of the string is name or not.
isname(“s2”) return YES.
isname(“4r”) return NO.
Isname("abc") yes
Isname("123"). No
7. frontchar (String,FrontChar,RestString) (string,char,string) (i,o,o)
(o,i,i): Extracts the first character from a string, the remainder is matched
with RestString.
frontchar (“prolog”,C,R)
C=‟p‟, R=”rolog”.
8. fronttoken (String,Token,Rest) (string,string,string) (I,o,o)(i,o,i) (i,i,o)
(o,i,i): Skips all white space characters (blanks,tabs) and separates from
the resulting string the first valid token. The remainder is matched with
RestString. A valid token is either a variable or name 'A'...'Z','a'...'z','
','0'...'9', a number '0'...'9' or a single character. It fails if String was
empty or contained only whitespace.
fronttoken (“complete prolog program”,T,R)
T=”complete”, R=”prolog program”.
9. frontstr (StrLen,String,FrontStr,RestStr) (i,i,o,o): Extracts the first n
characters from a string. This establishes a relation between String,
Count, FronStr, and RestString, thus that String = FrontStr+RestString
and str_len(FronStr,Count) is true. The String and Count arguments must
be initialized.
frontstr(3,”cdab 2000”,T,R)
T=”cda”, R=”b 2000”.
10. concat (Str1,Str2,ResStr) (string,string,string) (i,i,o): Merges to
strings to one by appending the second argument to the first.
concat (“prolog”,”2011”,R)
R=”prolog2011”.
11. Upper_lower(string,string)
Convert the string in upper case(in capital letter) to the lower
case (small letter) and the opposite.
upper_lower(capital_letter,small_letter)
upper_lower("ABC",X)
X="abc"
upper_lower("Abc",X)
X="abc"
upper_lower(X,"abc")
X="ABC"