| Author |
Topic Search Topic Options
|
usaboy
Newbie
Joined: 05 July 2003
Status: Offline
Points: 9
|
Post Options
Thanks(0)
Quote Reply
Topic: Triming specific number of words before & Posted: 05 July 2003 at 11:02am |
Hi Experts.. i have designed a search page.. and in the result page i have the string matching the query and the words found in the string are highlighted using a function..
the thing that I want to do now is to trim the string to 10 words before the matching keywords and 10 words after the matching keywords.. and a link to detail page so they can view the full text matching the query.. i don't have probelm with detail page. but with triming.. i just don't know what to do... i need a function to do that for me.. it will be a great help if anyone can give me the function to the triming as follow :
trim 10 words before the matching keywords and 10 words after the matching keywords in a string
tanx sooo much.
|
 |
pmormr
Senior Member
Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
|
Post Options
Thanks(0)
Quote Reply
Posted: 05 July 2003 at 11:23am |
|
so you want to do something like...
Stuff on page: Hello world my name is paul and i really like computers. I just finished cutting the grass, its is really hot where i live...
you input "finished"
your want outputed: ...name is paul and i really like computers. I just finished cutting the grass, its is really hot where i live...
|
|
|
 |
usaboy
Newbie
Joined: 05 July 2003
Status: Offline
Points: 9
|
Post Options
Thanks(0)
Quote Reply
Posted: 05 July 2003 at 1:49pm |
Hi Paul... yes.. that's EXACTLY what I want to do in my ASP (VBscript) page.
Edited by usaboy
|
 |
Gullanian
Senior Member
Joined: 04 January 2002
Location: England
Status: Offline
Points: 4373
|
Post Options
Thanks(0)
Quote Reply
Posted: 05 July 2003 at 4:33pm |
I think its inStr which returns the position of the keyword, IE
strKeyword = "finished" intCharacterPositionOfKeyWord = inStr(strEntireLine,strKeyword)
Might return for example 50. I think the easiest way then would be to do a simple loop, going through each character of the sentence one way then the other, and everytime it comes accross a space bar it adds one to the counter, then the loop terminates when its hit 10 spaces, if that makes sense! Coz im bored and feeling nice ill do an example:
strKeyword = "finished" strSentence = "Hello world my name is paul and i really like computers. I just finished cutting the grass, its is really hot where i live"
intPositionToStartFrom = inStr(strEntireLine,strKeyword)
If intPositionToStartFrom <> 0 then
intCharLoopCount = intPositionToStartFrom
'Get the right 10 words Do until intWordcount = 10 If mid(strEntireLine,intCharLoopCount,1) = " " then intWordCount = intWordCount + 1 end if strToTheRight10Words = strToTheRight10Words + (mid(strEntireLine,intCharLoopCount,1)) intCharLoopCount = intCharLoopCount + 1 Loop
intCharLoopCount = intPositionToStartFrom
'Get the left 10 words Do until intWordcount = 10 If mid(strEntireLine,intCharLoopCount,1) = " " then intWordCount = intWordCount + 1 end if strToTheLeft10Words = strToTheLeft10Words + (mid(strEntireLine,intCharLoopCount,1)) intCharLoopCount = intCharLoopCount - 1 Loop
end if
|
of course this is really buggy I guess, havent got enough time to check it but should help you along
Edited by Gullanian
|
 |
usaboy
Newbie
Joined: 05 July 2003
Status: Offline
Points: 9
|
Post Options
Thanks(0)
Quote Reply
Posted: 06 July 2003 at 7:06am |
|
hmmm tanx Gullanian.. do u think we need to reset the intWordcount to 0 before starting the second loop??? btw... this didn't work for me :( i really need help on doing this.. I hope Paul gives some tips on that...
|
 |
pmormr
Senior Member
Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
|
Post Options
Thanks(0)
Quote Reply
Posted: 06 July 2003 at 9:59am |
many search engines use trimming scripts, did you try contacting them and asking them how they did it? Some search engine is bound to give you some help...
|
|
|
 |
pmormr
Senior Member
Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
|
Post Options
Thanks(0)
Quote Reply
Posted: 06 July 2003 at 10:00am |
|
i'm going on vacation in two hours... good luck!
|
|
|
 |
ljamal
Mod Builder Group
Joined: 16 April 2003
Status: Offline
Points: 888
|
Post Options
Thanks(0)
Quote Reply
Posted: 06 July 2003 at 10:49am |
|
The way I would do this is to limit it by characters not words. Set it to like 100 before the word and 100 character from the beginning of the word.
function FocusKeyword (strSting, strKeyword)
dim intPosition, intLength
intLength = 100
' get keyword's position in the string
intPosition = inStr(strString, strKeyword)
'truncate characters before keyowrd to intLength
if intPosition > intLength then
strString = Right(strString, len(strString)-intLength)
intPosition = inStr(strString, strKeyword)
end if
' truncate characters after the beginning of the keyword to intLength
if len(strString) > intPosition + intLength then
strString = Left(strString, intPosition+intLength)
end if
'truncate to the first space in the string
strString = right(strString, len(strString)-Instr(strString," "))
'truncate to the first space and the end of the string
strString = left(strString, inStrRev(strString))
'highlight keyword
strString = Replace(strString, strKeyword, "<b>"& strKeyword &"</b>")
'return string
FocusKeyword = strString
end function
I didn't test the function so it may not be perfect, but it will give to the general idea.
|
|
|
 |