how do I parse the words of a sentence?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mike

    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

  • Aaron Bertrand [SQL Server MVP]

    #2
    Re: how do I parse the words of a sentence?

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

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

    ....



    "mike" <mike_newsgroup s@yahoo.comwrot e in message
    news:1159303854 .396503.82760@d 34g2000cwd.goog legroups.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
    >

    Comment

    • mike

      #3
      Re: how do I parse the words of a sentence?


      Aaron Bertrand [SQL Server MVP] wrote:
      well, you could brute force replace all punctuation with empty string before
      performing the split:
      >
      sentence = replace(sentenc e, ",", "")
      sentence = replace(sentenc e, ".", "")
      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

      Comment

      • Dave Anderson

        #4
        Re: how do I parse the words of a sentence?

        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:


        Regular Expression Syntax (useful in both languages):




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


        Comment

        • mike

          #5
          Re: how do I parse the words of a sentence?


          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:

          Ifonlythiswould workIwouldbehap pyWellmaybenot

          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

          Comment

          • Mike Brind

            #6
            Re: how do I parse the words of a sentence?


            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:
            >
            Ifonlythiswould workIwouldbehap pyWellmaybenot
            >
            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,
            ilovejackdaniel s...etc):

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

            function ereg_replace(st rOriginalString , strPattern, strReplacement)
            dim objRegExp : set objRegExp = new RegExp
            objRegExp.Patte rn = strPattern
            objRegExp.Ignor eCase = True
            objRegExp.Globa l = True
            ereg_replace = objRegExp.repla ce(strOriginalS tring, strReplacement)
            set objRegExp = nothing
            end function


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

            --
            Mike Brind

            Comment

            • mike

              #7
              Re: how do I parse the words of a sentence?


              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.

              Comment

              Working...