Написать программу, которая бы искала введенное слово в тексте (текст в файле) по максимальному количеству совпадающих первых букв. Найденные слова сохранить в новом файле
DOMAINS
file = input ; output
strlist = string*
mul_strlist = strlist*
PREDICATES
start
% Read input file
% mul_string (o)
reading(mul_strlist)
% Create (multi) list of word
% string Line (i)
% strlist Old string (i)
% strlist New string (i)
% mul_strlist Temp string (o)
% mul_strlist Output string (o)
createlist(string, strlist, strlist, mul_strlist, mul_strlist)
% strlist List (i)
% mul_strlist Temp string (o)
% mul_strlist Output string (o)
reverselist(strlist, strlist, strlist)
% string Prefix (i)
% mul_strlist Words (i)
% strlist Output list (o)
searchin_list(string, strlist)
searchin_mullist(string, mul_strlist)
% string Prefix (i)
% string Word (i)
prefix(string, string)
saveword(string)
GOAL
clearwindow,
start.
CLAUSES
start:-
openread(input, "in.txt"),
readdevice(input),
openwrite(output, "out.txt"),
closefile(output),
openmodify(output, "out.txt"),
writedevice(output),
reading([]).
reading(Mlist):-
not(eof(input)),
readln(Line),
createlist(Line, [], New_list, MList, List2),
reading(List2).
% EOF
reading(Mlist):-
%write(Mlist),
readdevice(keyboard),
readln(Pattern),
searchin_mullist(Pattern, Mlist),
closefile(input),
closefile(output).
createlist(Line, Old_list, New_list, List1, List2):-
Line <> "",
% Sting, Token, Rest
fronttoken(Line, Token, Rest),
New_list = [Token | Old_list],
createlist(Rest, New_list, Blank_list, List1, List2).
% List was created -> reverse it
createlist(_, Old_list, [], List1, List2):-
reverselist(Old_list, [], New_list),
List2 = [New_list | List1].
reverselist([], Inputlist, Inputlist).
reverselist([Head | Tail], List1, List2):-
reverselist(Tail, [Head | List1], List2).
searchin_mullist(Pattern, []).
searchin_mullist(Pattern, [List | Rest]):-
searchin_list(Pattern, List), searchin_mullist(Pattern, Rest), filepos(output, 0, 0), not(eof(output));
eof(output), str_len(Pattern, L), LL = L - 1, L > 1,
frontstr(LL, Pattern, H, T), searchin_mullist(H, [List|Rest]).
searchin_list(Pattern, []).
searchin_list(Pattern, [Word|Rest]):-
prefix(Pattern, Word), searchin_list(Pattern, Rest);
searchin_list(Pattern, Rest).
prefix(Pattern, Word):-
str_len(Pattern, L), frontstr(L, Word, H, T), H = Pattern, saveword(Word).
saveword(Word):- write(Word, 'n').