10 Replies Latest reply on May 16, 2007 1:16 PM by Newsgroup_User

    Fast find words in text

    walfridson Level 1
      Hey all
      Got a big text, I want to detect some words in it and change the color of those words.
      Tried doing a repeatthing like
      repeat with i=1 to mytext.char.count
      testword = testword + mytext.char.count
      is testword a specialword then change color of those chars
      and so on...

      Not very fast when having a large text!
      The string handler finds the words very fast but I cant seem to return charnumbers - so I can change the color...

      Any ideas how to do this, any whole other way maybe?

      No 200$ xtras please ;)

      cheers
        • 1. Re: Fast find words in text
          Level 7
          You could try the offset() function instead of searching char by char.
          However, this will require that you start with a duplicate of your
          original string since offset will only find the first match (match in
          your duplicate, get the first char from the offset function, delete from
          char 1 to offset + matching_string_length, keep a running total of chars
          deleted, repeat until offset returns 0)
          Or you could use a regular expression and the PRegEx xtra (free) to find
          a match and obtain the offset
          • 2. Re: Fast find words in text
            Level 7
            By far the fastest text searching xtra is pRegEx. It is not terribly
            easy to use if you are not familiar with regular expressions, but that
            is the fastest way to do it. And it's free.

            http://openxtras.org/pregex/
            • 3. Re: Fast find words in text
              Level 7
              Here's a handler that uses pRegEx to get the match positions:
              --
              on mGetMatchPositions aString, aMatch
              lString = [aString]
              lReturn = []
              if PRegEx_SearchBegin(lString, aMatch, "g") > 0 then
              repeat while (pRegEx_SearchContinue() > 0)
              lReturn.append( PRegEx_GetPos() )
              end repeat
              end if
              return lReturn
              end

              From the message window:
              t = "This is a string of text"
              put mGetMatches(t, "t")
              -- [1, 12, 21, 24]
              The returned list indicates the position of the /last/ character in a
              match. Since you provide the match string, you know how long it is, so
              you can iterate through the list returned subtracting the length of the
              string matched and setting the colour of this chunk
              --
              on mSetColour aMember, sMatch, aColour
              -- housekeeping:
              if aMember.type <> #text then exit
              if stringP(sMatch) = 0 then exit
              if ilk(aColour, #color) = 0 then exit

              lMatched = mGetMatchPositions( aMember.text, sMatch )
              len = length(sMatch)
              repeat with aPosition in lMatched
              aMember.char[(aPosition - len + 1)..aPosition].color = aColour
              end repeat
              end
              • 4. Re: Fast find words in text
                Lukewig Level 1
                HI,

                Hard to answer without knowing:
                - How many 'special words' do you have, and could they be defined using a pattern or do they require a 'dictionary lookup'.
                - Is this for Shockwave or Projector?
                - Is the text dynamic or can 'pre markup' the text

                -- Luke
                • 5. Fast find words in text
                  walfridson Level 1
                  Thanks all!
                  Luke, its javascript plus some more words and characters. Projector.

                  Thanks for all your snippets Sean, Im all ready to give pReg a try!


                  While I all have you here, couldnt find any thread about this...
                  Is it possible to make the projector associated application for a extension. The lingo stuff Im worried about.
                  I dont have buddy api...

                  edit: let me make that clearer... so projector reads the clicked file when launched.
                  • 6. Re: Fast find words in text
                    Level 7
                    > Is it possible to make the projector associated application for a extension.
                    > I dont have buddy api...

                    Yes. You'll need Buddy API (or similar) for registry access. Check out:
                    < http://director-online.com/buildArticle.php?id=1146>
                    • 7. Fast find words in text
                      Lukewig Level 1
                      Hi,

                      Since it is a projector, definitely look at PregEx Xtra (its fast). Have a look at the PRegEx_ReplaceExec method - it will let you make callback on each match. So you would use a regular expression to isolate potential matches, and then a lookup on each match to confirm it is a key word -- Something like this:

                      TextMember = member("foo") -- this is a text member containing the text to process
                      script("Colourise").ProcessTextMember(TextMember)


                      • 8. Re: Fast find words in text
                        walfridson Level 1
                        Thanks again!

                        Are there any good benefits by converting to html and back?
                        • 9. Re: Fast find words in text
                          walfridson Level 1
                          Preg is working great now, very smooth

                          Looked through the docs and cant find any info how to detect for ex */
                          like "((*/)|(/*))" if it was easy :)
                          • 10. Re: Fast find words in text
                            Level 7
                            > Preg is working great now, very smooth
                            >
                            > Looked through the docs and cant find any info how to detect for ex */
                            > like "((*/)|(/*))" if it was easy :)

                            The xtra has a method for escaping special/reserved characters:
                            put re_quotemeta("\*")
                            -- "\\\*"