📄 geobase.inc
字号:
/*************************************************************************
SUPPORT PREDICATES - These are the clauses which support the
general system, including the parser and the menu system. Most of
the clauses involve list processing and are general enough to be
used in any system.
*************************************************************************/
PREDICATES
write_list(INTEGER,STRINGLIST) /* Write the list separated by spaces */
write_list2(STRINGLIST) /* Display an answer */
append(STRINGLIST,STRINGLIST,STRINGLIST) /* Append two lists */
unik(STRINGLIST,STRINGLIST) /* Eliminate duplicates in a list */
index(STRINGLIST,INTEGER,STRING) /* Select an element from a list */
CLAUSES
index([X|_],1,X):- !.
index([_|L],N,X):- N>1,N1=N-1,index(L,N1,X).
unik([],[]).
unik([H|T],L):-member(H,T),!,unik(T,L).
unik([H|T],[H|L]):-unik(T,L).
append([],L,L).
append([Ah|At],B,[Ah|C]):-append(At,B,C).
write_list(_,[]).
write_list(_,[X]):-!,write(X).
write_list(4,[H|T]):-!,write(H),nl,write_list(0,T).
write_list(3,[H|T]):-str_len(H,LEN),LEN>13,!,write(H),nl,write_list(0,T).
write_list(N,[H|T]):-str_len(H,LEN),LEN>13,!,N1=N+2,writef("%-27 ",H),write_list(N1,T).
write_list(N,[H|T]):-N1=N+1,writef("%-13 ",H),write_list(N1,T).
write_list2([]).
write_list2([H|T]):-write(H,' '),write_list2(T).
/*************************************************************************
Evaluating queries - This is the mechanism which reads a query, scans
the string and removes punctuation, parses the query and evaluates
it. The number of solutions are also reported here.
*************************************************************************/
DOMAINS
/* Nine types of questions are recognized by the evaluator */
QUERY = q_e(ENT) ;
q_eaec(ENT,ASSOC,ENT,STRING) ;
q_eaq(ENT,ASSOC,ENT,QUERY) ;
q_sel(ENT,RELOP,ENT,REAL);
q_min(ENT,QUERY);
q_max(ENT,QUERY);
q_not(ENT,QUERY) ;
q_or(QUERY,QUERY) ;
q_and(QUERY,QUERY)
PREDICATES
/* Input-output */
loop(STRING) /* Main loop */
readquery(STRING)
write_unit(STRING) /* Write the unit for an entity */
write_solutions(INTEGER) /* Write the number of solutions */
/* Scanner */
scan(STRING,STRINGLIST) /* Convert a string to a list of words */
filter(STRINGLIST,STRINGLIST) /* Eliminate commas and periods */
/* Parser */
pars(STRINGLIST,STRING,QUERY)
/* Evaluation */
eval(QUERY,STRING)
CLAUSES
loop(STR):- STR >< "",
scan(STR,LIST), /* Returns a list of words(symbols) */
filter(LIST,LIST1), /* Removes punctuation and words to be ignored*/
pars(LIST1,E,Q), /* Parses queries */
findall(A,eval(Q,A),L),
unik(L,L1),
write_list(0,L1),
write_unit(E),
listlen(L1,N),
write_solutions(N),
fail.
loop(STR):- STR >< "",readquery(L),loop(L).
readquery(QUERY):-nl,nl,write("Query: "),readln(QUERY).
scan(STR,[TOK|LIST]):-
fronttoken(STR,SYMB,STR1),!,
upper_lower(SYMB,TOK),
scan(STR1,LIST).
scan(_,[]).
filter(["."|T],L):- !,filter(T,L).
filter([","|T],L):- !,filter(T,L).
filter(["?"|T],L):- !,filter(T,L).
filter([H|T],L):- ignore(H),!,filter(T,L).
filter([H|T],[H|L]):- filter(T,L).
filter([],[]).
write_unit(E):-unit(E,UNIT),!,write(' ',UNIT).
write_unit(_).
write_solutions(0):-!,write("\nNo solutions").
write_solutions(1):-!.
write_solutions(N):-!,writef("\n% Solutions",N).
/*************************************************************************
ENTITY NAMES
*************************************************************************/
PREDICATES
entn(STRING,STRING) /* Convert an entity to singular form */
entity(STRING) /* Get all entities */
ent_synonym(STRING,STRING) /* Synonyms for entities */
ent_name(STRING,STRING) /* Convert between an entity
name and an internal entity name */
CLAUSES
ent_synonym(E,ENT):-synonym(E,ENT).
ent_synonym(E,E).
ent_name(ENT,NAVN):-entn(E,NAVN),ent_synonym(E,ENT),entity(ENT).
entn(E,N):-concat(E,"s",N).
entn(E,N):-free(E),bound(N),concat(X,"ies",N),concat(X,"y",E).
entn(E,E).
entity(name):-!.
entity(continent):-!.
entity(X):-schema(X,_,_).
/*************************************************************************
ERROR DETECTION -
Once the string has been converted to a list of words, the word
list can be checked against the language database to see if it
is a known word. Words which are not known are collected into a
list which the system reports on.
*************************************************************************/
PREDICATES
error(STRINGLIST)
known_word(STRING)
CLAUSES
error(LIST):- write(">> "),member(Y,LIST),not(known_word(Y)),!,
write("Unknown word: ",Y),nl.
error(_):- write("Sorry, the sentence can't be recognized").
known_word(X):-str_real(X,_),!. /* Check for special case words */
known_word("and"):-!.
known_word("or"):-!.
known_word("not"):-!.
known_word("all"):-!.
known_word("thousand"):-!.
known_word("million"):-!.
known_word(X):-minn(X),!. /* If not a special case word, check the */
known_word(X):-maxx(X),!. /* dynamic database for known words */
known_word(X):-size(_,X),!. /* additional words. */
known_word(X):-ignore(X),!.
known_word(X):-unit(_,X),!.
known_word(X):-assoc(_,AL),member(X,AL),!.
known_word(X):-ent_name(_,X),!.
known_word(X):-entity(X),!.
known_word(X):-relop(L,_),member(X,L),!.
known_word(X):-entity(E),not(unit(E,_)),ent(E,X).
/*************************************************************************
PARSER
*************************************************************************/
/*
PARSER SUPPORT - Compound entities:
This is used by the parser to handle a compound entity (e.g.
New York).
*/
PREDICATES
check(STRINGLIST) /* Check that the list is empty */
get_ent(STRINGLIST,STRINGLIST,STRING) /* Get the compound entity */
get_cmpent(STRINGLIST,STRINGLIST,STRING,STRING) /* Get the first component */
ent_end(STRINGLIST) /* Get the rest of the entity */
CLAUSES
check([]).
get_ent([E|S],S,E):-ent_end(S),!.
get_ent(S1,S2,ENT):-get_cmpent(S1,S2," ",E1),frontchar(E1,_,E),ENT=E.
get_cmpent([E|S],S,IND,ENT):-ent_end(S),concat(IND,E,ENT).
get_cmpent([E|S1],S2,IND,ENT):-
concat(IND,E,II),concat(II," ",III),
get_cmpent(S1,S2,III,ENT).
ent_end([]).
ent_end(["and"|_]).
ent_end(["or"|_]).
/*
Here begins the parser. The first two parameters for the parsing
predicates are the inputlist and what remains of the list
after a part of a query is stripped off. In the last parameter, a
structure for the query is built up.
This method is called "parsing by difference lists." Once you
understand how it works, you can easily add new sentence
constructions to the language.
*/
PREDICATES
s_rel(STRINGLIST,STRINGLIST,STRING)
s_unit(STRINGLIST,STRINGLIST,STRING)
s_val(STRINGLIST,STRINGLIST,REAL)
CLAUSES
s_rel(S1,S2,REL):-relop(RLIST,REL),append(RLIST,S2,S1).
s_unit([UNIT|S],S,UNIT).
s_val([X,thousand|S],S,VAL):- !,str_real(X,XX),VAL=1000*XX.
s_val([X,million|S],S,VAL):- !,str_real(X,XX),VAL=1000000*XX.
s_val([X|S],S,VAL):- str_real(X,VAL).
PREDICATES
s_attr(STRINGLIST,STRINGLIST,STRING,QUERY)
s_minmax(STRINGLIST,STRINGLIST,STRING,QUERY)
s_rest(STRINGLIST,STRINGLIST,STRING,QUERY)
s_or(STRINGLIST,STRINGLIST,STRING,QUERY)
s_or1(STRINGLIST,STRINGLIST,STRING,QUERY,QUERY)
s_and(STRINGLIST,STRINGLIST,STRING,QUERY)
s_and1(STRINGLIST,STRINGLIST,STRING,QUERY,QUERY)
s_elem(STRINGLIST,STRINGLIST,STRING,QUERY)
s_assoc(STRINGLIST,STRINGLIST,STRING,QUERY)
s_assoc1(STRINGLIST,STRINGLIST,STRING,STRING,QUERY)
s_nest(STRINGLIST,STRINGLIST,STRING,QUERY)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -