pseudo code for replacing words in a paragraph efficiently?
am 13.07.2006 16:28:21 von Mike
Hi guys,
I have a table of substitutions, like this:
id word substitutions
1 cat feline mammal
2 dog mans best friend
3 bird creature belonging in a cage
and then I have some text,
The big cat was swallowed up by the fat dog just as an ugly bird flew
overhead.
and I want to know the most efficient way of replacing the words in the
text with the substitutions in the table for those words:
The big feline mammal was swallowed up by the fat mans best friend as
an ugly creature belonging in a cage flew overhead.
right now I get a recordset from the database, then use the Replace()
vbscript function, as follows:
while not rs.eof
replace(text, rs("term"), rs("substitution"))
rs.movenext
wend
This seems kind of inefficient and server-intensive. Is there a better
way to do it dynamically (I dont want to store the text with the
substitutions)?
Thanks,
Mike
Re: pseudo code for replacing words in a paragraph efficiently?
am 13.07.2006 16:44:43 von reb01501
mike wrote:
> Hi guys,
>
> I have a table of substitutions, like this:
>
> id word substitutions
> 1 cat feline mammal
> 2 dog mans best friend
> 3 bird creature belonging in a cage
>
> and then I have some text,
>
> The big cat was swallowed up by the fat dog just as an ugly bird flew
> overhead.
>
> and I want to know the most efficient way of replacing the words in
> the text with the substitutions in the table for those words:
>
> The big feline mammal was swallowed up by the fat mans best friend as
> an ugly creature belonging in a cage flew overhead.
>
> right now I get a recordset from the database, then use the Replace()
> vbscript function, as follows:
>
> while not rs.eof
> replace(text, rs("term"), rs("substitution"))
> rs.movenext
> wend
>
> This seems kind of inefficient and server-intensive. Is there a
> better way to do it dynamically (I dont want to store the text with
> the substitutions)?
>
Well, I would use GetRows to put the substitutions into an array which,
depending on how static the data is, I would cache either in Application
or Session. Then, when it came time to process the text:
Let's assume the list of substitutions rarely changes so we decide to
put it in Application, using Application_onstart in global.asa:
'you've previously opened cn, your connection
set rs=cn.execute(select term,substitution from substitutions",,1)
if not rs.eof then ar = rs.getrows
rs.close: set rs=nothing
cn.close: set cn=nothing
Application("SubstList") = ar
end sub
Then, in the page that processes the text:
dim arSubst, i
arSubst=Application("SubstList")
if isarray(arSubst) then
for i=0 to ubound(arSubst,2)
text = Replace(text, arSubst(0,i),arSubst(1,i))
next
end if
There may be a way to create a regular expression from the data in the
array to do this in one action, but I don't have time to play with that
right now.
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.