首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Prolog使用列表中的事实

Prolog使用列表中的事实
EN

Stack Overflow用户
提问于 2017-10-31 21:01:50
回答 1查看 125关注 0票数 0

我对swi-prolog中的列表结构有一些问题。

代码语言:javascript
复制
rlTopic([
    [
        [noun],                          % find match for this          
        [
            [ 
                grammar([noun],1),       % use one of this          
                grammar([det,noun],1)           
            ],
            [
                noun(fox,1),               % fill one of the grammars
                det(the,1),                % with these words
                noun(cat,1)
            ]
        ]
    ],
    [
        [det,adj,noun],                                 
        [
            [ 
                grammar([verb,adj],1),       % use one of this          
                grammar([det,noun],1)           
            ],
            [
                det(the,1),               % fill one of the grammars
                adj(quick,1),             % with these words
                noun(cat,1),
                verb(ran,1)
            ]
        ]
    ],
 ....

我尝试使用[noun] (从输入)查找匹配项。在此之后,获取下一级中的一个语法,并将它们与单词进行匹配。现在查找语法很好,但我在将单词插入到语法中时遇到了问题。

我现在的代码:

代码语言:javascript
复制
get_keyword(KeyList, [KeyList,_]).
get_response(RespList, [_, RespList]).

find_grammar([Grammars,Words],AtomGrammar):- 
            is_list(Grammars),
            find_grammar(Grammars,AtomGrammar),!;
            AtomGrammar = Grammars.

find_grammar(_,_).

find_match(Input, [FirstRecord|RestDatabase], ListOfResponse):-
    get_keyword(Keyword, FirstRecord),
    Keyword == Input, get_response(ListOfResponse, FirstRecord), !;
    find_match(Input, RestDatabase, ListOfResponse).

find_match(_, [_], _).

test(Input):-       
    rlTopic(ListOfRecord),
    find_match(Input, ListOfRecord, ListOfResponse), 
    find_grammar(ListOfResponse,G).

对于输入test([det,adj,noun]),输出应该是一种填充了单词的语法,比如run quick

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2017-10-31 22:39:20

我不能完全理解你想要做什么,我想是因为我们都被你创建的数据结构所阻碍。在我看来,您正在尝试做词性标记,然后解析自然语言句子,这是非常有趣的事情。这是我曾经修补过的东西,但有几本关于这个主题的好书,包括Ivan Bratko的著名著作Prolog Programming for are和Fernando Pereira的Prolog and Natural Language Analysis,该书鲜为人知,但非常有用。

你这里有很多列表结构,它们似乎在妨碍你,所以我现在就忽略它。您始终可以使用member/2使列表像存储中的事实一样工作。你的第二个问题是,我认为你想搜索noun/2det/2,可能还有其他词性,但当你抓住一个单词时,你不知道你想搜索哪一个。所以我建议你用part_of_speech(Word, PartOfSpeech)之类的东西来代替noun/2det/2等等。所以这就是我想要开始的数据库:

代码语言:javascript
复制
grammar([noun]).
grammar([det,noun]).

part_of_speech(fox, noun).
part_of_speech(the, det).
part_of_speech(cat, noun).

我删除了整数,因为我看不到它们的用途。在这里,很容易找到一个语法并将其填充到您的输入中:

代码语言:javascript
复制
match(Input, Grammar, Tagged) :-
    grammar(Grammar),
    maplist(match_word, Input, Grammar, Tagged).

match_word(Word, Part, Tag) :-
    part_of_speech(Word, Part),
    Tag =.. [Part, Word].

这会产生如下结果:

代码语言:javascript
复制
?- match([the,cat], G, T).
G = [det, noun],
T = [det(the), noun(cat)].

?- match([cat], G, T).
G = [noun],
T = [noun(cat)] ;
false.

?- match([the,cat], G, T).
G = [det, noun],
T = [det(the), noun(cat)].

?- match([the,fox], G, T).
G = [det, noun],
T = [det(the), noun(fox)].

我认为这就是您想要的,但我不能完全确定,因为我没有看到示例输入/输出。这是一个简化问题的简化解决方案;我认为如果解决方案在正确的范围内,那么应该可以将其提升到相当不方便的数据结构中。如果不是,那么我建议您考虑一种基于DCGs的方法,而不是像这样的规则/匹配方法;它可能会更干净、更直接。

词性标注在AI中绝对是一个有趣的话题,尤其是“经典”AI,而这种方法可能并不能很好地扩展。

此外,请查看WordNet;他们的数据库有一个Prolog版本,这可能会为您节省一些工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47036138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档