Created
April 12, 2020 03:51
-
-
Save AshyIsMe/4995c63eda95a21ca5eb4e9b05f2bf08 to your computer and use it in GitHub Desktop.
crappy-fzf.ijs - apl J lang - levenshtein distance based best match finding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env jconsole | |
NB. https://code.jsoftware.com/wiki/Essays/Levenshtein_Distance | |
NB. version producing only scalar distance | |
levdist=: 4 : 0 " 1 | |
'a b'=. (/: #&>)x;y | |
z=. >: iz =. i.#b | |
for_j. a do. | |
z=. <./\&.(-&iz) (>: <. (j ~: b) + |.!.j_index) z | |
end. | |
{:z | |
) | |
NB.'sitting' levdist 'kitten' | |
NB. https://code.jsoftware.com/wiki/Phrases/Strings | |
strsplit=: #@[ }.each [ (E. <;.1 ]) , | |
NB. https://code.jsoftware.com/wiki/JPhrases/InsideBoxes | |
NB. # &.> a NB. a0=: each=: &.> Apply verb to each box | |
lines =: LF strsplit stdin'' | |
word1 =: {.2}.ARGV | |
NB. get levenshtein distance from argv for each line of stdin | |
levdistances =: word1 levdist &.> lines | |
echo 'levenshtein distances';'search word:';word1 | |
echo 'lines';lines | |
echo 'levdistances';levdistances NB. ;lines | |
bestmatch_index =: (i. <./) >levdistances | |
echo 'bestmatch_index';bestmatch_index | |
bestmatch =: >bestmatch_index { lines | |
echo 'bestmatch';bestmatch | |
exit'' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment