472,119 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

how do I parse the words of a sentence?

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

Sep 26 '06 #1
6 1896
well, you could brute force replace all punctuation with empty string before
performing the split:

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

....

"mike" <mi*************@yahoo.comwrote in message
news:11*********************@d34g2000cwd.googlegro ups.com...
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

Sep 26 '06 #2

Aaron Bertrand [SQL Server MVP] wrote:
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

Sep 26 '06 #3
mike wrote:
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[i] + "\">" + a[i] + "</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...b8ed696d0a.asp

Regular Expression Syntax (useful in both languages):
http://msdn.microsoft.com/library/en...6f58358c0e.asp

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.
Sep 26 '06 #4

Dave Anderson wrote:
mike wrote:
Hello, I am trying to write some code to parse a sentence and
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

Sep 26 '06 #5

mike wrote:
Dave Anderson wrote:
mike wrote:
Hello, I am trying to write some code to parse a sentence and

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.")

--
Mike Brind

Sep 27 '06 #6

Mike Brind wrote:
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.

Sep 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Mosher | last post: by
4 posts views Thread by Andrew E | last post: by
7 posts views Thread by HumanJHawkins | last post: by
46 posts views Thread by vvk4 | last post: by
reply views Thread by PLENI SELENE | last post: by
AdrianH
5 posts views Thread by AdrianH | last post: by
AdrianH
1 post views Thread by AdrianH | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.