2011-05-02 78 views
1

我正在学习编程语言课程的学期末项目。分配如下。我正在用Java编写它,并且在编写Prolog时遇到了很多麻烦。我一直在使用Prolog遇到很多麻烦,所以这个问题同样需要寻求帮助,因为它试图更好地理解Prolog。任何帮助,我能得到将不胜感激将一个没有任何空格/分隔符的句子拆分为一个带空白的句子

一个句子中包含的话,出现在字典中的所有 ,这种情况发生 是没有白 空格作为分隔符连接起来。描述一个 解决方案,该解决方案产生所有可能的 答案,与给定的 字典兼容以下两种 3种语言:Java,Haskell,Prolog。 测试数据作为UTF-8文本 文件提供,该文件每行包含一个句子, ,所有单词出现在 字典中,作为UTF-8文本 文件提供,每行包含一个字。 输出应该是一个UTF-8文本文件 ,其中包含所有由空格分隔的单词 。 word文件的

例子:



树皮
运行

句文件的一个例子是

thedogbarks
thecatrunsaway

+0

你能问一个具体的问题吗?你试过什么了? – hammar 2011-05-02 02:05:47

+0

我不知道从哪里开始,说实话 – MeeksMan13 2011-05-02 02:50:02

+0

当单词是别人的前缀时,这是什么行为,即“the”和“there”都是单词? – 2011-05-02 03:48:48

回答

3

程序的核心应该是一个谓词,它标记字符代码列表,即从代码中构建原子列表(=字)。下面是一个概述:

%% tokenize(+Codes:list, -Atoms:list) 
% 
% Converts a list of character codes 
% into a list of atoms. There can be several solutions. 
tokenize([], []) :- !. 

tokenize(Cs, [A | As]) :- 
    % Use append/3 to extract the Prefix of the code list 
    append(...), 
    % Check if the prefix constitutes a word in the dictionary, 
    % and convert it into an atom. 
    is_word(Prefix, A), 
    % Parse the remaining codes 
    tokenize(...). 

现在,您可以定义:

is_word(Codes, Atom) :- 
    atom_codes(Atom, Codes), 
    word(Atom). 

word(the). 
word(there). 
word(review). 
word(view). 

split_words(Sentence, Words) :- 
    atom_codes(Sentence, Codes), 
    tokenize(Codes, Words). 

,并使用它像这样:

?- split_words('thereview', Ws). 
Ws = [the, review] ; 
Ws = [there, view] ; 
false. 

或更复杂的东西使用它,你解析文件获取输入并将结果输出到文件中。

+0

+1。如果OP需要额外的功劳,他们应该在Prolog中给出一个动态的编程解决方案;) – 2011-05-02 13:39:01

相关问题