Prolog :使用谓词“Name”按音节划分单词
我需要从用户那里读取一个单词,然后将其拆分为音节 基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DCG在处理输入时更实用:
测试:
DCG are more practical when handling input:
test: