问题描述
我正在为编程语言课程开展学期末项目.任务如下.我正在用 Java 编写它,但在 Prolog 中编写时遇到了很多麻烦.我在使用 Prolog 时遇到了很多麻烦,所以这个问题既是在寻找作业的帮助,也是在试图更多地了解 Prolog.非常感谢我能得到的任何帮助
I'm working on an end-of-semester project for a Programming languages course. The assignment is given below. I'm finishing writing it in Java and I'm having a lot of trouble writing in Prolog. I've been having a lot of trouble with Prolog so this question is as much looking for help with the assignment as it is trying to understand Prolog more. Any help that I can get would be GREATLY appreciated
一个句子包含单词,所有出现在字典中,那会发生没有白色的连接空格作为分隔符.描述一个产生所有可能的解决方案答案,与给定的兼容字典中的以下 2 个3 种语言:Java、Haskell、Prolog.这测试数据以 UTF-8 文本形式提供每行包含一个句子的文件,所有单词出现在字典,以 UTF-8 文本形式提供文件每行一个字.这输出应该是一个 UTF-8 文本文件包含所有的句子用空格分隔的单词.
word 文件示例:
猫
狗
树皮
运行
离开
cat
dog
barks
runs
the
away
一个句子文件的例子是
狗吠
猫逃跑
推荐答案
你的程序的核心应该是一个谓词,它标记一个字符代码列表,即从代码中构建一个原子(=单词)列表.大纲如下:
The core of your program should be a predicate that tokenizes a list of character codes, i.e. builds a list of atoms (= words) out of the codes. Below is an outline:
%% 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.
或者在更复杂的情况下使用它,您解析文件以获取输入并将结果输出到文件中.
or use it in something more complex where you parse a file to obtain the input and output the results into a file.
这篇关于将没有任何空格/分隔符的句子拆分为带有空格的句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!