用于完成字符串的 Prolog 通配符 [英] Prolog wildcard for completing a string

查看:36
本文介绍了用于完成字符串的 Prolog 通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到了一个序言问题.

I am currently stuck on a prolog problem.

到目前为止我有:

film(Title) :- movie(Title,_,_). (其中 'movie(T,_,_,)' 是对我的数据库)

film(Title) :- movie(Title,_,_). (Where 'movie(T,_,_,)' is a reference to my database)

namesearch(Title, Firstword) :- film(Title), contains_term(Firstword, Title).

很难解释我需要什么帮助,但基本上是否有一个通配符可以用来搜索以特定单词开头的所有电影,例如,如果我要搜索所有以单词开头的电影"的".

It is hard to explain what I need help on, but basically is there a wildcard I can use to search for all films starting with a specific word, for example, if I were to search for all films beginning with the word "The".

是否有通配符允许我输入:namesearch(X,'The*')?

Is there a wildcard which would allow me to input as such: namesearch(X,'The*') ?

我试过像这样使用星号,但它不起作用,

I have tried using the asterisk like this and it does not work,

感谢您的帮助

推荐答案

这完全取决于标题的表示方式.

It all depends how the title is represented.

如果表示为原子,则需要sub_atom(Atom, Before, Length, After, Sub_atom)

If it is represented as an atom, you need sub_atom(Atom, Before, Length, After, Sub_atom)

?- Title = 'The Third Man', sub_atom(Title, 0, _, _, 'The').
Title = 'The Third Man'.

代码列表

如果它是一个代码列表,在爱丁堡传统的 Prologs 中称为字符串,您可以使用 append/3 对其进行硬编码",或者您可以使用 Definite Clause Grammars 表示一般模式.

List of codes

If it is a list of codes which is called a string in Prologs in Edinburgh tradition, you can either "hard code" it with append/3 or you might use Definite Clause Grammars for general patterns.

?- set_prolog_flag(double_quotes,codes).
true.

?- append("The",_, Pattern), Title = "The Third Man", Pattern = Title.
Pattern = Title, Title = [84, 104, 101, 32, 84, 104, 105, 114, 100|...].

?- Title = "The Third Man", phrase(("The",...), Title).
Title = [84, 104, 101, 32, 84, 104, 105, 114, 100|...] ;
false.

注意84是T等的字符代码

Note that 84 is the character code of T etc.

phrase/2 是语法的入口".有关更多信息,请参阅 .上面使用了以下定义:

phrase/2 is "the entry" to grammars. See dcg for more. Above used the following definition:

... --> [] | [_], ... .

字符列表

类似于代码列表,字符列表提供了一种更具可读性的表示,它仍然具有与列表谓词和定语从句语法兼容的优点:

List of characters

Similar to list of codes, list of characters provide a more readable representation that has still the advantages of being compatible with list predicates and Definite Clause Grammars:

?- set_prolog_flag(double_quotes,chars).
true.

?- append("The",_, Pattern), Title = "The Third Man", Pattern = Title.
Pattern = Title, Title = ['T', h, e, ' ', 'T', h, i, r, d|...].

?- Title = "The Third Man", phrase(("The",...), Title).
Title = ['T', h, e, ' ', 'T', h, i, r, d|...] ;
false.

另见这个答案.

这篇关于用于完成字符串的 Prolog 通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆