_TITLE "Ngrams 1" 'started 2019-04-20 B+ ' ref: Daniel Shiffman Coding Challenge #42.1 Markov Chains Part 1
' [youtube]https://www.youtube.com/watch?v=eGFJ8vugIWA&list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH[/youtube]&index=52
' Basically take some text, collect all unique nGrams of a certain length, the gram.
' Collect all the letters that follow a gram in the text in nextChar string.
' Create a new random text with same nGrams based on random selection of following chars.
'setup the grams
nextGI = 1
'get some text, this might be fun
txt$ = txt$ + t$
'PRINT txt$
'Load the grams array
ngCandidate$
= MID$(txt$
, i
, ngLen
) 'PRINT "ngCandidate$ "; ngCandidate$
found = find(ngCandidate$)
'PRINT "found "; found
'add to grams
grams(nextGI).gram = ngCandidate$
grams
(nextGI
).nextChar
= MID$(txt$
, i
+ ngLen
, 1) 'sort grams
QSort 1, nextGI
'FOR i = 1 TO nextGI
' PRINT grams(i).gram, grams(i).nextChar
'NEXT
nextGI = nextGI + 1
grams
(found
).nextChar
= grams
(found
).nextChar
+ "~" + MID$(txt$
, i
+ ngLen
, 1) 'INPUT "OK press enter..."; wate$
'check load
'FOR i = 1 TO nextGI
' PRINT grams(i).gram, grams(i).nextChar
' IF i MOD 20 = 0 THEN
' PRINT: INPUT "OK, press enter...", wate$
' CLS
' END IF
'NEXT
'generate random text
'find a gram that starts with a Capital letter, dang it's always the same start!
i
= INT(RND * (nextGI
- 1)) + 1 rtxt$ = grams(i).gram
rtxt$ = rtxt$ + grams(i).nextChar
Split1000 grams
(i
).nextChar
, "~", select$
() i = i + 1
'PRINT rtxt$, LEN(rtxt$)
g$
= MID$(rtxt$
, LEN(rtxt$
) - ngLen
+ 1) found = find(g$)
rtxt$ = rtxt$ + grams(found).nextChar
Split1000 grams
(found
).nextChar
, "~", select$
() rtxt$ = rtxt$ + " "
SUB QSort
(Start
, Finish
) 'shownK needs to be shared array i = Start
j = Finish
m$
= grams
(INT((i
+ j
) / 2)).gram
i = i + 1
j = j - 1
i = i + 1
j = j - 1
IF j
> Start
THEN QSort Start
, j
IF i
< Finish
THEN QSort i
, Finish
low = 1: hi = nextGI - 1
test = (low + hi) \ 2
IF grams
(test
).gram
< x$
THEN low
= test
+ 1 ELSE hi
= test
- 1
'notes: REDIM the array(0) to be loaded before calling Split '<<<<<<<<<<<<<<<<<<<<<<< IMPORTANT!!!!
' bplus modifications of Galleon fix of Bulrush Split reply #13
' http://xmaxw.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=1612.0
' this sub further developed and tested here: \test\Strings\Split test.bas
copy = mystr 'make copy since we are messing with mystr when the delimiter is a space
'special case if delim is space, probably want to remove all excess space
copy
= MID$(copy
, 1, p
- 1) + MID$(copy
, p
+ 1) curpos = 1
arrpos = 0
dpos
= INSTR(curpos
, copy
, delim
) arr
(arrpos
) = MID$(copy
, curpos
, dpos
- curpos
) arrpos = arrpos + 1
curpos = dpos + LD
dpos
= INSTR(curpos
, copy
, delim
) arr
(arrpos
) = MID$(copy
, curpos
)