Skip to main content
Known Participant
April 9, 2009
Question

How to count words?

  • April 9, 2009
  • 1 reply
  • 4217 views

Hello, namless hero~

Just like the title said, if you has any idear, please reply this post, thanks!!

This topic has been closed for replies.

1 reply

Adobe Employee
April 9, 2009

There are two basic ways I can think of to do this. Either get the text, and run your own word count alogorithm on it, or use the word boundary methods in ParagraphElement. In the first instance, you have more control over what is considered as a word, and in the second way you let the Player decide.

Option 1:

You can get the text of the TextFlow by exporting using the plain text filter:

     var text:String = TextFilter.export(textFlow, PLAIN_TEXT_FORMAT, ConversionType.STRING_TYPE) as String;

Once you have the text, just scan through it to find the words. This is the best way if you want to define for yourself what constitutes a word.

If there is going to be a lot of text, you may prefer to look at it paragraph by paragraph. So instead you would loop through the paragraphs in the flow, and call getText() on each one, and then run your algorithimn on the String returned by getText().

Option 2:

Otherwise, you could iterate through the paragraphs of the flow, looking for word boundaries. This will get you all possible word boundaries, including between spaces. To do this, it would look something like this:

     var paragraph:ParagraphElement = textFlow.getFirstLeaf().getParagraph();

     do {

          var relativePosition:int = paragraph.findWordBoundary(0);

          while (relativePosition < paragraph.textLength)

          {

               trace("Word boundary at", paragraph.getAbsoluteStart() + relativePosition);

               relativePosition = paragraph.findWordBoundary(relativePosition);

          }

          paragraph = paragraph.getNextParagraph();

     } while (paragraph != null);

I haven't compiled this or debugged it, and I coded it from memory, but hopefully this will serve as a guideline for what you could do.

maniChinaAuthor
Known Participant
April 10, 2009

Thanks!!![?]