[Lua] Sentence Generator-VBForums
Results 1 to 1 of 1

Thread: [Lua] Sentence Generator

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,156

    [Lua] Sentence Generator

    This one was pretty tough for me. Not necessarily the coding concepts, but the grammar concepts. I actually failed my English II class in high school and was never really good in English class. However this code demonstrates how to create a complex sentence from various words that are stored in arrays.

    Code:
    -- Create the random seed for the random function
    math.randomseed(os.time())
    
    -- Our grammar arrays
    articles = {"the", "a", "one", "some"}
    nouns = {"baby", "boy", "girl", "child", "man", "woman", "dog", "cat", "town", "car"}
    verbs = {"drove", "jumped", "ran", "walked", "skipped", "joke"}
    prepositions = {"to", "from", "over", "under", "on", "about"}
    
    
    while true do
    
    	-- Tell the user to press enter
    	io.write("Press enter to generate a sentence.")
    	io.read()
    
    	-- Set article1 to some random number
    	article1 = math.random(1, table.getn(articles))
    
    	-- Do the same for article two, but don't allow for duplicates
    	repeat
    		article2 = math.random(1, table.getn(articles))
    	until article1 ~= article2
    
    	-- Set noun1 to some random number
    	noun1 = math.random(1, table.getn(nouns))
    
    	-- Do the same for noun two, but don't allow for duplicates
    	repeat
    		noun2 = math.random(1, table.getn(nouns))
    	until noun1 ~= noun2
    
    	-- Set the verb to some random number
    	verb = math.random(1, table.getn(verbs))
    
    	-- Set the preposition to some random number
    	preposition = math.random(1, table.getn(prepositions))
    
    	--Print out our random sentence by getting string that is associated with the array
    	io.write(articles[article1] .. " " .. nouns[noun1] .. " " .. verbs[verb] .. " " .. prepositions[preposition] .. " " .. articles[article2] .. " " .. nouns[noun2] .. "\n\n")
    
    end
    Last edited by dday9; Jun 12th, 2014 at 10:14 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.