Mat*_*hee 5 parsing declarative prolog dcg
我设法为给定的句子构建了解析树,这里的句子是:"那个人回家了."
T = s(np(det(the), n(man)), vp(v(went), np(n(home))))
Run Code Online (Sandbox Code Playgroud)
1)如何使用短语/ 2?
如何使用prolog翻译逻辑语言中的句子? - 类似于我的需要,但它的解决方案对我不起作用.
2)我想用语法模式映射它并得到单词标签.
Det=the,N(Subject)=man,V=went,N(Object)=home
有没有办法用给定的树集结构映射这个树并识别语法.如何使用解析树来识别主语,动词,宾语,语法模式以及生成目标语言句子.
稍后编辑..我试过这个代码,它给出了相当大的答案.对此代码的任何建议.
sent("(s(np(n(man))) (vp(v(went)) (np(n(home)))))").
whitespace --> [X], { char_type(X, white) ; char_type(X, space) }, whitespace.
whitespace --> [].
char(C) --> [C], { char_type(C, graph), \+ memberchk(C, "()") }.
chars([C|Rest]) --> char(C), chars(Rest).
chars([C]) --> char(C).
term(T) --> chars(C), { atom_chars(T, C) }.
term(L) --> list(L).
list(T) --> "(", terms(T), ")".
terms([]) --> [].
terms([T|Terms]) --> term(T), whitespace, !, terms(Terms).
simplify([s,[np, [n,[Subject]]], [vp,[v,[Verb]],[np,[n,[Object]]]]],Result) :- Result = [Subject,Verb,Object].
Run Code Online (Sandbox Code Playgroud)
谢谢Mathee
更简单的方法是访问树,对您感兴趣的符号进行“硬编码”。
这是一个更通用的实用程序,它使用 ( =.. )/2 捕获树的命名部分:
part_of(T, S, R) :- T =.. [F|As],
( F = S,
R = T
; member(N, As),
part_of(N, S, R)
).
?- part_of(s(np(det(the), n(man)), vp(v(went), np(n(home)))),np,P).
P = np(det(the), n(man)) ;
P = np(n(home)) ;
false.
Run Code Online (Sandbox Code Playgroud)
它是一种member/2,仅适用于树。顺便说一句,我不明白你的问题的第一部分:为什么你想在语法树上使用phrase/2?通常语法(phrase/2 的第一个参数)旨在从“原始”字符流构建语法树......