将句点标记插入到特定点的数组中

问题描述:

我想允许用户使用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

不要使用:

y.times do 
    ... 
end 

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

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