powered by NetLogo

view/download model file: MadLibs.nlogo

WHAT IS IT?

In the tradition of the original MadLibs, we have the Netlogo version!


AUTHOR

Peter Brooks, Stuyvesant High School, 2011


PROCEDURES

globals [
  noun-list
  verb-list
  adjective-list
  ]

to-report string2list [L]
  let inword false
  let out-list []
  let i 0
  let wstart 0
  
  if length L = 0 [report out-list]
  
  while [i < length L] [
    ifelse inword [
      if (item i L) = " " [
        set out-list lput (substring L wstart i) out-list
        set inword false
      ]
    ] [
      if (item i L) != " " [
        set inword true
        set wstart i
      ]
    ]
    set i i + 1
  ]
  
  if inword [
    set out-list lput (substring L wstart (length L)) out-list
  ]
  
  report out-list
end

to Generate-sentence
  set noun-list string2list nouns
  if length noun-list = 0 [
    user-message "There are no nouns!  Create a few..."
    stop
  ]
  
  set verb-list string2list verbs
  if length verb-list = 0 [
    user-message "There are no verbs!  Create a few..."
    stop
  ]
  
  set adjective-list string2list adjectives
  if length adjective-list = 0 [
    user-message "There are no adjectives!  Create a few..."
    stop
  ]
  output-print full-sentence
  output-print ""
  
end

to-report noun-phrase [num-adjectives]
  let L (list one-of noun-list)
  if num-adjectives = 0 [report L]
  let n random num-adjectives
  if n = 0 [report L]
  report sentence (n-of n shuffle adjective-list) L
end

to-report full-sentence
  report list2string (sentence "The" (noun-phrase 4) (one-of verb-list) (noun-phrase 3))
end

to-report list2string [L]
  let S item 0 L
  let i 1
  while [i < length L] [
    set S (word S " " item i L)
    set i i + 1
  ]
  set S word S "."
  report S
  
end