Prolog :使用谓词“Name”按音节划分单词

发布于 2025-01-06 17:34:45 字数 1331 浏览 0 评论 0原文

我需要从用户那里读取一个单词,然后将其拆分为音节 基于 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).


*/

I need to read a word in from the user and then split it into syllables
based on one of 2 rules: vowel-consonant-vowel, or
vowel-consonant-consonant-vowel.

Looks that predicate "name" does not work, should be word in output, not list
Could you please help?

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).


*/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨与橙与城 2025-01-13 17:34:45

DCG在处理输入时更实用:

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.

DCG are more practical when handling input:

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").

test:

?- 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文