2011-05-02 85 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

相關問題