我需要从用户处读取一个单词,然后将其拆分为音节 基于2个规则之一:元音 - 辅音 - 元音,或 元音,辅音,辅音,元音。
看起来谓词“name”不起作用,应该是输出中的单词,而不是列表
你能帮忙吗?
vowel(a).
vowel(e).
vowel(i).
vowel(o).
vowel(u).
vowel(y).
consonant(L) :- not(vowel(L)).
ssplit(A,B) :- atom_chars(A,K),ssplit(K,B,-1). %convert atom to list
test(A,B) :- append(A,[],F), name(N,F).
ssplit([],[],0) :- append(L,[],F), name(N,F), writeln(N).
ssplit([H1|T1],[H1|T2],-1) :- ssplit(T1,T2,0).
ssplit([H1|T1],[H1|T2],0) :- consonant(H1), ssplit(T1,T2,0). %split to syllables
ssplit([H1|T1],[H1|T2],0) :- vowel(H1), ssplit(T1,T2,1).
ssplit([H1|T1],[H1|T2],1) :- vowel(H1), ssplit(T1,T2,1). %split to syllables
ssplit([H1|[]],[H1|T2],1) :- consonant(H1), ssplit([],T2,0).
ssplit([H1,H2|[]],[H1,H2|T2],1) :- consonant(H1), vowel(H2), ssplit([],T2,1).
ssplit([H1,H2|T1],['-',H1,H2|T2],1) :- consonant(H1), vowel(H2), ssplit(T1,T2,1).
ssplit([H1,H2|T1],T2,1) :- consonant(H1), consonant(H2), ssplit([H1,H2|T1],T2,2).
ssplit([H1,H2|[]],[H1,H2|T2],2) :- ssplit([],T2,0). %split to syllables
ssplit([H1,H2,H3|[]],[H1,H2,H3|T2],2) :- vowel(H3), ssplit([],T2,1).
ssplit([H1,H2,H3|T1],[H1,'-',H2,H3|T2],2) :- vowel(H3), ssplit(T1,T2,1).
ssplit([H1,H2,H3|T1],[H1,H2,H3|T2],2) :- consonant(H3), ssplit(T1,T2,0).
/*
ssplit(analog,L).
ssplit(ruler,L).
ssplit(prolog,L).
*/
答案 0 :(得分:3)
split_name(N, L) :-
atom_codes(N, Cs),
phrase(split_v(L), Cs, []).
split_v([]) --> [].
split_v([S|Syllables]) -->
vowel(X),
consonant(Y),
vowel(Z),
{atom_codes(S, [X,Y,Z])},
split_v(Syllables).
split_v([S|Syllables]) -->
vowel(V1),
consonant(C1),
consonant(C2),
vowel(V2),
{atom_codes(S, [V1,C1,C2,V2])},
split_v(Syllables).
% catch all unhandled
split_v([S|Syllables]) -->
[C], {atom_codes(S, [C])},
split_v(Syllables).
vowel(C) --> [C], {vowel(C)}.
consonant(C) --> [C], {\+vowel(C)}.
vowel(C) :- memberchk(C, "aeiou").
试验:
?- split_name(stackoverflow,L).
L = [s, t, acko, v, e, r, f, l, o|...] ;
L = [s, t, a, c, k, ove, r, f, l|...] ;
L = [s, t, a, c, k, o, v, e, r|...] ;
false.