Text generator
Posted: Thu Mar 05, 2015 8:04 am
This little program uses a piece of text to generate another text, based upon the words found in the original text. Moreover, it retains some of the structure of that text. First a table is built with one record for each unique (key) word in the text. Next, all words directly following each unique word is added to the record of that word. The new text is generated by printing a keyword, then choosing randomly one of the successor words, use that word as the next keyword, print it, etc.
In general, the logic of the text as a whole is completely lost. But within each sentence, there is still some logic. To a certain extent, the entire text seams more or less readable, but means nothing but nonsence as a whole. I found the original program in a book about proper programming practice. The program in there was somewhat more ambitious, but would require much larger input texts to produce good results and hence to much running time in smart basic. As an example, the program uses this very text to make an input file. If you want to experiment with your own texts, outcomment the 3th line in the program and replace the file named 'text1234' with your own text bloc. Do not forget to put double quotes around the text, because the program reads it as one string. The output is printed on the screen, but also printed to a file named 'out1234'. Run the program and try to make sense of the output.
In general, the logic of the text as a whole is completely lost. But within each sentence, there is still some logic. To a certain extent, the entire text seams more or less readable, but means nothing but nonsence as a whole. I found the original program in a book about proper programming practice. The program in there was somewhat more ambitious, but would require much larger input texts to produce good results and hence to much running time in smart basic. As an example, the program uses this very text to make an input file. If you want to experiment with your own texts, outcomment the 3th line in the program and replace the file named 'text1234' with your own text bloc. Do not forget to put double quotes around the text, because the program reads it as one string. The output is printed on the screen, but also printed to a file named 'out1234'. Run the program and try to make sense of the output.
Code: Select all
option base 1 ! randomize ! z=12
dim s$(5000),w$(2)
make_textfile() ' comment this out if you provide your own textfile
file "text1234" input text$! split text$ to s$,nrec with " "
dim wb$(nrec,z)
for k=1 to nrec-1 ! added=0
w1$=s$(k) ! w2$=s$(k+1)
if wind=0 then add
for i=1 to wind
if wb$(i,1)=w1$ then
for j=2 to z-1
if wb$(i,j)=w2$ then break
if wb$(i,j)="" then ! wb$(i,j)=w2$ ! break ! end if
next j
added=1 ! if j<z then wb$(i,z)=j
end if
next i
if not added then
add: wind+=1 ! wb$(wind,1)=w1$ ! wb$(wind,2)=w2$ ! wb$(wind,z)=2
end if
next k
ind=1 ! file "out1234" delete
loop:
print wb$(ind,1) & " "; ! file "out1234" print wb$(ind,1) & " ";
k=wb$(ind,z) ! suf$=wb$(ind,2+rnd(k-1))
for i=1 to wind
if suf$=wb$(i,1) then ! ind=i ! break ! end if
next i
goto loop
end
def make_textfile()
dim d$(26)
if file_exists("text1234") then file "text1234" delete
d$(1)="""This little program uses a piece of text to generate "
d$(2)="another text, based upon the words found in the original "
d$(3)="text. Moreover, it retains some of the structure of that "
d$(4)="text.First a table is built with one record for each "
d$(5)="unique (key) word in the text. Next, all words directly "
d$(6)="following each unique word is added to the record of that "
d$(7)="word. The new text is generated by printing a keyword, "
d$(8)="then choosing randomly one of the successor words, use "
d$(9)="that word as the next keyword, print it, etc. In general, "
d$(10)="the logic of the text as a whole is completely lost. But "
d$(11)="within each sentence, there is still some logic. To a "
d$(12)="certain extent, the entire text seams more or less "
d$(13)="readable, but means nothing but nonsense as a whole. I "
d$(14)="found the original program in a book about proper "
d$(15)="programming practice. The program in there was somewhat "
d$(16)="more ambitious, but would require much larger input texts "
d$(17)="to produce good results and hence to much running time in "
d$(18)="smart basic. As an example, the program uses this very "
d$(19)="text to make an input file. If you want to experiment with "
d$(20)="your own texts, outcomment the 3th line in the program and "
d$(21)="replace the file named 'text1234' with your own text bloc. "
d$(22)="Do not forget to put double quotes around the text, "
d$(23)="because the program reads it as one string. The output is "
d$(24)="printed on the screen, but also printed to a file named "
d$(25)="'out1234'. Run the program and try to make sense of the "
d$(26)="output."""
for i=1 to 26 ! file "text1234" print d$(i); ! next i
end def