开发者

Prolog Parsing Output

开发者 https://www.devze.com 2023-02-22 13:45 出处:网络
I\'m doing a piece of university coursework, and I\'m stuck with some Prolog. The coursework is to make a really rudimentary Watson (the machine that answers questions on Jeapoardy).

I'm doing a piece of university coursework, and I'm stuck with some Prolog.

The coursework is to make a really rudimentary Watson (the machine that answers questions on 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; i.e. [Tostig_godwinson, was, slain].

I much prefer programming in languages like C etc., so I'm a bit stuck. If this were a procedural 开发者_如何学Pythonlanguage, I'd use parsing tools, but Prolog doesn't have any... What do I need to do to extract those parts?

Thank you in advance


In Prolog, the language is the parsing tool. Use the univ (=..) operator to do term inspection:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号