2014-10-20 64 views
0

我想允許用戶使用Dinosaurus寶石查找單詞。我有這個部分,我有它,所以用戶輸入他們想要的句子和段落的數量。這是使用後期處理顯示的。但是,我希望能夠使用句點(".")來分隔每個單獨的句子,但我不知道在哪裏以及如何在後處理過程中編寫該句子。將句點標記插入到特定點的數組中

這是我的HTML文件中的代碼,帶有<form>標記。在表格I中,將變量@nsentences@nparagraphs指定爲每個段落的句子數量和用戶輸入的段落數量。

<form action = "/process" method="post"> 
    Topic: <input type="text" name ="word"></br> 
    Number of Sentences <input type="text" name="nsentences"></br> 
    Number of Paragraphs <input type="text" name="nparagraphs"></br> 
    <input type="submit" value="Submit"> 
</form> 

這是我的app.rb文件中的後期過程,它鏈接到我上面的HTML文件。在app.rb文件:

post '/process' do 
    @topic = params['word'] 
    @nsentences = params['nsentences'] 
    @nparagraph = params['nparagraphs'] 

    results = [] 
    results = Dinosaurus.lookup(@topic) 

    results2 = [] 
    results2 = Dinosaurus.lookup(results['noun']['syn'].sample) 

    y = @nsentences.to_i 
    z = @nparagraph.to_i 

    @nwords = [] 
    @sentences = [] 
    @content = [] 

    results['noun']['syn'].each do |word| 
    @nwords << word 
    end 

    results2['noun']['syn'].each do |word| 
    @nwords << word 
    end 

    y.times do 
    a_sentence = [] 

    15.times do 
     a_sentence << @nwords.sample 
    end 

這是我推的陣列,@sentences,這是一個空白的陣列將包含y的句子的用戶希望中的句子。

@sentences << a_sentence #.join('. ') #.to_s + "." 

    end 

    z.times do 
    @content << @sentences #.join('. ') #clean #.join(' ') #clean 
    end 

    @contentclean = @content.join(' ') 

    erb :some_file 

end 

在我的ERB文件中,我已將它設置爲顯示@contentclean

+0

我們需要看到樣品的輸入數據,以及輸出的例子。 – 2014-10-20 21:01:09

+0

你不需要告訴我們你是否是編程新手。只要做好你的研究,並向我們提出一個很好的問題,告訴我們你所嘗試的是什麼,然後向我們提供我們需要的輸入數據以及所需的輸出。 – 2014-10-20 21:08:24

回答

0

不要使用:

y.times do 
    ... 
end 

相反,句子添加到收藏使用(使用map)和join 「」:

(1..y).map{(1..15).map{@nwords.sample}.join}.join(". ")