JS : Levenshtein distance for two lists or strings
Hello
I'm completely new to using the js object, and my javascript knowledge
is still limited to what I could get from codecademy, so please forgive my ignorance…
I want to import into Max a function I use frequently in Lisp and Lisp-like environments,
the Levenshtein distance, which computes how many basic operations (add, delete, replace) you need to get from one sequence to another.
I got the javascript code from here : http://rosettacode.org/wiki/Levenshtein_distance#JavaScript
and tried to adapt it for js object, but I'm stuck with to problems.
1) how to apply this to lists made of symbols (ex: a b c and a b a c)?
Of course, JS reads these as arrays of variables, which is wrong...
The functions accepts normally only strings, but also works with string arrays,
like ["a", "b", "c"], yet I can't understand how to convert properly the max lists to strings
or string arrays ("tosymbol" is useless).
2) how to get the output... even for lists of integers of floats?
I thought these would be converted to [1, 2, 3] for instance,
but even this way my code doesn't produce any output.
Here's my js code for now :
inlets = 2;
outlets = 1;
var str1;
var str2;
function list()
{
if(inlet==0)
{
str1 = arrayfromargs(arguments);
}
else if(inlet==1)
{
str2 = arrayfromargs(arguments);
}
}
function levenshtein(str1, str2) {
var m = str1.length,
n = str2.length,
d = [],
i, j;
if (!m) return n;
if (!n) return m;
for (i = 0; i
Thanks in advance for your help
Best
Julien
The conventional behavior of any Max object is to produce output when a message arrives in the leftmost inlet. But you have to program that behavior. Specifically, you'd need to call outputRes()
as the last thing you do in the if (inlet==0)
branch of your list handler. You might also want to implement a bang handler.
As for "lists made of symbols." Max doesn't actually have lists that start with a symbol. If you have something that looks like a list starting with a symbol, it is actually a message (the first symbol) followed by arguments. List always start with a number.
Probably the easiest way to handle this in a JS object is to implement an "anything" handler. I've not done this in JavaScript for yonks, but I seem to recall that there's something in the documentation.
Hello Peter
Thank you very much, the outputRes() added to the function list did the trick.
So for now it's working with lists of number, but actually this is enough since
[a, b, c] vs [a, c, b, d] and [1, 2, 3] vs [1, 3, 2, 4] is considered the same for Levenshtein algorithm.
I just need to implement some kind of translators from symbols to index, by order of appearance,
so I can work with it for now.
Thanks again, all my best !
Julien
If you like Lisp, you could also write your code in Clojure and load the classes into Mxj. Check out Nick Rothwell's writing on this.
Also, there's ClojureScript; it generates JavaScript byte code from Clojure.
A minor nit-pick: ClojureScript compiles to (unreadable) Javascript, not to bytecode.
If your Lisp code is sufficiently general, you could go for either Clojure or ClojureScript. The former needs a JVM, the latter doesn't, but the toolchain for ClojureScript is a bit more complicated.
The Java version: https://github.com/cassiel/net.loadbang.clojure
The Javascript version: https://github.com/cassiel/jsui-cljs