本文介绍了Ruby中的随机句生成器:如何随机选择哈希中特定键的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个RSG的Ruby版本,并以某种方式停留在生成句子的过程中(...)

所以我设法实现了所有的函数,比如read ,转换为哈希...等。但问题是如何随机选择哈希值来生成句子?



现在我在这里有一个哈希值:

  hash = {< start>=> [[The,< object>,< verb> 
< object>=> [[waves],[big,yellow,flowers],[slugs]],
< verb> => [[sigh,<副词>,[portend,like,< object>]],[die,< adverb>]],
< adverb>=> [[warily],[grumpily]]}

目标是在散列中使用随机值组装字符串,例如当函数迭代到< object>时,它会将其作为然后随机选择相应的值(< object> => [> [[波浪],[大 黄色,花朵],[slugs]] )并组装它,然后输出如下所示:

 海浪今晚惊呼

到目前为止我的代码:

  hash.each do | _,value | 
value.map {| x | x.map {| Y | is_non_terminal?(y)? (puts value.values_at(SomethingToPutInto)):( puts y)}}
end



values_at 将导致TypeError:没有隐式转换String into Integer(TypeError)



对于 is_non_terminal?(y)函数来检查字符串是否包含< >


$ b $ ($<'&&'>')? true:false
end


解决方案

我们把它称为 generate

  def生成(键)

在密钥处读取散列并随机使用 sample

  words = @hash [key] .sample 

然后,对于每个单词,检查它是否是< key> 。如果是这样,请调用生成,否则保存:

  if (word.start_with?(<)&& word.end_with?(>))
generate(word)
else
@sentence<< word
end

放在一起:

  @hash = {< start>=> [[The,< object>,< verb>, ]],
< object>=> [[波浪],[大,黄色,花朵,[slugs]],
< verb> => [[sigh,<副词>,[portend,like,< object>]],[die,<副词> ]],
<副词>=> [[warily],[grumpily]]}

@sentence = []

def生成(key)
words = @hash [key] .sample
words.each do | word |
if(word.start_with?(<)&& word.end_with?(>))
generate(word)
else
@sentence << word
end
end
end

generate(< start>)
puts @ sentence.join()

注意我使用@ -variables使它们的作用域可以在方法内到达。



样本输出:大黄色的花朵今晚在叹息。

I'm working on a Ruby verison of RSG and somehow stuck on the sentence generating process (...)

so I managed to implement all functions like read, convert to hash...,etc. But problem is how to randomly pick values in hash to generate the sentence?

Now I have a hash here:

hash = {"<start>"=>[["The", "<object>", "<verb>", "tonight."]],
        "<object>"=>[["waves"], ["big", "yellow", "flowers"], ["slugs"]], 
        "<verb>"=>[["sigh", "<adverb>"], ["portend", "like", "<object>"],["die", "<adverb>"]], 
        "<adverb>"=>[["warily"], ["grumpily"]]}

The Objective is to assemble the strings using random values in the hash for example when the function iterates to "<object>", it will take it as a key and search in the hash to find matching key then randomly pick the corresponding values (one of the strings in "<object>"=>[["waves"], ["big", "yellow", "flowers"], ["slugs"]]) and assemble it, then the output would be something like this:

"The waves sigh warily tonight" 

My code so far:

hash.each do |_, value|
  value.map{|x| x.map{|y| is_non_terminal?(y) ? (puts value.values_at("SomethingToPutInto")) : (puts y)}}
end

Somehow the logics in the code becomes too complicated and I'm stuck on this step.. Using values_at will cause TypeError: no implicit conversion of String into Integer (TypeError)

For is_non_terminal?(y) is just a function to check whether the string contains < and >:

def is_non_terminal?(s)
   s.include?('<' && '>') ? true : false
end
解决方案

I assume they're looking for a recursive method, let's call it generate.

def generate(key)

Read the hash at the key and take one randomly using sample:

  words = @hash[key].sample

Then, for each word, check to see if it's a <key>. If so, call generate on it, otherwise save it:

    if (word.start_with?("<") && word.end_with?(">"))
      generate(word)
    else
      @sentence << word
    end

Putting it all together:

@hash = {"<start>"=>[["The", "<object>", "<verb>", "tonight."]],
        "<object>"=>[["waves"], ["big", "yellow", "flowers"], ["slugs"]], 
        "<verb>"=>[["sigh", "<adverb>"], ["portend", "like", "<object>"],["die", "<adverb>"]], 
        "<adverb>"=>[["warily"], ["grumpily"]]}

@sentence = []

def generate(key)
  words = @hash[key].sample
  words.each do |word|
    if (word.start_with?("<") && word.end_with?(">"))
      generate(word)
    else
      @sentence << word
    end
  end
end

generate("<start>")
puts @sentence.join(" ")

Notice I used @-variables to make their scope reachable from within the method.

Sample output: The big yellow flowers sigh grumpily tonight.

这篇关于Ruby中的随机句生成器:如何随机选择哈希中特定键的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:49