Page 1 of 4

Predictive Text...

Posted: Mon Mar 10, 2025 1:39 pm
by cmhjon
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 :-)

Re: Predictive Text...

Posted: Mon Mar 10, 2025 2:42 pm
by richmond62
SShot 2025-03-10 at 15.39.57.jpg

Re: Predictive Text...

Posted: Mon Mar 10, 2025 3:13 pm
by dunbarx
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 466 times
Craig

Re: Predictive Text...

Posted: Mon Mar 10, 2025 5:32 pm
by FourthWorld
The filter command is a natural fit this. For resizing the field, see the formattedHeight function.

Re: Predictive Text...

Posted: Tue Mar 11, 2025 2:15 pm
by cmhjon
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 :-)

Re: Predictive Text...

Posted: Tue Mar 11, 2025 6:02 pm
by richmond62
I'll have a go, but not until tomorrow as a bit busy at the moment. 8)

Re: Predictive Text...

Posted: Tue Mar 11, 2025 7:39 pm
by dunbarx
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

Re: Predictive Text...

Posted: Tue Mar 11, 2025 8:35 pm
by dunbarx
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

Re: Predictive Text...

Posted: Tue Mar 11, 2025 9:40 pm
by richmond62
That is odd.

Re: Predictive Text...

Posted: Wed Mar 12, 2025 3:52 pm
by richmond62
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 7314 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)

Re: Predictive Text...

Posted: Wed Mar 12, 2025 3:55 pm
by richmond62
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.

Re: Predictive Text...

Posted: Wed Mar 12, 2025 4:09 pm
by Klaus
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

Re: Predictive Text...

Posted: Wed Mar 12, 2025 4:29 pm
by Klaus
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 487 times

Re: Predictive Text...

Posted: Wed Mar 12, 2025 5:26 pm
by dunbarx
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

Re: Predictive Text...

Posted: Wed Mar 12, 2025 6:26 pm
by dunbarx
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