Creating a line from a map

I am currently working on a project where, using the rules of free grammar context, I will generate random sentences. Right now, I'm working on creating a function that will take a hash, and traversing it, choosing the right settings, to create a sentence and return it as a string.

For example, given a hash of the following format:

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

I should be able to generate a random sentence, such as: "The waves today are feverishly dying."

Here is the general process for generating this sentence:

  • He will always start generating in the tag <start>and continue filling out the required productions.
  • It goes through <start>and adds "The", then presses " <object>", so it goes into the key <object>and extracts a random value from it, for example ["waves"].
  • Then it comes back and continues to move and gets to " <verb>", so it goes into the key <verb>and extracts a random value from it, for example ["die", "<adverb>"].
  • Since he encountered " <adverb>", he must enter the key <adverb>and select a random value, for example ["angrily"].
  • Then he returns to the detour, and stumbles and adds "today." He has reached the end <start>so that he can come up with a proposal now.

How to write a method for randomly generating sentences?

+4
4

!

String # has_placeholder? < >

, . , . . undefined .

, .

class String
  def has_placeholder?
    self=~/<\w+>/
  end
end

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


sentence = grammar["<start>"].sample.join(' ')

while sentence.has_placeholder? do
  puts sentence
  sentence.sub!(/(<\w+>)/){grammar[$1].sample.join(' ')}
end

puts sentence

:

The <object> <verb> tonight.
The slugs <verb> tonight.
The slugs portend like <object> tonight.
The slugs portend like slugs tonight.

The <object> <verb> tonight.
The big yellow flowers <verb> tonight.
The big yellow flowers portend like <object> tonight.
The big yellow flowers portend like slugs tonight.

:

, , :

def expand(grammar, nonterm = "<start>")
  sentence = grammar[nonterm].sample.join(' ')
  while sentence.has_placeholder? do
    sentence.sub!(/(<\w+>)/){grammar[$1].sample.join(' ')}
  end
  sentence
end
+4

, symbols <>, , , , .

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

def generate_sentence key
  return key if key.class == String
  $grammar[key].sample.map {|word| generate_sentence word}.flatten
end

3.times do
  puts generate_sentence(:start).join(" ")
end

:

The big yellow flowers sigh warily tonight.
The slugs die warily tonight.
The big yellow flowers portend like slugs tonight.
+2

, - , . , , , , - - ..

def random_words(h, key)
  h[key].map { |obj| recurse(h,obj) }.join(' ')
end

def recurse(h, obj)
  case obj
  when Array
    obj.map { |o| recurse(h, o) }
  when /\<.+?\>/
    recurse(h, h[obj].sample)
  else
    [obj]
  end
end

< >

1

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

random_words(h, "<start>")
  #=> "The waves portend like slugs tonight."
random_words(h, "<start>")
  #=> "The big yellow flowers sigh warily tonight." 
random_words(h, "<start>")
  #=> "The slugs die warily tonight." 

random_words(h, "<object>")
  #=> "waves big yellow flowers slugs" 
random_words(h, "<verb>")
  #=> "sigh warily portend like waves die warily" 
random_words(h, "<adverb>")
  #=> "warily grumpily" 

2

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

random_words(h, "<start>")
  #=> "The big sigh grumpily yellow flowers die The waves sigh grumpily \
  #    tonight. grumpily tonight." 
random_words(h, "<start>")
  #=> "The big die The big die The slugs sigh grumpily tonight. grumpily \
  #    yellow flowers die The big sigh warily slugs yellow flowers die The \
  #    slugs die The slugs portend like big portend like big sigh grumpily \
  #    yellow flowers yellow flowers tonight. grumpily tonight. grumpily \
  #    tonight. warily waves tonight. warily big die The slugs sigh warily \
  #    big sigh grumpily yellow flowers tonight. warily big portend like big \
  #    portend like waves yellow flowers yellow flowers yellow flowers yellow  \
  #    flowers sigh warily waves tonight." 

3

h = { "<g1>"=>[["It", "<g2>", "<g3>", "..."]],
      "<g2>"=>[["of"], ["waves"], ["was the", "<g3>", "<g4>", "<g3>"],
               ["wisdom,"], ["foolishness,"]],
      "<g3>"=>[["<g4>", "of", "<g2>"], ["it", "<g2>"]],
      "<g4>"=>[["best"], ["worst"], ["age"], ["times,"]]
}

random_words(h, "<g1>")
  #=> "It of it was the it was the it was the times, of foolishness, times, \
  #    it wisdom, best best of was the it of times, it was the times, of of \
  #    best it waves worst age of waves ..."
random_words(h, "<g1>")
  #=> "It was the best of times, it was the worst of times, it was the age of  \
  #    wisdom, it was the age of foolishness...
+2
def sentence_generator(hash)
  verby = hash["<verb>"].sample.map do |string|
    string = hash[string].nil? ? string : hash[string].sample.sample
  end.join(" ")

  hash["<start>"][0][0] + " " + hash["<object>"].sample.sample + " " +  verby + " " + hash["<start>"][0][3]
end

. , . ,

+1

Source: https://habr.com/ru/post/1659354/


All Articles