The word extractor function TOOL
Post by michael on Mar 29th, 2016, 9:59pm
A place for FN tools is great for this forum.. And that makes this board useful, if it was for this kind of tool.
My first invented function.. (and even though this is a tool.. I think it is in the wrong area) OH WELL!! ITS A TOOL!
I will need this in my next project which involves POV IDE graphics style work.
This tool is perfected (I think)
Code: PRINT "The custom word extractor (perfected)"
PRINT
PRINT "the last human"
PRINT
PRINT "each word is about to be extracted"
PRINT
A$= FNew("the last human",2)
PRINT "(0,1,2) extraction 2 is :"; A$
A$= FNew("the last human",1)
PRINT "(0,1,2) extraction 1 is :";A$
A$= FNew("the last human",0)
PRINT "(0,1,2) extraction 0 is :";A$
WAIT 0
END
REM it takes txt$ and extracts the coun (holds the number 0,1,2,ect) word from the string
REM ew means extract word
DEF FNew(txt$,coun)
DIM wl$(255)
LOCAL chk$,ps%,sl%,wc%,getword$
FOR x=0 TO 255
wl$(x)=""
NEXT x
chk$="":wc%=0
sl%=LEN(txt$):ps%=1
REPEAT
chk$=MID$(txt$,ps%,1):ps%=ps%+1
IF chk$=" " THEN chk$="":getword$="yes"
wl$(wc%)=wl$(wc%)+chk$
IF getword$="yes" THEN wc%=wc%+1:getword$="no":chk$=""
UNTIL ps%>sl%
=wl$(coun)
Re: The word extractor function TOOL
Post by Zaphod on Mar 29th, 2016, 11:32pm
Yes, you have got the hang of the Function.
Have you thought of using INSTR, which would be simpler.
Code: DEF FNew(z$, n% )
LOCAL I%, J%, b$
FOR I%=0 TO n%
J%=INSTR(z$,CHR$32) :REM Find the next space. J% is zero if there isn't one.
IF J% THEN
b$=LEFT$(z$,J%-1) :z$=MID$(z$,J%+1) :REM Slice off next value from string. Remainder in z$
ELSE
b$=z$ :z$="" :REM This must be the last word.
ENDIF
NEXT
=b$
Or if you like counting from 1
Code: DEF FNew(z$, n% )
LOCAL I%, J%, b$
IF n%=0 :=""
FOR I%=1 TO n%
J%=INSTR(z$,CHR$32)
IF J% THEN
b$=LEFT$(z$,J%-1) :z$=MID$(z$,J%+1)
ELSE
b$=z$ :z$=""
ENDIF
NEXT
=b$
Re: The word extractor function TOOL
Post by DDRM on Mar 30th, 2016, 09:01am
Hi Both,
Useful functions, though it's worth pointing out that both will fail if your list contains double spaces or other punctuation marks etc.
Here's a slightly more complex version that deals with most things, though I dare say you can make it fail if you try! I've chosen to leave apostrophes in.
I've also chosen to convert to ASCII codes to do my tidying, but the characters themselves form an ordinal set, so you can use them directly.
Best wishes,
D
Code:
t$="Hello world, this is David speaking. Here's a test piece to try out my 23 new routine."
PRINT t$
FOR x%=1 TO 20
PRINT FNWordExtract(t$,x%)
NEXT x%
END
:
DEFFNWordExtract(txt$,n%)
LOCAL a%,x%,t$,w$
FOR x%=1 TO LEN(txt$)
a%=ASC(MID$(txt$,x%,1))
REM Strip out any characters that aren't letters, spaces, or apostrophes,
REM replacing them with spaces to preserve word breaks
IF a%=32 OR a%=39 OR (a%>64 AND a%<91) OR (a%>96 AND a%<123) THEN t$+=CHR$(a%) ELSE t$+=CHR$(32)
REM avoid any double spaces
IF RIGHT$(t$,2)=" " THEN t$=LEFT$(t$) :REM Funny usage of LEFT$ - equivalent to LEFT$(t$,LEN(t$)-1)
NEXT x%
REM Clear any spaces (or apostrophes) at the front of the string
WHILE LEFT$(t$,1)<"A"
t$=MID$(t$,2)
ENDWHILE
REM Now go through and find the nth word.
x%=0
REPEAT
x%+=1
a%=INSTR(t$," ")
IF a%=0 THEN
w$=t$
t$=""
ELSE
w$=LEFT$(t$,a%-1)
t$=MID$(t$,a%+1)
ENDIF
IF x%=n% THEN =w$
UNTIL x%=n% OR w$=""
=w$
Re: The word extractor function TOOL
Post by DDRM on Mar 30th, 2016, 09:08am
Version using letters as ordinals - may be clearer to understand than all that ASCII!
:-)
D
Code:
t$="Hello world, this is David speaking. Here's a test piece to try out my 23 new routine."
PRINT t$
FOR x%=1 TO 20
PRINT FNWordExtract(t$,x%)
NEXT x%
END
:
DEFFNWordExtract(txt$,n%)
LOCAL x%,a$,t$,w$
FOR x%=1 TO LEN(txt$)
a$=MID$(txt$,x%,1)
REM Strip out any characters that aren't letters, spaces, or apostrophes,
REM replacing them with spaces to preserve word breaks
IF a$=" " OR a$="'" OR (a$>="A" AND a$<="Z") OR (a$>="a" AND a$<="z") THEN t$+=a$ ELSE t$+=" "
REM avoid any double spaces
IF RIGHT$(t$,2)=" " THEN t$=LEFT$(t$) :REM Funny usage of LEFT$ - equivalent to LEFT$(t$,LEN(t$)-1)
NEXT x%
REM Clear any spaces (or apostrophes) at the front of the string
WHILE LEFT$(t$,1)<"A"
t$=MID$(t$,2)
ENDWHILE
REM Now go through and find the nth word.
x%=0
REPEAT
x%+=1
a%=INSTR(t$," ")
IF a%=0 THEN
w$=t$
t$=""
ELSE
w$=LEFT$(t$,a%-1)
t$=MID$(t$,a%+1)
ENDIF
IF x%=n% THEN =w$
UNTIL x%=n% OR w$=""
=w$
Re: The word extractor function TOOL
Post by michael on Mar 30th, 2016, 3:58pm
To put it simply... I am making the Graphics text controls and tools for not only the reinvention of windows style abilities within the graphics display, but eventually for a special project that is similar to another popular post I made elsewhere..
I can not post it here, as it is conflicting code to this forum.. But I will link it in private message.
The sample is functional, but only went so far as forums only allow so much code on a post..
But it did prove the interest people have in such a tool.
Over 463 views. ( more popular than any program I made including BMPTOCODE, which was unique because of the HUGE level of compression), but in BBC
BMPTOCODE would have been A LOT easier to make.
And if such a IDE POV style tool was made for the 3D world for BBC it would rock the forums!