简体   繁体   English

Prolog解析输出

[英]Prolog Parsing Output

I'm doing a piece of university coursework, and I'm stuck with some Prolog. 我正在上大学的一部分课程,并且坚持使用Prolog。

The coursework is to make a really rudimentary Watson (the machine that answers questions on Jeapoardy). 课程的工作是制作一个非常基本的Watson(可以回答有关Jeapoardy的问题的机器)。

Anyway, I've managed to make it output the following: 无论如何,我设法使其输出以下内容:

noun_phrase(det(the),np2(adj(traitorous),np2(noun(tostig_godwinson)))),
verb_phrase(verb(was),np(noun(slain)))).

But the coursework specifies that I now need to extract the first and second noun, and the verb, to make a more concise sentence; 但是课程要求我现在需要提取第一个和第二个名词以及动词,以使句子更简洁。 ie [Tostig_godwinson, was, slain]. 即[Tostig_godwinson,被杀]。

I much prefer programming in languages like C etc., so I'm a bit stuck. 我更喜欢使用C等语言进行编程,因此有点卡住了。 If this were a procedural language, I'd use parsing tools, but Prolog doesn't have any... What do I need to do to extract those parts? 如果这是一种过程语言,我将使用解析工具,但Prolog没有任何...我需要怎么做才能提取那些部分?

Thank you in advance 先感谢您

In Prolog, the language is the parsing tool. 在Prolog中,语言是解析工具。 Use the univ ( =.. ) operator to do term inspection: 使用univ( =.. )运算符进行期限检查:

% find terminal nodes (words) in Tree
terminal(Tree, Type, Item) :-
    Tree =.. [Type, Item],
    atomic(Item).
terminal(Tree, Type, Item) :-
    Tree =.. [_, Sub],
    member(Node, Sub),
    terminal(Node, Type, Item).

Now get a list of all nouns with findall(N, terminal(Tree, noun, N), Nouns) and get the nth1 element. 现在获得带有findall(N, terminal(Tree, noun, N), Nouns) nth1 findall(N, terminal(Tree, noun, N), Nouns)的所有名词的列表,并获得nth1元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM