Download: Flash Text Search Engine Class

Downloads, Classes - July 10

Create text search engines in ActionScript easily using this versatile and lightweight class. It can be used in a simple way, or with expert features such as excluded characters, case sensitivity, etc.

download TextSearchEngine Class (detailed instructions in Class file)

Lately, I had to make a large CD-Rom containing archives for several years of a monthly magazine. Of course, the client wanted a full text search engine on the whole archive! This gave me the idea to make a tiny class to perform search over textfields easily and quickly.

A Cute Little Flash Search Engine

Please take a look at the following example. Pushing "find exact" you will search for the exact sentence. Pushing "find all" you will search for each word in the search key. The "Case Sensitive" check box speaks by itself :)
Feel free to paste any text in the window and check the speed of searches.


TextSearchEngine Explained

TextSearchEngine is a static class, thus doesn't need to be instantiated. To perform a serach on a TextField, and let the Class manage text formatting, it as simple as typing:

PippoFlash.TextSearchEngine.findExact({key:"find me", textField:myTextField});

This is what comes out just typing the above line of code (only 1.5Kb)...

download source code (fla & ActionScript)
All TextSearchEngine accept a single parameters' Object (read explanatory post). The Class can work directly on a TextField and perform highlights, or return search results in a simple feedback object very easy to manage.

Features

-> Search for entire phrase or each word
-> Case sensitive / unsensitive
-> Exclude search exact words
-> Set forbidden characters
-> Set minimum search term length
-> Set TextFormat for original text, and for search results
-> Set parameters once, and proceed with searches
-> Returns verbose object with all needed search results

Methods

setupSearchParameters(params);

This command sets up all parameters than can remain set. This command can be skipped, because all these parameters can be also set with the findAll() and findExact() methods. Accepted parameters are:

minWordSize - NUMBER - The minimum length of the search key. If a term is shorter than this, the Class doesn't look for it and returns a message.
textField - TextField- A reference to a TextField instance containing the text to be searched (in alternative, use "text");
text - STRING - If no textField is defined, a string can be inputed directly.
mainTextFormat - TextFormat - The text format to set plain text pefore a search (if is not defined, the original TextFormat of textField is used)
foundTextFormat - TextFormat - The text format to set found words in the defined textField. (If not defined, words are coloured in red).
excludeKeys - STRING - A space separated list of terms to be excluded from search. (If searching an HTML text, it is better to exclude html text. To do this you can set this string to: "font size color table div ..."); This excludes only EXACT terms.
excludeChars - STRING - A space separated list of forbidden chars. (If searching an HTML text, it is better to exclude html soecial characters. To do this you can set this string to: "<> ! \" ' ? / ..."); This excludes search terms which CONTAIN the char, or the word in this list. Remeber to put spaces or it will be treated as a single word.

findExact(params);

This command finds the EXACT phrase set in params.key. This command can be launched with only 1 or 2 parameters, but all other parameters setted in setupSearchParameters() can be overwritten here. Here are the parameters:

textField - TextField - A reference to a TextField instance containing the text to be searched (in alternative, use "text"); This can be skipped if already set with setupSearchParameters().
key - STRING - The search key. It can be a single word or a sentence.
matchCase - BOOLEAN - If set to true, the search will be case sensitive.
*REMEMBER* All parameters can be re-setted with this function.

Returned Object for findExact();

The Class returns an object for each search. Performing an exact search:

var result = TextSearchEngine.findExact({key:"find me"});

The result Object will contain the following parameters:
message - STRING - A verbose message with the feedback from the class.
found - NUMBER - The amound of found results in text. (if 0, no results found)
key - STRING - The searched phrase.
positions - ARRAY - A list of numbers with the starting position in text of the search results.

findAll(params);

This command finds EACH SEPARATE WORD phrase set in params.key. This command too can overwrite parameters set with setupSearchParameters() Here are the parameters:

textField - TextField- A reference to a TextField instance containing the text to be searched (in alternative, use "text"); This can be skipped if already set with setupSearchParameters().
key - STRING - The search key. It can be a single word or a sentence. If a sentence, every word will be found.
matchCase - BOOLEAN - If set to true, the search will be case sensitive.
*REMEMBER* All parameters can be re-setted with this function.

Returned Object for findAll();

The Class returns an object for each search. Performing an exact search:

var result = TextSearchEngine.findExact({key:"find me"});

The result Object will contain the following parameters:
message - STRING - A verbose message with the feedback from the class.
found - NUMBER - The amound of found results in text. (if 0, no results found)
results - ARRAY - A list objects, like the ones returned by findExact(), with the search result for each word in the searched sentence.

Conclusion

When I was working on the searchable Cd-Rom archive, I developed this Class for my own usage, and already it turned out to be useful also for other purposes (last usage was for automatic word completion on user input).

The power of this class is in it's versatility: it can be used for the simplest task with just one line of code, or fit into a complex application to manage multiple searches with advanced features.

Managing text operations with flash can be tricky, and searching text in ActionScript gave headache to many developers. I will soon upload more text management classes very useful in the daily work.

Detailed instructions are present in the Class .as file itself.

download TextSearchEngine Class (detailed instructions in Class file)

Please feel free to use TextSearchEngine and let me know if it helped :)

Namaste
Pippo

Flash, ActionScript, OOP, object oriented programming, freelance flash developer, actionscript developer, flash programmer, flasher, rich internet applications developer, ria, flash games, flash 3D, web applications, ActionScript 2.0, free actionscript classes, swf, free flash downloads, free flash components, flash tutorials, actionscript tutorials, flash developer beijing, programmatore flash, sviluppatore flash, sviluppatore flash italia, sviluppatore flash roma, sviluppatore flash italiano, freelance flash developer, Flash, ActionScript, actionscript, OOP, object oriented programming, freelance, actionscripter, programmer, freelance flasher, actionscript developer, rich internet applications, ria, flash games, 3D, web applications, interface developer, ActionScript 2.0, classes, swf, useability, usability, free stuff, free code, free classes, Flash Components, Flash Developer, Flash Games developer, Flash MX 2004, Flash 8, Flash Developer in Italy, Italian Flash developer, Flash Sviluppatore roma, Flash Developer Beijing, flash developer china, flash Developer Bangkok