How to resolve the algorithm Abbreviations, automatic step by step in the Prolog programming language
How to resolve the algorithm Abbreviations, automatic step by step in the Prolog programming language
Table of Contents
Problem Statement
The use of abbreviations (also sometimes called synonyms, nicknames, AKAs, or aliases) can be an easy way to add flexibility when specifying or using commands, sub─commands, options, etc.
It would make a list of words easier to maintain (as words are added, changed, and/or deleted) if the minimum abbreviation length of that list could be automatically (programmatically) determined.
For this task, use the list (below) of the days-of-the-week names that are expressed in about a hundred languages (note that there is a blank line in the list). Caveat: The list (above) most surely contains errors (or, at the least, differences) of what the actual (or true) names for the days-of-the-week.
To make this Rosetta Code task page as small as possible, if processing the complete list, read the days-of-the-week from a file (that is created from the above list).
Notes concerning the above list of words
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Abbreviations, automatic step by step in the Prolog programming language
Source code in the prolog programming language
minimum_abbreviation_length(Day_names, Min_length):-
sort(Day_names, Sorted_names),
minimum_abbreviation_length(Sorted_names, Min_length, 1).
minimum_abbreviation_length([_], Min_length, Min_length):- !.
minimum_abbreviation_length([Name1, Name2|Rest], Min_length, M1):-
common_prefix_length(Name1, Name2, Length),
M2 is max(M1, Length + 1),
minimum_abbreviation_length([Name2|Rest], Min_length, M2).
common_prefix_length(String1, String2, Length):-
string_chars(String1, Chars1),
string_chars(String2, Chars2),
common_prefix_length1(Chars1, Chars2, Length, 0).
common_prefix_length1([], _, Length, Length):-!.
common_prefix_length1(_, [], Length, Length):-!.
common_prefix_length1([C1|_], [C2|_], Length, Length):-
C1 \= C2,
!.
common_prefix_length1([C|Chars1], [C|Chars2], Length, L1):-
L2 is L1 + 1,
common_prefix_length1(Chars1, Chars2, Length, L2).
to_upper_case([], []):-!.
to_upper_case([String|S], [Upper_case|U]):-
string_upper(String, Upper_case),
to_upper_case(S, U).
process_line(""):-
nl,
!.
process_line(Line):-
split_string(Line, "\s\t", "\s\t", Day_names),
to_upper_case(Day_names, Upper),
minimum_abbreviation_length(Upper, Length),
writef('%w %w\n', [Length, Line]).
process_stream(Stream):-
read_line_to_string(Stream, String),
String \= end_of_file,
!,
process_line(String),
process_stream(Stream).
process_stream(_).
process_file(File):-
open(File, read, Stream),
process_stream(Stream),
close(Stream).
main:-
process_file("days_of_week.txt").
You may also check:How to resolve the algorithm Execute Computer/Zero step by step in the RPL programming language
You may also check:How to resolve the algorithm Random numbers step by step in the Pascal programming language
You may also check:How to resolve the algorithm Ramanujan's constant step by step in the Perl programming language
You may also check:How to resolve the algorithm Execute HQ9+ step by step in the Sidef programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Objeck programming language