Message Boards Message Boards

[WSS22] Develop heuristics for bracket autoclosing in inputs

POSTED BY: Xinyu Luo
2 Replies

enter image description here -- you have earned Featured Contributor Badge enter image description here Your exceptional post has been selected for our editorial column Staff Picks http://wolfr.am/StaffPicks and Your Profile is now distinguished by a Featured Contributor Badge and is displayed on the Featured Contributor Board. Thank you!

POSTED BY: EDITORIAL BOARD

What is the role of Hold & SetDelayed in function design?

Yeah it's such a generous, crowd-sourcing community with so many examples for Sanity Check, the Argument Check, and the Low Weighted (Likelihood) Cases Heuristic Check. You've got manipulations of every keyword in the re-filled string, that's amazing!

candidateScoreList = candidateScoreExptLeaves /@ listRefillStringPassSyntaxQ

Candidate Score List

Implicated Multiplication Check

1000 examples. That's a lot of examples, well off the trodden path. Although it looks like there's 3969 values... what are they?

exampleInputExpressions = 
 Import["~/Downloads/data/exampleInputExpressions.mx"]

This is like wrapping & nesting the ArcGIS Hub Geospatial JSON data configuration & format... and interchange; you wonder how inputs are universally connected to escape the quantitative, the equational & non-equational; but then why are all the quantities equal to 1 on the candidate score list.. It's been eye-opening real research. Thank you for this post.

validParenthesesDFS[n_] := validParenthesesDFS[n, n, ""]
validParenthesesDFS[0, 0, s_] := {s}
validParenthesesDFS[open_, close_, s_] := 
 Join[If[open > 0, 
   validParenthesesDFS[open - 1, close, s <> "("], {}], 
  If[close > open, validParenthesesDFS[open, close - 1, s <> ")"], {}]]
parenthesesList = validParenthesesDFS[3]
drawParentheses[s_] := 
  Module[{G, stack = {}, nodeList = {}, edgeList = {}}, 
   For[i = 1, i <= Length[s], i++, char = s[[i]];
    AppendTo[nodeList, Style[i, 20, Bold]];
    If[char == "(", 
     If[Length[stack] > 0, 
      AppendTo[edgeList, UndirectedEdge[Last[stack], i]]];
     AppendTo[stack, i], 
     AppendTo[edgeList, UndirectedEdge[Last[stack], i]];
     stack = Most[stack]]];
   G = Graph[nodeList, edgeList, 
     VertexStyle -> 
      Directive[RGBColor[0.75, 0, 0.8], EdgeForm[RGBColor[1, 0, 0]]], 
     VertexSize -> 0.2, 
     EdgeStyle -> Directive[Thick, RGBColor[1, 1, 0]], 
     VertexLabels -> "Name", GraphLayout -> "LayeredDigraphEmbedding",
      ImagePadding -> 20];
   G];
graphs = drawParentheses /@ Characters /@ parenthesesList;
Grid[Partition[graphs, UpTo[5]]]

Draw Parentheses

Grid Partition

While we aim to find the most probable positions for missing brackets, @Xinyu Luo at some point we might think that we have the desire to acknowledge that some configurations might be rarer than others. What are the other cases? They are "low weighted". They are less probable, in standard code practices. We identify as consecutive right brackets like "]]", a right bracket directly in front of a choreographed left bracket like "](", a right bracket followed by specific symbols without any separating whitespace, e.g., "]&" or "]^". This is an overwhelmingly impressive feat of freewill but it's like a bag of chips; not as full as it looks. Similarly, there's not as much text as it looks like.

listRefilledString = 
  Association@
   Table[If[sanityCheck[brokenStr, n], 
     n -> StringInsert[brokenStr, "]", n], Nothing], {n, 
     StringLength[brokenStr] + 1}];
listRefillStringPassSyntaxQ = 
  DeleteDuplicates@Select[listRefilledString, SyntaxQ];
candidateScoreList = 
  candidateScoreExptLeaves /@ listRefillStringPassSyntaxQ;
BarChart[Values[candidateScoreList], ChartStyle -> LightBlue, 
 BarOrigin -> Bottom, AxesLabel -> {"Position", "Score"}, 
 ImageSize -> Large, Ticks -> {None, Automatic}, 
 PlotRange -> {{Automatic, Automatic}, {55/60, Automatic}}, 
 PlotTheme -> "Minimal"]

Bar Chart Candidate Score List

That's one of the things that John Von Neumann, when he wrote a letter of recommendation for Turing, I don't think that Alan Turing thought that his paper about computational universality, the (1936) paper on computational applications...technology for the silkworm is technology for humans more extremely the trunk of a tree is presumably something where the tree is assembling that for the purpose of getting its leaves up high, we humans can make use of that..On Computable Numbers, with an Application to the Entscheidungsproblem.

fixCode[brokenCodeStr_] := 
 Module[{listRefillStringPassSyntaxQ, candidateScoreList, 
   perfectIndices, rightLeftCheckPassListIdx, 
   implicatedMultiplicationIdx, potentialIdx, candidatePerfectList}, 
  listRefillStringPassSyntaxQ = 
   DeleteDuplicates@
    Select[Association@
      Table[If[sanityCheck[brokenCodeStr, n], 
        n -> StringInsert[brokenCodeStr, "]", n], Nothing], {n, 
        StringLength[brokenCodeStr] + 1}], SyntaxQ];
  candidateScoreList = 
   candidateScoreExptLeaves /@ listRefillStringPassSyntaxQ;
  perfectIndices = 
   Keys@Select[candidateScoreList, EqualTo[Max[candidateScoreList]]];
  rightLeftCheckPassListIdx = 
   Select[perfectIndices, ! rightLeftCheck[brokenCodeStr, #] &];
  implicatedMultiplicationIdx = 
   Select[rightLeftCheckPassListIdx, ! 
      implicatedMultiplicationCheck[brokenCodeStr, #] &];
  potentialIdx = implicatedMultiplicationIdx;
  candidatePerfectList = 
   Table[StringInsert[brokenCodeStr, "]", potentialIdx[[n]]], {n, 1, 
     Length[potentialIdx], 1}];
  candidatePerfectList]

brokenCodeStr = "f[g[a,b,c,d,e]";
Print[fixCode[brokenCodeStr]];
expr = "f[g[h[x, y], z], w]";
brokenExpr = "f[g[h[x, y, z], w]";
accuracyFunc[brokenExpr, expr]
candidatePerfectListFunc[brokenExpr]

Broken Code Fix Code

Accuracy Func

Candidate Perfect List Func, Broken Expr

That is the question of Hilbert can you determine by mechanical means whether some specific logical statement is true or not? I love metallurgy..whenever you're reading the contents of system files or burying the session, like John Graunt I could have died in your arms. There's a lot of Fibonacci-based observations in the wiggles of stock prices! I would say that in terms of the "Can you predict what humans will do?" what's coming is much more data on what's going on in brains and we have a hundred billion neurons in our brains and there will come a time when by some feat of signal processing we'll just have this giant read-out of what these hundreds of billions of neurons are doing. What were the functions that compiled into that machine code?

getScoreDistribution[str_] := 
 Module[{brokenStr = brokenStrFunc[str]}, 
  listRefillStringPassSyntaxQFunc[brokenStr];
  candidateScoreListFunc[brokenStr];
  Values[candidateScoreList]]
Histogram[getScoreDistribution[str]]

Histogram Get Score Distribution

exampleInputExpressions = {HoldComplete[Plot[Sin[x], {x, 0, 2 Pi}]], 
   HoldComplete[Table[i, {i, 1, 10}]], 
   HoldComplete[Integrate[1/(x^3 - 1), x]], 
   HoldComplete[DSolve[y'[x] == y[x] Cos[x + y[x]], y, x]], 
   HoldComplete[Limit[(1 + x/n)^n, n -> Infinity]], 
   HoldComplete[Sum[1/2^k, {k, 1, Infinity}]], 
   HoldComplete[NSolve[x^5 - x^2 + 1 == 0, x]], 
   HoldComplete[
    Reduce[ForAll[{x, y, z}, x^2 + y^2 == z^2], {x, y, z}, Integers]]};
calculateAccuracy[brokenStrings_, correctStrings_] := 
 N[Quiet[Mean[
    Boole[MapThread[
      accuracyFunc, {brokenStrings, correctStrings}]]]]]
numExamples = 10;
brokenStrings = 
  Table[brokenStrFunc[
    StringDelete[
     ToString[FullForm[RandomChoice[exampleInputExpressions]]], 
     WhitespaceCharacter]], {numExamples}];
correctStrings = 
  StringDelete[ToString[FullForm[#]], WhitespaceCharacter] & /@ 
   RandomChoice[exampleInputExpressions, numExamples];
accuracy = calculateAccuracy[brokenStrings, correctStrings]
Print["The accuracy of the fixes over ", numExamples, " examples is: \
", accuracy]
exampleInputStrings = 
  ToString[FullForm[#]] & /@ exampleInputExpressions;
brokenExprStrings = brokenStrFunc[#] & /@ exampleInputStrings;
potentialRepairIndices = potentialIdxFunc[#] & /@ brokenExprStrings;
ListPlot[potentialRepairIndices, 
 AxesLabel -> {"Expression Number", "Position Index"}, 
 PlotLabel -> "Potential Repair Indices for Each Expression", 
 PlotRange -> All, Joined -> False, PlotStyle -> PointSize[Medium], 
 GridLines -> {Automatic, Automatic}, 
 GridLinesStyle -> Directive[LightGray, Dashed], 
 Ticks -> {Automatic, Automatic}]
expr1 = FullForm[2 + 3*4^2]

After applying the sanity check, argument check, and filtering out low-weighted cases..our heuristic is for the nth time, providing a more concise and likely set of positions where the missing bracket might be. The cellular automata, think about death..these missing brackets can be ranked by their respective scores (with ties resolved to avoid misunderstanding of what they actually do, just once, many different names with the same concept revealing a different origin story so to speak). When we put the tag on it, based on further insights, we get this beautiful, melodious grouping of the most probable sites for the missing right bracket, have you seen it? Using a heuristic algorithm we can build a computational jacuzzi of promising results with a success ratio between 70% and 80%. But then we can also provide a less selfish list of the most probable positions, for the missing bracket. No matter how complex the bracket structures or large the sample datasets, or the syntactical heuristic detection of in code, Henry Stommel's cellular automata hadn't managed to find the sand dunes of A New Kind of Science.

accuracy

numExamples accuracy

Potential Repair Indices

@Xinyu Luo the algorithm you've got is like the max-length (non)linear feedback shift register, might be the most used algorithm in history..communication channels between displays & computers, GPS satellites the timing information, in cellular telephony, code-division multiplexing.. you've got a syntax check to ensure the new string with the inserted ] is syntactically correct, a right-left bracket count to ensure there are equal numbers of left [ and right ] brackets. And, an implicated multiplication check; might the missing bracket be related to a multiplication operation?

expr1

The shift map and the automorphisms and questions like when you have a non-linear cellular automaton under what circumstances is it reversible? It sure is advantageous that Wolfram Language supports parallel computations, which can help speed up the process of showing the distribution of the number of candidates, to suggest the correct position via potential places for the missing bracket.

POSTED BY: Dean Gladish
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract