how do I parse the words of a sentence?

M

mike

Hello, I am trying to write some code to parse a sentence and hyperlink
just the words in it. I used Aaron's code from an earlier question as
a start.

So far, all the code does below is hyperlink everything separated by a
space, which means stuff like "work." "happy." "Well;" "not." from the
sentence become hyperlinks (whereas im interested in just the words
themselves becoming hyperlinks and the punctuation staying
nonhyperlinks). Anyone know how to do it?

sentence = "If only this would work, I would be happy. Well; maybe
not."

qwords = split(sentence)

for j = 0 to ubound (qwords)
response.write "<a href=""default.asp?q="
response.write qwords(j)
response.write """>"
response.write qwords(j)
response.write "</a> "
next
 
A

Aaron Bertrand [SQL Server MVP]

well, you could brute force replace all punctuation with empty string before
performing the split:

sentence = replace(sentence, ",", "")
sentence = replace(sentence, ".", "")

....
 
M

mike

Aaron said:
well, you could brute force replace all punctuation with empty string before
performing the split:

sentence = replace(sentence, ",", "")
sentence = replace(sentence, ".", "")

Hi Aaron,

I was hoping to be able to write out the entire sentence with just the
words hyperlinked. If I replace all the punctuation with nothing, I'll
lose all the punctuation when I go to print the sentence out. Is there
a way around this?

-Mike
 
D

Dave Anderson

mike said:
Hello, I am trying to write some code to parse a sentence and
hyperlink just the words in it. I used Aaron's code from an
earlier question as a start.

So far, all the code does below is hyperlink everything separated
by a space, which means stuff like "work." "happy." "Well;" "not."
from the sentence become hyperlinks (whereas im interested in just
the words themselves becoming hyperlinks and the punctuation
staying nonhyperlinks). Anyone know how to do it?

Here are a couple of ways.

In JScript:
===========
var Str = "If only this would work, I would be happy. Well; maybe not.",
a = Str.split(/\W+/)
for (var i=0; i<a.length; i++) Response.Write(
"<a href=\"default.asp?q=" + a + "\">" + a + "</a>"
)



In VBScript:
============
Dim Str, RX, Matches, Match

Set RX = New RegExp
RX.Pattern = "\w+"
RX.Global = True

Str = "If only this would work, I would be " & _
"happy. Well; maybe not."
Set Matches = RX.Execute(Str)

For Each Match in Matches
Response.Write("<a href=""default.asp?q=" &_
Match.Value & """>" & Match.Value & "</a>")
Next

RegExp Object:
http://msdn.microsoft.com/library/en-us/script56/html/05f9ee2e-982f-4727-839e-b1b8ed696d0a.asp

Regular Expression Syntax (useful in both languages):
http://msdn.microsoft.com/library/en-us/script56/html/ab0766e1-7037-45ed-aa23-706f58358c0e.asp
 
M

mike

that works good, but the result looks like this:

IfonlythiswouldworkIwouldbehappyWellmaybenot

is there any way to somehow save the characters between tokens so that
the sentence can be displayed as it was originally (with punctuation
and spaces), but with the words only hyperlinked?

-Mike
 
M

Mike Brind

mike said:
that works good, but the result looks like this:

IfonlythiswouldworkIwouldbehappyWellmaybenot

is there any way to somehow save the characters between tokens so that
the sentence can be displayed as it was originally (with punctuation
and spaces), but with the words only hyperlinked?

VBScript:

Adapted from a bit of code knocking around on a few sites (4guys,
ilovejackdaniels...etc):

function create_links(strText)
strText = ereg_replace(strText, "(\w+)", "<a
href=""default.asp?q=$1"">$1</a>")
create_links = strText
end function

function ereg_replace(strOriginalString, strPattern, strReplacement)
dim objRegExp : set objRegExp = new RegExp
objRegExp.Pattern = strPattern
objRegExp.IgnoreCase = True
objRegExp.Global = True
ereg_replace = objRegExp.replace(strOriginalString, strReplacement)
set objRegExp = nothing
end function


Response.Write create_links("If only this would work, I would be " & _
"happy. Well; maybe not.")
 
M

mike

Mike said:
mike wrote:

Mike this works incredibly well, thanks! Is there a way to exclude
certain words from being converted? For example, the words "the" and
"a" don't need a hyperlink.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top