Author Topic: Poem Generator  (Read 3570 times)

0 Members and 1 Guest are viewing this topic.

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Poem Generator
« on: May 27, 2021, 09:39:43 pm »
This is a simple poem generator, with some grammar/vocabulary rules.

There is no limit to how this program can grow.

I also attached the source, just in case you do not want the 3 or so spaces that are inserted when selecting/copying the source from the code box.

It is well commented. Have fun.

Code: QB64: [Select]
  1. _TITLE "Poem Generator"
  2. '*******************************************************************************
  3. '*** Poem Generator
  4. '*** By George McGinn - May 27, 2021
  5. '***
  6. '***
  7. '*** About the AI system
  8. '***    Since writing poems has specific rules, which can easily break grammar
  9. '***    rules, the AI will impose just the basic grammar syntax, like adding a
  10. '***    "an" when required, and not ending sentences with words such as
  11. '***    "like" which happens now. They will have a special modifier tag as with
  12. '***    the tables in use, one of them can be used to finish the sentence.
  13. '***
  14. '***    However, poems do not follow strict sentence structure either, and like
  15. '***    at the end of a line, since there is no period, it may be a pause where
  16. '***    the first words on the next continues the thought. This will be
  17. '***    built into the modifier tag. It will also check the words ahead of it to
  18. '***    see if they are on the modifier list for the word "like" and others like
  19. '***    it.
  20. '***
  21. '***    So due to the special circumstances on meter and rhyme and even the
  22. '***    structure of the poem, the AI system will rely heavily on rule and word
  23. '***    tables that will provide it the words, apply modifiers, check for any
  24. '***    grammar changes, and will have a way to keep the poem on one theme.
  25. '***
  26. '***    And for this system to work properly, the vocabulary must grow to
  27. '***    proportions where each word has associated tags for use with other words
  28. '***    to help the AI engine to keep the poem on target.
  29. '***
  30. '*******************************************************************************
  31.  
  32.  
  33. 'SCREEN 12
  34. SCREEN _NEWIMAGE(800, 600, 32)
  35. DEFINT A-Z
  36.  
  37.  
  38. '***************************************************************
  39. '*** Initialize variables/arrays (comments to prevent confusion)
  40. '***
  41. x = 0 '* Index to poem length (# lines)
  42. i = 0 '* Index to loading Arrays
  43.  
  44. cr$ = CHR$(10) '* Add a Carriage-Return character
  45.  
  46. Author$ = "By QB64 Program" '* Name of Programmer running code
  47.  
  48. DIM article$(35), noun$(60), verb$(46), adjective$(46)
  49. DIM colormodifier$(28), colortable$(8), nounmodifiers$(14)
  50. DIM connectives$(151), conective_extended$(34)
  51.  
  52.  
  53. '**************************************************
  54. '*** Load Arrays from Data Statements
  55. '***
  56.  
  57. GOSUB LoadWordArrays
  58.  
  59.  
  60. '**************************************************
  61. '*** Pick a 2- to 3-word title
  62. '***
  63. ta$ = article$(INT(RND * 35) + 1)
  64. b$ = LCASE$(noun$(INT(RND * 60) + 1)) '** Pick a primary noun
  65. b1$ = LCASE$(noun$(INT(RND * 60) + 1)) '** Pick a noun-color modifier
  66. c$ = LCASE$(verb$(INT(RND * 46) + 1)) '** Pick a primary verb
  67. c1$ = LCASE$(verb$(INT(RND * 46) + 1)) '** Pick a verb modifier
  68. d$ = LCASE$(adjective$(INT(RND * 46) + 1)) '** Pick an adjective
  69. bc$ = LCASE$(colormodifier$(INT(RND * 28) + 1)) '** Pick a modifier for a color
  70. color$ = LCASE$(colortable$(INT(RND * 8) + 1)) '** Pick a color as a modifier
  71. nounmod$ = LCASE$(nounmodifiers$(INT(RND * 14) + 1)) '** Pick a non-color noun modifier
  72.  
  73. IF LEFT$(b$, 1) = "@" OR LEFT$(b1$, 1) = "@" THEN GOSUB CheckForCapitalization
  74. IF RIGHT$(b$, 1) = "*" OR RIGHT$(b$, 1) = "+" THEN GOSUB CheckForModifierAfter
  75. IF LEFT$(b$, 1) = "*" OR LEFT$(b$, 1) = "+" THEN GOSUB CheckForModifierBefore
  76. tb$ = b$
  77.  
  78. Title$ = ta$ + " " + tb$
  79.  
  80.  
  81. '**************************************************
  82. '*** Create the poem, 16 lines in 4 4-line stanzas
  83. '***
  84.  
  85. PRINT SPACE$(3) + Title$
  86. PRINT SPACE$(3) + Author$
  87.  
  88.  
  89. '*** NOTE: b1 & c1 placeholders for future use as modifiers, or
  90. '***       to initially use b1 & c1 as modifiers themselves.
  91. FOR x = 1 TO 16
  92.     a$ = LCASE$(article$(INT(RND * 35) + 1)) '** Pick an Article
  93.     b$ = LCASE$(noun$(INT(RND * 60) + 1)) '** Pick a primary noun
  94.     b1$ = LCASE$(noun$(INT(RND * 60) + 1)) '** Pick a noun-color modifier
  95.     c$ = LCASE$(verb$(INT(RND * 46) + 1)) '** Pick a primary verb
  96.     c1$ = LCASE$(verb$(INT(RND * 46) + 1)) '** Pick a verb modifier
  97.     d$ = LCASE$(adjective$(INT(RND * 46) + 1)) '** Pick an adjective
  98.     bc$ = LCASE$(colormodifier$(INT(RND * 28) + 1)) '** Pick a modifier for a color
  99.     color$ = LCASE$(colortable$(INT(RND * 8) + 1)) '** Pick a color as a modifier
  100.     nounmod$ = LCASE$(nounmodifiers$(INT(RND * 14) + 1)) '** Pick a non-color noun modifier
  101.  
  102.     '*** Check for modifiers to nouns
  103.     IF LEFT$(b$, 1) = "@" OR LEFT$(b1$, 1) = "@" OR LEFT$(bc$, 1) = "@" THEN GOSUB CheckForCapitalization
  104.     IF LEFT$(b$, 1) = "*" OR LEFT$(b$, 1) = "+" THEN GOSUB CheckForModifierBefore
  105.     IF RIGHT$(b$, 1) = "*" OR RIGHT$(b$, 1) = "+" THEN GOSUB CheckForModifierAfter
  106.  
  107.     '*** Capitalize the first word of each sentence or stanza (whether noun or verb)
  108.     r = RND(2) + 1
  109.     IF r = 1 THEN
  110.         z = LEN(a$)
  111.         a$ = UCASE$(LEFT$(a$, 1)) + RIGHT$(a$, z - 1)
  112.         Line$ = a$ + " " + b$ + " " + c$ + " " + d$
  113.     ELSE
  114.         z = LEN(c$)
  115.         c$ = UCASE$(LEFT$(c$, 1)) + RIGHT$(c$, z - 1)
  116.         Line$ = c$ + " " + b$ + " " + d$
  117.     END IF
  118.     IF x MOD 4 = 0 THEN Line$ = Line$ + "." + cr$
  119.     PRINT SPACE$(3) + Line$
  120.  
  121.  
  122. endPROG:
  123. '**************************************************
  124. '*** Print Copyright and Date/Time Stamp and END
  125. '***
  126.  
  127. mnth$ = LEFT$(DATE$, 2): M = VAL(mnth$)
  128. day$ = MID$(DATE$, 4, 2): D = VAL(day$)
  129. day$ = STR$(D) ' eliminate any leading zeros
  130. year$ = RIGHT$(DATE$, 4): Y = VAL(year$)
  131.  
  132.     CASE 1: Month$ = "January"
  133.     CASE 2: Month$ = "February"
  134.     CASE 3: Month$ = "March"
  135.     CASE 4: Month$ = "April"
  136.     CASE 5: Month$ = "May"
  137.     CASE 6: Month$ = "June"
  138.     CASE 7: Month$ = "July"
  139.     CASE 8: Month$ = "August"
  140.     CASE 9: Month$ = "September"
  141.     CASE 10: Month$ = "October"
  142.     CASE 11: Month$ = "November"
  143.     CASE 12: Month$ = "December"
  144.  
  145.  
  146. '*** Copyright Sign (Character, Unicode, UTF-8 Values
  147. '***
  148.  
  149. PRINT: PRINT: PRINT SPACE$(3) + "(C)2021 " + Author$ + " (if it is good), All Rights Reserved"
  150. PRINT SPACE$(3) + "Created " + WeekDay$(M, D, Y) + ", " + Month$ + day$ + ", " + year$ + " at " + Clock$
  151.  
  152.  
  153.  
  154. '***
  155. '*** END OF MAIN PROGRAM
  156. '*******************************************************************
  157.  
  158.  
  159. '*******************************************************************
  160. '*** FUNCTIONS/SUB ROUTINES
  161. '***
  162.  
  163.  
  164. LoadWordArrays:
  165. '**************************************************
  166. '*** Load Arrays from Data Statements
  167. '***
  168.  
  169. '*** Load Articles
  170.     FOR i = 1 TO 35: READ article$(i): NEXT i
  171.  
  172. '*** Load Nouns
  173.     FOR i = 1 TO 60: READ noun$(i): NEXT i
  174.  
  175. '*** Load Verbs
  176.     FOR i = 1 TO 46: READ verb$(i): NEXT i
  177.  
  178. '*** Load Adjectives
  179.     FOR i = 1 TO 46: READ adjective$(i): NEXT i
  180.  
  181. '*** Load Color Modifiers
  182.     FOR i = 1 TO 28: READ colormodifier$(i): NEXT i
  183.  
  184. '*** Load Color Table
  185.     FOR i = 1 TO 7: READ colortable$(i): NEXT i
  186.  
  187. '*** Load Noun Modifiers
  188.     FOR i = 1 TO 14: READ nounmodifiers$(i): NEXT i
  189.  
  190. '*** Load Connectives
  191.     FOR i = 1 TO 151: READ connectives$(i): NEXT i
  192.  
  193. '*** Load Contective Extended
  194.     FOR i = 1 TO 34: READ conective_extended$(i): NEXT i
  195.  
  196.  
  197.  
  198.  
  199. CheckForCapitalization:
  200. '*** Check for modifiers (@=Capitalize Word)
  201. IF LEFT$(b$, 1) = "@" THEN
  202.     z = LEN(b$)
  203.     b$ = RIGHT$(b$, z - 1)
  204.     b$ = UCASE$(LEFT$(b$, 1)) + RIGHT$(b$, z - 2)
  205. IF LEFT$(b1$, 1) = "@" THEN
  206.     z = LEN(b1$)
  207.     b1$ = RIGHT$(b1$, z - 1)
  208.     b1$ = UCASE$(LEFT$(b1$, 1)) + RIGHT$(b1$, z - 2)
  209. IF LEFT$(bc$, 1) = "@" THEN
  210.     z = LEN(bc$)
  211.     bc$ = RIGHT$(bc$, z - 1)
  212.     bc$ = UCASE$(LEFT$(bc$, 1)) + RIGHT$(bc$, z - 2)
  213.  
  214.  
  215.  
  216. CheckForModifierBefore:
  217. '*** Check for modifiers Before
  218.  
  219. '*** "*" the noun needs a color before it
  220. IF LEFT$(b$, 1) = "*" THEN
  221.     z = LEN(b$)
  222.     b$ = RIGHT$(b$, z - 1)
  223.     b$ = color$ + " " + b$
  224.  
  225. '*** "+" the noun needs another noun to follow it (selection from noun modifier)
  226. IF RIGHT$(b$, 1) = "+" THEN
  227.     z = LEN(b$)
  228.     b$ = LEFT$(b$, z - 1)
  229.     b$ = nounmod$ + " " + b$
  230.  
  231.  
  232.  
  233. CheckForModifierAfter:
  234. '*** Check for modifiers after
  235.  
  236. '*** "*" the noun is a color and only color-friendly nouns considered
  237. IF RIGHT$(b$, 1) = "*" THEN
  238.     z = LEN(b$)
  239.     b$ = LEFT$(b$, z - 1)
  240.     b$ = b$ + " " + bc$
  241.  
  242. '*** "+" the noun needs another noun to follow it (selection from noun modifier)
  243. IF RIGHT$(b$, 1) = "+" THEN
  244.     z = LEN(b$)
  245.     b$ = LEFT$(b$, z - 1)
  246.     b$ = b$ + " " + nounmod$
  247.  
  248.  
  249.  
  250.  
  251. '**************************************************
  252. '*** DATA Statements for Poem Generator
  253. '***
  254.  
  255. '*** 35 Articles
  256. articles:
  257. DATA "The","All of the","Most of the","Some of the"
  258. DATA "My","Your","His","Her","Their","Our","Everybody's","Almost all of the"
  259. DATA "That","I knew that the","We knew that the","She knew that the","He knew that the","They knew that the","And the coming","Oh, the","A spring of","Beyond the","Within the","And the","Alone, alone,","I fear","I looked upon","A","But where the","Like","A still and","Alone","All alone, a","The moving","It is"
  260.  
  261.  
  262. '*** 60 Nouns
  263. nouns:
  264. DATA "darkness","morning","morning+","light","feeling","feeling+","beauty","love","hatred","happiness","sadness","anger","frustration","expression","message","ship","lips","mouth","voice","garment","saint","snake","snakes","water","fire","lead","dreams","air","ghost","sails","sleep","river","cloud","@moon","@sun","waters","life","stars","the stars","lightning","beams","beams+","*beard","fool","white*","black*","green*","blue*","yellow*","red*","light beams","*river","*garment","*ship","*snake","*snakes","*sails","voice+","ship+","ocean blue"
  265.  
  266.  
  267. '*** 46 Verbs (lv)
  268. verbs:
  269. DATA "was","had been","will be","could be","might be","should have been","would have been","could have been","drunk","drank","was heavenly","a heavy","blessed","glossy","velvet","flash","kind","coiled","swarm","swarmed","fire","pity","filled","fill","moved","wind","danced","steep","wide","steep and wide","thick","light","roar","loud","more loud","lightning","struck","fell","more horrible","horrible","awful","hit","huge","holds","long","surrounds"
  270.  
  271.  
  272. '*** 46 Adjectives/adverbs
  273. adjectives:
  274. DATA "abstract","mysterious","permanent","unfortunate","was unfortunate"
  275. DATA "intricate","confusing","serene","confusing"
  276. DATA "true","false","fake","a lie","burden"
  277. DATA "a stranger","a friend","an enemy"
  278. DATA "terrible","enchanting","is mine","was yours","is his","is hers","was theirs","was ours"
  279. DATA "fortunate","was understood","mine","is mutual","with an artistic flair","was musical"
  280. DATA "golden pond","blessed","moment","unaware","no","yes","sure"
  281. DATA "like","dreams","between","inbetween","alone","than that","interesting","glossy lake"
  282.  
  283.  
  284. '*** 28 Color modifiers
  285. colormodifiers:
  286. DATA "morning","light","beauty","love","ship","lips","garment","knight","saint","snake","snakes","water","fire","ghost","sails"
  287. DATA "river","cloud","@moon","@sun","waters","stars","lightning","beams","beard","velvet","flash","grass","book"
  288.  
  289.  
  290. '*** 8 Color table entries
  291. colortable:
  292. DATA "red","yellow","blue","green","white","black","orange","purple"
  293.  
  294.  
  295. '*** 14 Noun Modifier Table (Nouns, Verbs/Adverbs, Adjectives)
  296. nounmodifier:
  297. DATA "sunshine","light","glow","fog","of peace","of love","blue","great","unloved","sad","happy","loved","of light","of love"
  298.  
  299. '*** 151 Connectives
  300. connectives:
  301. DATA "I","the","of","and","to","a","in","that","is","was","he","for","it"
  302. DATA "with","as","his","on","be","at","by","i","this","had","not"
  303. DATA "are","but","from","or","have","an","they","which","one","you","were"
  304. DATA "her","all","she","there","would","their","we","him","been","has"
  305. DATA "when","who","will","more","no","if","out","so","said","what","u","its","about"
  306. DATA "into","than","them","can","only","other","new","some","could","time","these"
  307. DATA "two","may","then","do","first","any","my","now","such","like","our"
  308. DATA "over","man","me","even","most","made","after","also","did","many","before","must"
  309. DATA "through","back","years","where","much","your","way","well","down","should"
  310. DATA "because","each","just","those","people","mr","how","too","little"
  311. DATA "state","good","very","make","world","still","own","see","men","work","long"
  312. DATA "get","here","between","both","life","being","under","never","day","same"
  313. DATA "another","know","while","last","might","us","great","old","year","off"
  314. DATA "come","since","against","go","came","right","used","take","three"
  315.  
  316. '*** 34 Extened Connectives:
  317. connectives_extend:
  318. DATA "whoever","nonetheless","therefore","although","consequently","furthermore"
  319. DATA "whereas","nevertheless","whatever","however","besides","henceforward","yet"
  320. DATA "until","alternatively","meanwhile","notwithstanding","whenever"
  321. DATA "moreover","despite","similarly","firstly","secondly","lastly","eventually"
  322. DATA "gradually","finally","thus","hence","accordingly","otherwise","indeed"
  323. DATA "though","unless"
  324.  
  325.  
  326.  
  327.  
  328. '*******************************************************************
  329. '*** FUNCTIONS (FORMAT DATE AND TIME FIELDS)
  330. '***
  331.  
  332.  
  333. FUNCTION WeekDay$ (M, D, Y)
  334.     IF M < 3 THEN M = M + 12: Y = Y - 1 'add 12 to Jan - Feb month, -1 year
  335.     C = Y \ 100: Y = Y MOD 100 'split century and year number
  336.     S1 = (C \ 4) - (2 * C) - 1 'century leap
  337.     S2 = (5 * Y) \ 4 '4 year leap
  338.     S3 = 26 * (M + 1) \ 10 'days in months
  339.     WkDay = (S1 + S2 + S3 + D) MOD 7 'weekday total remainder
  340.     IF WkDay < 0 THEN WkDay = WkDay + 7 'Adjust negative results to 0 to 6
  341.     SELECT CASE WkDay
  342.         CASE 0: day$ = "Sunday"
  343.         CASE 1: day$ = "Monday"
  344.         CASE 2: day$ = "Tuesday"
  345.         CASE 3: day$ = "Wednesday"
  346.         CASE 4: day$ = "Thursday"
  347.         CASE 5: day$ = "Friday"
  348.         CASE 6: day$ = "Saturday"
  349.     END SELECT
  350.     WeekDay$ = day$
  351.  
  352.  
  353. FUNCTION Clock$
  354.     hour$ = LEFT$(TIME$, 2): H% = VAL(hour$)
  355.     min$ = MID$(TIME$, 3, 3)
  356.     IF H% >= 12 THEN ampm$ = " pm" ELSE ampm$ = " am"
  357.     IF H% > 12 THEN
  358.         IF H% - 12 < 10 THEN hour$ = STR$(H% - 12) ELSE hour$ = LTRIM$(STR$(H% - 12))
  359.     ELSEIF H% = 0 THEN hour$ = "12" ' midnight hour
  360.     ELSE: IF H% < 10 THEN hour$ = STR$(H%) ' eliminate leading zeros
  361.     END IF
  362.     Clock$ = hour$ + min$ + ampm$
* Poem Generator.bas (Filesize: 13.96 KB, Downloads: 160)
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Poem Generator
« Reply #1 on: May 28, 2021, 06:26:02 am »
I think my computer has a dirty mind...

an excerpt from "I Looked Upon Beams Great"

Quote
...
The water thick intricate
Long darkness a stranger
Oh, the feeling of love long like
Glossy red beard between
...

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: Poem Generator
« Reply #2 on: May 28, 2021, 07:59:53 am »
Mysterious and fun...
Why not yes ?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Poem Generator
« Reply #3 on: May 28, 2021, 10:42:07 am »
I am looking over the code and wondering why the weekday and clock Subs?

LOL oh it's for copyright "if it is good"

I suppose for those with dirty minds, this might be...

image_2021-05-28_104203.png
* image_2021-05-28_104203.png (Filesize: 13.27 KB, Dimensions: 539x514, Views: 158)
« Last Edit: May 28, 2021, 10:44:11 am by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Poem Generator
« Reply #4 on: June 02, 2021, 11:47:58 pm »
Well, it's a bit better than the FreeBasic version I tried a few years ago. All it generated was...

Here I sit
Brokenhearted
Tried F.B.
It was retarded

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Poem Generator
« Reply #5 on: June 03, 2021, 09:22:31 pm »
Congrats for creating the strangest program I have ever seen.
It works better if you plug it in.