Changeable words: Difference between revisions

Added Prolog Solution
m (→‎{{header|REXX}}: moved the reading and filtering the dictionary to a subroutine.)
(Added Prolog Solution)
Line 722:
underclassman <=> underclassmen
upperclassman <=> upperclassmen
</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
{{trans|Go}}
<lang prolog>:- dynamic dictionary_word/1.
 
main:-
File_name = 'unixdict.txt',
load_dictionary_from_file(File_name, 12),
print_changeable_words(File_name).
 
load_dictionary_from_file(File, Min_length):-
open(File, read, Stream),
retractall(dictionary_word(_)),
load_dictionary_from_stream(Stream, Min_length),
close(Stream).
 
load_dictionary_from_stream(Stream, Min_length):-
read_line_to_string(Stream, String),
String \= end_of_file,
!,
string_length(String, Length),
(Length >= Min_length ->
assertz(dictionary_word(String))
;
true),
load_dictionary_from_stream(Stream, Min_length).
load_dictionary_from_stream(_, _).
 
print_changeable_words(File_name):-
writef('Changeable words in %w:\n', [File_name]),
findall([Word1, Word2],
(dictionary_word(Word1),
dictionary_word(Word2),
Word1 \= Word2,
hamming_distance(Word1, Word2, 1)),
Words),
nth1(N, Words, [Word1, Word2]),
writef('%3r: %15l-> %w\n', [N, Word1, Word2]),
fail.
print_changeable_words(_).
 
hamming_distance(String1, String2, Dist):-
string_chars(String1, Chars1),
string_chars(String2, Chars2),
hamming_distance(Chars1, Chars2, Dist, 0).
 
hamming_distance([], [], Dist, Dist):-!.
hamming_distance([Ch|Chars1], [Ch|Chars2], Dist, Count):-
!,
hamming_distance(Chars1, Chars2, Dist, Count).
hamming_distance([_|Chars1], [_|Chars2], Dist, Count):-
Count1 is Count + 1,
Count1 < 2,
hamming_distance(Chars1, Chars2, Dist, Count1).</lang>
 
{{out}}
<pre>
Changeable words in unixdict.txt:
1: aristotelean -> aristotelian
2: aristotelian -> aristotelean
3: claustrophobia -> claustrophobic
4: claustrophobic -> claustrophobia
5: committeeman -> committeemen
6: committeemen -> committeeman
7: committeewoman -> committeewomen
8: committeewomen -> committeewoman
9: complementary -> complimentary
10: complimentary -> complementary
11: confirmation -> conformation
12: conformation -> confirmation
13: congresswoman -> congresswomen
14: congresswomen -> congresswoman
15: councilwoman -> councilwomen
16: councilwomen -> councilwoman
17: craftsperson -> draftsperson
18: draftsperson -> craftsperson
19: eavesdropped -> eavesdropper
20: eavesdropper -> eavesdropped
21: frontiersman -> frontiersmen
22: frontiersmen -> frontiersman
23: handicraftsman -> handicraftsmen
24: handicraftsmen -> handicraftsman
25: incommutable -> incomputable
26: incomputable -> incommutable
27: installation -> instillation
28: instillation -> installation
29: kaleidescope -> kaleidoscope
30: kaleidoscope -> kaleidescope
31: neuroanatomy -> neuroanotomy
32: neuroanotomy -> neuroanatomy
33: newspaperman -> newspapermen
34: newspapermen -> newspaperman
35: nonagenarian -> nonogenarian
36: nonogenarian -> nonagenarian
37: onomatopoeia -> onomatopoeic
38: onomatopoeic -> onomatopoeia
39: philanthrope -> philanthropy
40: philanthropy -> philanthrope
41: prescription -> proscription
42: proscription -> prescription
43: schizophrenia -> schizophrenic
44: schizophrenic -> schizophrenia
45: shakespearean -> shakespearian
46: shakespearian -> shakespearean
47: spectroscope -> spectroscopy
48: spectroscopy -> spectroscope
49: underclassman -> underclassmen
50: underclassmen -> underclassman
51: upperclassman -> upperclassmen
52: upperclassmen -> upperclassman
</pre>
 
1,777

edits