[AS3][CONCEPT TEST] Text Prediction

Well this came about in [this thread] about how to predict a word, but not have it match letter combinations/patterns that are in the middle of a word. I didn’t want to keep cluttering that thread because I don’t know if I even helped that member with their problem, so I’m starting this here. This “project” is allowing me to get back into Actionscript since it’s been a while so it serves no purpose for me other than a personal challenge.

So anyway, I started to think about how to work this out and I got the idea to take the words stored in the array and cut them down to the first X amount of characters that are typed into the input box. What this does is compares the typed word with the word in the array as if they were the same amount of characters and takes the words that match those letters and add them to a separate array which would be where the possible predicted words would be stored.

Example:
I type “the” in the input box and any words that start with the letters “the” get added to the possible predictions and any words that don’t get ignored. So the word “thumb” would be ignored as it starts with 'thu", but the word “theory” would be added.

After getting a crude base working the awesome Krilnon taught me about using Array.filter() and Regular Expressions (RegExp) in Flash. That helps compact/simplify my script a little bit. I’ve since taken my script and Krilnon’s script and have been expanding on this concept and decided I will keep track of my updates here and post the current version as an attachment for those interested (with a limited “dictionary” that can be modified). If I keep working on this my hopes is to have the ability to auto-complete or click to replace the word, but I’m not sure if I’m at that ability just yet. I also want to see about auto-capitalizing the first letter if it’s the beginning of a sentence, but again, we’ll cross these bridges when we get to it. I have ideas on how to go about it, I just have to figure out if and how I can implement them.

I hope this is useful for someone out there. And if not, at least I’m learning something :smiley:

Changelog:

Version 3.5: (Current/Attached)

  • Added: automatically changes the case of the first letter depending on how the user types it (changing case after a period to indicate start of sentence will come once I fix another bug that effects it)
  • Added auto-fill suggestion by adding a textfield under the input field to display the first word in the prediction array (as this will be the closest word to what is typed so far)
  • Added auto-complete when you press the space bar and there is an auto-fill available (buggy)
  • Bug: Pressing spacebar when there is an auto-fill available will always automatically use the auto-fill even if it’s not what you wanted. Only way I can think to prevent this is to manually add that word to the wordList array. Will probably switch to alternate method in later version.
  • Bug: For some reason all punctuation breaks the prediction (which is good) except a period (which is bad). For Example: Typing “Ap,” or “Ap!” gives me no predictions, but typing “Ap.” still gives me predictions for all words that start with “Ap” and “Ap.l” gives me “Apple”. I’m assuming it’s something in regex since the “.” has to be escaped otherwise it counts as “any character”, but everything I have tried to escape it has been unsuccessful. UPDATE: I couldn’t figure out how to fix it via regex, so I use replace(".", “\.”) to replace the period with an escaped period and it works like a charm.

[SIZE=2]Note: Admittedly this version is rather hacked together. I ran into quite a few issues implementing the features added so much of it is workarounds to get them to work. Coupled with frustration that AS3 is so strict (which is good, but I’m not used to it yet) so the AS panel tells me the code is error free, but when I would run it I would get a slew of errors in the output panel. I’m hoping with practice and keeping at it I will adjust and be able to streamline this better.[/SIZE]

Version 3:

  • Added support for multiple words
  • Added support for backspacing (also works backspacing between words)
  • Modified the script to run only if you are at least two letters into the word. This fixed a bug where the entire list of words for that letter presented itself when only that letter was typed.
  • Moved word list array to external .as file

Version 2:

  • Integrated Krilnons Array.filter() and RegExp method in with my own
  • Added support for multi-dimensional arrays to store words for each letter of the alphabet in a separate array
  • Bug: Still only support for one word in the input box.

Version 1:

  • Crude base only.
  • Written in AS2 then up-converted to AS3.
  • Worked with only one array and one word in the input box.