Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Keyword Parsing with ASP

Reply
Thread Tools

Keyword Parsing with ASP

 
 
ARK
Guest
Posts: n/a
 
      08-29-2003
I am writing a search program in ASP(VBScript). The user can enter keywords
and press submit.
The user can separate the keywords by spaces and/or commas and key words may
contain plain words, single quoted strings (phrases), double quoted strings
(phrases).
For example:

Keywords:

Jack, Jill, Jim, "Timothy Brown", Mary OR
Jack Jill Jim 'Timothy Brown' Mary OR
Jack, Jill Jim, 'Timothy Brown' "Mary"

When I parse it i store the keywords in an array. The results must be:

Jack
Jill
Jim
Timothy Brown
Mary

I have tried doing this using Split but am unable to get the Phrases. Some
suggestions, code examples or links would help.

Thanks in advance

ARK.


 
Reply With Quote
 
 
 
 
Andrew J Durstewitz
Guest
Posts: n/a
 
      08-29-2003
You might want to replace the spaces the user puts in with commas and
then use the split command.

strVariable = Replace(strVariable," ",",")
Split(strVariable,",")

Then you should have your array of items.

hth,
Andrew

* * * Sent via DevBuilder http://www.devbuilder.org * * *
Developer Resources for High End Developers.
 
Reply With Quote
 
 
 
 
TomB
Guest
Posts: n/a
 
      08-29-2003
Unfortunately, that would put a comma in "Timothy Brown" as well.

My suggestion would be to work your way through the string a character at a
time. If the character is a space, and not within quotes (" or ') then add
a comma, otherwise move along








"Andrew J Durstewitz" <(E-Mail Removed)> wrote in message
news:3f4f51c1$0$62076$(E-Mail Removed)...
> You might want to replace the spaces the user puts in with commas and
> then use the split command.
>
> strVariable = Replace(strVariable," ",",")
> Split(strVariable,",")
>
> Then you should have your array of items.
>
> hth,
> Andrew
>
> * * * Sent via DevBuilder http://www.devbuilder.org * * *
> Developer Resources for High End Developers.



 
Reply With Quote
 
Aaron Bertrand - MVP
Guest
Posts: n/a
 
      08-29-2003
> My suggestion would be to work your way through the string a character at
a
> time. If the character is a space, and not within quotes (" or ') then

add
> a comma, otherwise move along


This can get infinitely complex, e.g.

Bob, Mary, "Timothy, Brown" 'franke, "tom, hula hoop" mea, culpa"

You never know what a user is going to enter, and it's hard to write code to
understand exactly what they mean.

I'd be really interested to see how Google's parsing algorithm works. I
wasn't brave enough to do that: www.aspfaq.com supports all words, any
words, or exact phrase... but no combination of the three.


 
Reply With Quote
 
Andrew Durstewitz
Guest
Posts: n/a
 
      08-29-2003
I agree, keep it simple as possible. Setting up validation characters
such as " will require that you analyze the string one character at a
time.

Andrew

* * * Sent via DevBuilder http://www.devbuilder.org * * *
Developer Resources for High End Developers.
 
Reply With Quote
 
Ray at
Guest
Posts: n/a
 
      08-29-2003
Google is a non-stop source of awe. This is why I buy Google t-shirts. The
calculator can also do some math too in addition to unit conversion (okay,
that's also math, but fine), i.e.
5 percent of 343

Ray at work

"TomB" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Yes you are right, that would be complex.
> Speaking of Google's parsing have you seen the calculator? Try

"searching"
> for
>
> 100 kilometers in miles
>
> Very cool.
>
>



 
Reply With Quote
 
Jon Mundsack
Guest
Posts: n/a
 
      08-29-2003
SWEET! Thanks for the tip.

"TomB" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Yes you are right, that would be complex.
> Speaking of Google's parsing have you seen the calculator? Try

"searching"
> for
>
> 100 kilometers in miles
>
> Very cool.
>
>
> "Aaron Bertrand - MVP" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > > My suggestion would be to work your way through the string a character

> at
> > a
> > > time. If the character is a space, and not within quotes (" or ')

then
> > add
> > > a comma, otherwise move along

> >
> > This can get infinitely complex, e.g.
> >
> > Bob, Mary, "Timothy, Brown" 'franke, "tom, hula hoop" mea, culpa"
> >
> > You never know what a user is going to enter, and it's hard to write

code
> to
> > understand exactly what they mean.
> >
> > I'd be really interested to see how Google's parsing algorithm works. I
> > wasn't brave enough to do that: www.aspfaq.com supports all words, any
> > words, or exact phrase... but no combination of the three.
> >
> >

>
>



 
Reply With Quote
 
TomB
Guest
Posts: n/a
 
      08-29-2003
Yeah, I think that's why they call it a calculator
I thought the fact that it was able to determine that I wanted a calculation
rather than a search for the words was the cool part.


"Ray at <%=sLocation%>" <myfirstname at lane34 dot com> wrote in message
news:(E-Mail Removed)...
> Google is a non-stop source of awe. This is why I buy Google t-shirts.

The
> calculator can also do some math too in addition to unit conversion (okay,
> that's also math, but fine), i.e.
> 5 percent of 343
>
> Ray at work
>
> "TomB" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Yes you are right, that would be complex.
> > Speaking of Google's parsing have you seen the calculator? Try

> "searching"
> > for
> >
> > 100 kilometers in miles
> >
> > Very cool.
> >
> >

>
>



 
Reply With Quote
 
Aaron Bertrand - MVP
Guest
Posts: n/a
 
      08-29-2003
It even works with slightly more complex phrases, like 100 degrees
fahrenheit in celsius



"TomB" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Yeah, I think that's why they call it a calculator
> I thought the fact that it was able to determine that I wanted a

calculation
> rather than a search for the words was the cool part.



 
Reply With Quote
 
Bob Barrows
Guest
Posts: n/a
 
      08-29-2003
OK, I've come up with the following function that returns an array
containing the keywords. However, in order for this to work, you need to set
some ground rules:
1. Don't mix delimiters for a phrase. This will work correctly:
Jack, Jill Jim, "Timothy Brown", 'Mary'
but this will not:
Jack, Jill Jim, "Timothy Brown', 'Mary'

2. If literal delimiter characters are used, then they must not match the
delimiters used. For example, this will work:
"O'Malley"
but this will not:
'O'Malley'
Also, if literal delimiter characters are used, all delimiters in the entire
list must be the same. This will work:
Jim, "Tom Brown", "Pat O'Malley"
This won't:
Jim, 'Tom Brown', "Pat O'Malley"

Anyways, the function appears below my signature. You can use this code to
test it:
Dim iCount, arResult, sWords
sWords="Jack, Jill Jim, ""Timothy Brown"", 'Mary'"
Response.Write sWords & "<BR>"
arResult= ParseKeywords(sWords)
if IsArray(arResult) then
for iCount = 0 to UBound(arResult)
Response.Write arResult(iCount) & "<BR>"
next
end if

HTH,
Bob Barrows

Function ParseKeywords(pKeywords)
Dim sKeywords,iQuotes, arQuoted(), i, j, k, sTmp, bQfound, bSQFound
dim iCommas, arCommas, arSpaces, bArrayDefined, arKeywords()
bArrayDefined = false
sKeywords = pKeywords
'first see if sKeywords contains quoted sections - if so, make
'sure they are paired, ie, there is an even number of quotes
iQuotes = len(sKeywords) - len(Replace(sKeywords,"""",""))
bQfound = false
if iQuotes > 0 then
if iQuotes mod 2 = 0 then
bQfound = true
redim arQuoted(iQuotes/2 - 1)
i=instr(sKeywords,"""")
k = 0
Do Until i = 0
j = instr(i+1,sKeywords,"""")
sTmp = mid(sKeywords,i,j+1-i)
arQuoted(k) = sTmp
k=k+1
sKeywords = replace(sKeywords,sTmp,"")
i=instr(sKeywords,"""")
Loop
for i = 0 to ubound(arQuoted)
arQuoted(i) = replace(arQuoted(i),"""","")
next
end if
end if

'now find single-quoted sections
iQuotes = len(sKeywords) - len(Replace(sKeywords,"'",""))
bSQFound = false
if iQuotes > 0 then
if iQuotes mod 2 = 0 then
bSQFound = true
if bQfound = false then
redim arQuoted(iQuotes/2 - 1)
k = 0
else
k = ubound(arQuoted) + 1
Redim preserve arQuoted(UBound(arQuoted) + iQuotes/2)
end if
i=instr(sKeywords,"'")
Do Until i = 0
j = instr(i+1,sKeywords,"'")
sTmp = mid(sKeywords,i,j+1-i)
arQuoted(k) = sTmp
k=k+1
sKeywords = replace(sKeywords,sTmp,"")
i=instr(sKeywords,"'")
Loop
for i = 0 to ubound(arQuoted)
arQuoted(i) = replace(arQuoted(i),"'","")
next
end if
end if
sKeywords = RTrim(sKeywords)
do until right(sKeywords,1) <> ","
sKeywords = rtrim(left(sKeywords,len(sKeywords)-1))
loop

'add quoted sections to result array
if bQfound or bSQFound then
redim arKeywords(UBound(arQuoted))
for i = 0 to ubound(arQuoted)
arKeywords(i) = arQuoted(i)
next
bArrayDefined = true
end if

'now process commas and spaces

iCommas = len(sKeywords) - len(Replace(sKeywords,",",""))
arCommas=split(sKeywords,",")
for i = 0 to ubound(arCommas)
arCommas(i) = RTrim(LTrim(arCommas(i)))
if len(arCommas(i)) > 0 then
if instr(arCommas(i)," ") = 0 then
if bArrayDefined then
redim preserve arKeywords(UBound(arKeywords) + 1)
else
redim arKeywords(0)
end if
arKeywords(ubound(arKeywords)) = arCommas(i)
else
arSpaces = split(arCommas(i)," ")
for j = 0 to ubound(arSpaces)
arSpaces(j) = RTrim(LTrim(arSpaces(j)))
if len(arSpaces(j)) > 0 then
if bArrayDefined then
redim preserve arKeywords(UBound(arKeywords) + 1)
else
redim arKeywords(0)
end if
arKeywords(ubound(arKeywords)) = arSpaces(j)
end if
next
end if
end if
next
ParseKeywords=arKeywords
end function


ARK wrote:
> I am writing a search program in ASP(VBScript). The user can enter
> keywords and press submit.
> The user can separate the keywords by spaces and/or commas and key
> words may contain plain words, single quoted strings (phrases),
> double quoted strings (phrases).
> For example:
>
> Keywords:
>
> Jack, Jill, Jim, "Timothy Brown", Mary OR
> Jack Jill Jim 'Timothy Brown' Mary OR
> Jack, Jill Jim, 'Timothy Brown' "Mary"
>
> When I parse it i store the keywords in an array. The results must be:
>
> Jack
> Jill
> Jim
> Timothy Brown
> Mary
>
> I have tried doing this using Split but am unable to get the Phrases.
> Some suggestions, code examples or links would help.
>
> Thanks in advance
>
> ARK.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing keyword=value pairs pgodfrin Perl Misc 4 02-23-2009 04:43 PM
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
ASP VIRTUAL keyword not working in ASP.NET anymore :S Dhruba Bandopadhyay ASP .Net 4 03-22-2006 03:21 PM
Text to keyword parsing script? abracad_1999@yahoo.com Perl Misc 3 06-09-2005 07:06 AM



Advertisments