A Java Class that can autocomplete delimiters and tell you exactly which function is associated with a closing delimiter!
Based on my curiosity of figuring out how the human brain finds patterns to match delimiters so easily.
The only file in this repo you need is the SmartDelimiterPredictor.jar
. You can get this through 2 methods -
-
You can download the whole repo as a zip and then extract the
.jar
file -
You can open
SmartDelimiterPredictor.jar
in raw mode, this will download the.jar
file. -
You can run the following command to download it directly from terminal.
curl -o SmartDelimiterPredictor.jar https://raw.githubusercontent.com/TotallyNotChase/Smart-Delimiter-Predictor/master/SmartDelimiterPredictor.jar
Now you can add this jar as an external archive to any of your projects and import the class using
import com.github.chase.smartdelimiter.SmartDelimiterPredictor;
- Well-documented code for you to follow along!
- Super modular! Can support any delimiters. The default supported delimiters are
(
\)
,{
\}
, and[
\]
However, you can add your own delimiter pairs anytime, by usingaddDelimiterPair(String openingDelimiter, String closingDelimiter)
! For instance if you wanted to add<
/>
, you'd simply do-SmartDelimiterPredictor sdp = new SmartDelimiterPredictor(); sdp.addDelimiterPair("<", ">"); // Use other methods here!
First instantiate the class using:
SmartDelimiterPredictor sdp = new SmartDelimiterPredictor();
Then, you may call the available methods.
There are 3 methods in the SmartDelimiterPredictor
class.
-
addDelimiterPair(String openingDelimiter, String closingDelimiter)
: Add your desired delimiter pair if it does not already exist! See above for details. -
closeDelimiters(String expression)
: Returns a string with all open delimiters in expression closed.If
expression
isloge[sin(2+56^{58*cos(log0[
it will return
loge[sin(2+56^{58*cos(log0[])})]
-
findFunctionForDelimiterAt(int index, String[] expression, String delimiter)
: Returns the function associated with the given delimiter (open or closed) at given index.If
expression
is["loge", "[", "sin", "[", "2", "+", "56", "^", "{", "58", "*", "cos", "[", "log0", "[", "23", "]", "]", "}", "]", "]"]
and if the
index
is 17and if the delimiter is either
[
or]
.it will return
cos
Here is the autocomplete in action!
note that although the frontend shows only parentheses, the backend uses different delimiters for different functions, so it still uses the exact same algorithm
And the functionfinder!
notice that whenever I remove a closing delimiter using backspace, the program is able to tell which function region I just entered
Check out an example of how it's supposed to be used here
Basically I got the idea for the algo from thinking step by step about how the human brain does it in the first place. The code has an explanation for all the algos.
A detailed explanation can found here
Check out the source code as well as the example and follow along!
As always, using the algo yourself and tracking the variables will make it easier to understand.