Predictive Text...

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

cmhjon
Posts: 190
Joined: Tue Aug 04, 2015 11:55 am

Predictive Text...

Post by cmhjon » Mon Mar 10, 2025 1:39 pm

Hi everyone,

I have a hidden text field which contains a list of my customers (it’s lengthy!). I have another field where a user types the name of the customer. I’d like my app to display a list of possible customers as the user types the name of the customer. I’d like it to work in the same way as typing something into the URL field of a web browser in that a field containing possible customers is displayed just below the text field the user is typing the customer in that the user can could click a customer name in the below field and have it populate the text field above. Ideally, the list field would dynamically resize itself vertically to show only the list of possible customers as the user types.

I’m not sure how to accomplish this. Can anyone provide some code on how to achieve this?

Thank you and best regards,
Jon :-)
Last edited by cmhjon on Mon Mar 10, 2025 3:26 pm, edited 2 times in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10077
Joined: Fri Feb 19, 2010 10:17 am

Re: Predictive Text...

Post by richmond62 » Mon Mar 10, 2025 2:42 pm

SShot 2025-03-10 at 15.39.57.jpg
Attachments
Predictivity.livecode.zip
Stack.
(1.13 KiB) Downloaded 486 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Predictive Text...

Post by dunbarx » Mon Mar 10, 2025 3:13 pm

Richmond.

Could not get your stack to work.

cmhjon

Try this stack. It begs for massive enhancement. Anything longer than two chars that you type into the "type here" field will list all possibles in the"results" field. You might start playing here by typing "emi". It finds strings of any sort, however they exist in each line in "raw data".
Binary Search.livecode.zip
(1.33 KiB) Downloaded 464 times
Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Predictive Text...

Post by FourthWorld » Mon Mar 10, 2025 5:32 pm

The filter command is a natural fit this. For resizing the field, see the formattedHeight function.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

cmhjon
Posts: 190
Joined: Tue Aug 04, 2015 11:55 am

Re: Predictive Text...

Post by cmhjon » Tue Mar 11, 2025 2:15 pm

Hi Richmond,

Thank you the sample stack! Although it works, could you expand the code so that the list of customers updates accordingly when the user presses the delete/backspace key?

Thank you so much and best regards,
Jon :-)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10077
Joined: Fri Feb 19, 2010 10:17 am

Re: Predictive Text...

Post by richmond62 » Tue Mar 11, 2025 6:02 pm

I'll have a go, but not until tomorrow as a bit busy at the moment. 8)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Predictive Text...

Post by dunbarx » Tue Mar 11, 2025 7:39 pm

cmJohn.

My posted stack, "Binary Search", already does that. What is it missing?

The process simply takes existing text in the "Type Here" field and filters the main database. It does not care how the text is created (or destroyed).

My stack was simply to demonstrate a particular method. Richard makes the point that the "filter' command can also be exploited. All that is up to you...

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Predictive Text...

Post by dunbarx » Tue Mar 11, 2025 8:35 pm

Richmond.

I tried your stack "predictivity" again but I still could not get it to work. So I changed the field handler to "keyDown" and it does. Interestingly, "keyUp" does not. I could not trap that message.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10077
Joined: Fri Feb 19, 2010 10:17 am

Re: Predictive Text...

Post by richmond62 » Tue Mar 11, 2025 9:40 pm

That is odd.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10077
Joined: Fri Feb 19, 2010 10:17 am

Re: Predictive Text...

Post by richmond62 » Wed Mar 12, 2025 3:52 pm

I am assuming by "delete key" you mean the key surrounded by red rather than the key surrounded by yellow:
-
Keys.png
Keys.png (103.8 KiB) Viewed 7264 times
-

Code: Select all

on keyUp 
   
end keyUp

on keyDown KP
   put KP after fld "ZZZ"
   put empty into fld "xlist"
   put fld "ZZZ" into XYZ
   put the number of chars in XYZ into KKK
   put 1 into LYNE
   repeat until line LYNE of fld "slist" is empty
      put char 1 to KKK of line LYNE of fld "slist" into ZLIST
      if ZLIST contains XYZ then
         put  line LYNE of fld "slist"  & cr after fld "xlist"
      end if
      add 1 to LYNE
   end repeat
end keyDown

on backspaceKey
   delete the last char of fld "ZZZ"
   put empty into fld "xlist"
   put fld "ZZZ" into XYZ
   put the number of chars in XYZ into KKK
   put 1 into LYNE
   repeat until line LYNE of fld "slist" is empty
      put char 1 to KKK of line LYNE of fld "slist" into ZLIST
      if ZLIST contains XYZ then
         put  line LYNE of fld "slist"  & cr after fld "xlist"
      end if
      add 1 to LYNE
   end repeat
end backspaceKey
This is called in LiveCode the 'backspace key'.

I have changed 'things' from keyUp to keyDown to keep some people hereabouts happy. 8)
Attachments
Predictivity.livecode.zip
(1.21 KiB) Downloaded 536 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10077
Joined: Fri Feb 19, 2010 10:17 am

Re: Predictive Text...

Post by richmond62 » Wed Mar 12, 2025 3:55 pm

If you wanted to be 'fancy' and consume some more processor cycles you could make the output field expand and contract vertically to accommodate the number of items.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Predictive Text...

Post by Klaus » Wed Mar 12, 2025 4:09 pm

Please use this much shorter and less cumbersome script for the field "slist".
No LOOP neccessary and FILTER is your friend here!

Code: Select all

on keyDown KP
   put KP afterme
   put fld "slist" into tList
   put me into tLookupString
   filter tList with (tLookupString & "*")
   if tList = EMPTY then
      beep
      put EMPTY into fld "xlist"
      exit to top
   end if
   put tList into fld "xlist"
 nd keyDown

on backspaceKey
   delete the last char of me
   send ("keydown" & "") to me
end backspaceKey
8)

Best

Klaus

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Predictive Text...

Post by Klaus » Wed Mar 12, 2025 4:29 pm

Here a more user-friendly and "real-life" version of the stack.
Click a line in the field that will appear and see what I mean.
Predictivity_KM.livecode.zip
(1.38 KiB) Downloaded 486 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Predictive Text...

Post by dunbarx » Wed Mar 12, 2025 5:26 pm

Klaus

I wanted to see if the filter version that you posted was faster than the loop version I did. It is, but not by much. I tried a dataSet with 500,000 lines. For each letter typed, my version took 0.9 seconds to find all hits and the filter version took 0.6 seconds.

My dataSet was built with about a dozen different names, repeated rather often to create a long list. Because of that I needed to add a duplicate killer:

Code: Select all

if me is in tLine and tline is not in fld 2 then put tLine & return after fld 2
I have no intention of building a real dataSet, but I may get a slight speed increase if that line was not required.

Anyway, filter is faster. Mine is shorter. :wink:

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Predictive Text...

Post by dunbarx » Wed Mar 12, 2025 6:26 pm

Just redid my stack with a random dataSet of 500,000 lines. For some reason, now I see far less speed difference between the two methods.

Filter is still slightly faster. Mine also is not limited to the beginning chars in a line, rather it "finds chars"

Mine is still shorter, and does not need a backSpace key handler.

This is so much better than doing my job.

Craig

The attached stack has only 30,000 lines so I could at least upload it. It has both handlers available.
Binary Search3.livecode.zip
(224.62 KiB) Downloaded 247 times
Last edited by dunbarx on Wed Mar 12, 2025 6:39 pm, edited 1 time in total.

Post Reply