[Gambas-user] Wordcount in a TextEdit

MaxVK maximvonk at ...626...
Sun Apr 20 10:56:23 CEST 2008


Cheers Ron, yes Iv considered keeping a buffer and working with that. I did
try SHELLing out to wc, and that worked very well too, although to be fair
it only gave the same results as your own code, so I don't really see the
point.

I might play with the buffer idea a bit later - I have a few documents that
are really quite large and I want to test the comparative speed of wc
against the code you suggested, but Iv a feeling that I might as well keep
this little bit of housekeeping 'in-house' so to speak.

Regards

Max


Ron Onstenk wrote:
> 
> Hint:
> keep a mirror buffer with the text.
> keep count variable
> keep lastkey variable
> 
> check for 'space', 'cr', ',' or '.' if key if pressed 
> 'only these makes a word valid after all
> 
> if newkey <> lastkey then then ' prevent invokes on i.e. double spaces
> 
> 1)
>   put textbox in tmp buffer
>   the replace cr to space (my replcae example code part)
>   (optional delete all numbers someway in tmp)
>   compare mirror buffer against textbox/tmp buffer
>   if different
>     call 'wc' 'awk' or my bits of code with textbox/tmp as source.
>     if count different
>       store new count
>       copy textbox/tmp to mirror buffer
>     end
>   end
> 
> 2)
>   put textbox in tmp buffer
>   the replace cr to space (my replcae example code part)
>   (optional delete all numbers someway in tmp)
>   compare mirror buffer against textbox/tmp buffer
> 
>   if different
>     tmpcount=0
>     split the different on space into array
>       (the code as example code)
>     foreach 
>       check for valid word
>       if valid increment tmpcount
>     next
> 
>     if len(mirror) < len(tmp) then 
>       count=count + tmpcount
>     end 
> 
>     if len(mirror) > len(tmp) then
>       count=count - tmpcount
>       or use full scan with wc/awk/code and store new count
>     end
> 
>     copy tmp to mirror buffer
> 
>   end
>  
> end ' lastkey<>newkey
> 
> I think using a mirror buffer and count here for compare against
> current text will speedup.
> The way in 2) here does the time consuming check/split on a smaller
> text/array
> and for both only when a possible word is given
> 
> BTW
> The second way I did use in the past in a assembly program for something
> like 
> this so this is from very old memory.
> 
> 
> Ron
> 

-- 
View this message in context: http://www.nabble.com/Wordcount-in-a-TextEdit-tp16757888p16790661.html
Sent from the gambas-user mailing list archive at Nabble.com.





More information about the User mailing list