I am trying to write a program where the user types on the keyboard in a specified location, and Mathematica outputs what you are currently typing, plus a prediction that is updated in real time of what you are going to type next. I have done everything I thought I had to do, and it is working partially in that the variable labeled "oracle" is updated dynamically, but I can't seem to figure out how to get the prediction to update. Can anyone help me or give advice?

EventHandler["click mouse here and type",
{{"KeyDown", "a"} :> {human = StringJoin[human, "a"],
 guess = StringJoin[guess, oracle]},
 {"KeyDown", "f"} :> {human = StringJoin[human, "f"], 
  Dynamic[guess = StringJoin[guess, oracle]]}


The rest of the code works as I expect, and gives the correct value as I type. The problem is that every time I type in a new letter to update the "human" input slot, the "guess" output slot stays blank. The guess is supposed to update every time I push the "a" or "f" key, by adding the oracle/prediction to the end of the guess string.

analysis[input_, n_] := 
  If[StringLength[input] >= n && ! (StringLength[input] == 0 ) ,
  Tally[StringPartition[input, n, 1]],{}]

(*This lists all the substrings of length 'n' of the input, 
 and tallies how much each substring occurs*) 

  tree = 
      If[! analysis[human, n] == {}, SortBy[analysis[human, n], Last], {}], 
      {n, 1, 5}]]

   branch = 
         StringTake[#[[1]], n - 1] == StringTake[human, -n + 1] &], 
       {n, 1, 5}]]

   oracle =
     If[! branch[[5]] == {}, StringTake[First[Last[branch[[5]]]], -1],
      If[! branch[[4]] == {}, StringTake[First[Last[branch[[4]]]], -1],
        If[! branch[[3]] == {}, StringTake[First[Last[branch[[3]]]], -1],
          If[! branch[[2]] == {}, StringTake[First[Last[branch[[2]]]], -1],
            If[! branch[[1]] == {}, StringTake[First[Last[branch[[1]]]], -1],

1 Answer 1


I can't understand your code. There are too many syntax errors and undefined variables -- it has no chance of running. Further, you show little understanding of the Dynamic front-end expression wrapper.

However, I want to do more than just scold you, so here is some code that implements an extremely simple event handler that actually works.

Simple oracle -- it guesses the next character is the one that has been typed the most in the past.

oracle[] :=
  Module[{acnt, fcnt},
    {acnt, fcnt} = {StringCount[human, "a"], StringCount[human, "f"]};
    If[acnt > fcnt, "a", "f"]]

Up-dater -- makes the event handler more succinct.

update[chr_] :=
  (human = human <> chr;
   guess = guess <> oracle[];
   text = text <> chr)

human = ""; 
guess = "";
prompt = "click mouse here and type:";
text = "";
EventHandler[Dynamic[prompt <> text],
  {{"KeyDown", "a"} :> update["a"],
   {"KeyDown", "f"} :> update["f"]}]

click mouse here and type:aaaafffaaafffffaaaafff




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.