Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Prolog使用列表中的事实_Prolog - Fatal编程技术网

Prolog使用列表中的事实

Prolog使用列表中的事实,prolog,Prolog,我对swi prolog中的列表结构有一些问题 rlTopic([ [ [noun], % find match for this [ [ grammar([noun],1), % use one of this grammar([det,noun],1)

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

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)
            ]
        ]
    ],
 ....
我试着用
[名词]
查找匹配项(从输入)。在此之后,获得下一级别的语法之一,并将其与单词匹配。现在找到语法很好,但是我在语法中插入单词时遇到了问题

我现在的代码:

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).
对于输入
测试([det,adj,noun])
,输出应该是填充单词的语法之一,比如
快速运行


提前谢谢

我不完全确定我是否理解您在这里试图做什么,我想是因为我们都被您创建的数据结构所阻碍。在我看来,你正在尝试做词性标注,然后解析自然语言句子,这是非常有趣的事情。这是我只修改过的东西,但是有几本关于这个主题的好书,包括Ivan Bratko的著名著作Prolog Programming for Artificial Intelligence和Fernando Pereira的不太出名但非常有用的Prolog and Natural Language Analysis

这里有很多列表结构,它们似乎在妨碍我们,所以我现在就忽略它。您可以使用
member/2
使列表始终像商店中的事实一样工作。你的第二个问题是,我认为你想搜索
noun/2
det/2
以及可能的其他词类,但当你抓住一个词时,你不知道你想搜索哪一个。因此,我建议您将
名词/2
det/2
等替换为类似
词性部分(单词,词性部分)
。这就是我首先要介绍的数据库:

grammar([noun]).
grammar([det,noun]).

part_of_speech(fox, noun).
part_of_speech(the, det).
part_of_speech(cat, noun).
我删除整数是因为我不知道它们的用途是什么。从这里,很容易找到语法并用您的输入填写:

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].
这将产生如下结果:

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

词性标记在人工智能中无疑是一个有趣的话题,尤其是“经典”人工智能,而且这种方法可能无法很好地扩展


还有,退房;他们的数据库有一个Prolog版本,这可能会为您节省一些工作。

我不完全确定我是否理解您在这里试图做什么,我想是因为我们都被您创建的数据结构所阻碍。在我看来,你正在尝试做词性标注,然后解析自然语言句子,这是非常有趣的事情。这是我只修改过的东西,但是有几本关于这个主题的好书,包括Ivan Bratko的著名著作Prolog Programming for Artificial Intelligence和Fernando Pereira的不太出名但非常有用的Prolog and Natural Language Analysis

这里有很多列表结构,它们似乎在妨碍我们,所以我现在就忽略它。您可以使用
member/2
使列表始终像商店中的事实一样工作。你的第二个问题是,我认为你想搜索
noun/2
det/2
以及可能的其他词类,但当你抓住一个词时,你不知道你想搜索哪一个。因此,我建议您将
名词/2
det/2
等替换为类似
词性部分(单词,词性部分)
。这就是我首先要介绍的数据库:

grammar([noun]).
grammar([det,noun]).

part_of_speech(fox, noun).
part_of_speech(the, det).
part_of_speech(cat, noun).
我删除整数是因为我不知道它们的用途是什么。从这里,很容易找到语法并用您的输入填写:

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].
这将产生如下结果:

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

词性标记在人工智能中无疑是一个有趣的话题,尤其是“经典”人工智能,而且这种方法可能无法很好地扩展


还有,退房;他们的数据库有一个Prolog版本,这可能会为您节省一些工作。

您在查询中提供了什么类型的
输入?只是
[名词]
?另外,
G
test(Input)
子句中的单例。这是故意的吗?输入是一个列表。e、 g.
测试([名词])
测试([det,名词,…])
。我打算在下一个子句中使用
G
。我的意思是明确的。我知道这是一张单子。当您运行示例查询时,您给出了什么?你期望得到什么结果?你得到了什么结果?当你进入
测试([名词])时,你期望得到什么结果。
?啊,对不起。例如:
测试([det,noun,adj])
,输出结果应该类似于
quick fox
。如果列表
[det,adj,noun]
在结构中,您确定不想使用吗?您在查询中提供了什么类型的
输入?只是
[名词]
?另外,
G
test(Input)
子句中的单例。这是故意的吗?输入是一个列表。e、 g.
测试([名词])
测试([det,名词,…])
。我打算在下一个子句中使用
G
。我的意思是明确的。我知道这是一张单子。当您运行示例查询时,您做了什么