The usual sequence

  • 5 Replies
  • 2225 Views
*

Zero

  • Eve
  • ***********
  • 1287
The usual sequence
« on: March 25, 2020, 12:36:39 am »
Hi,

Here's a little algorithm I came up with tonight, well nothing fancy but I quite like it. You give it sequences (arrays), it gets used to it, and when you give it incomplete sequences, it returns the most probable complete one. As I said, nothing fancy. It will end up in BirdyVM. Here is it.

Code: Javascript

sys.getUsual = (function () {

    var corpus = [];

    var getExtensions = function (base, corpus) {
        var result = [];
        for (let c of corpus) {
            var discard = false;
            for (let b of base)
                if (!c.includes(b)) {
                    discard = true;
                    break;
                }
            if (!discard) result.push(c);
        }
        return result;
    }

    // from https://stackoverflow.com/questions/11919065/sort-an-array-by-the-levenshtein-distance-with-best-performance-in-javascript
    var distance = function (s, t) {

        var d = [];
        var n = s.length;
        var m = t.length;
        if (n == 0) return m;
        if (m == 0) return n;
        for (var i = n; i >= 0; i--) d[i] = [];
        for (var i = n; i >= 0; i--) d[i][0] = i;
        for (var j = m; j >= 0; j--) d[0][j] = j;
        for (var i = 1; i <= n; i++) {
            var s_i = s[i - 1];
            for (var j = 1; j <= m; j++) {
                if (i == j && d[i][j] > 4) return n;
                var t_j = t[j - 1];
                var cost = (s_i == t_j) ? 0 : 1;
                var mi = d[i - 1][j] + 1;
                var b = d[i][j - 1] + 1;
                var c = d[i - 1][j - 1] + cost;
                if (b < mi) mi = b;
                if (c < mi) mi = c;
                d[i][j] = mi;
                if (i > 1 && j > 1 && s_i == t[j - 2] && s[i - 2] == t_j) {
                    d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost);
                }
            }
        }
        return d[n][m];
    }

    var getScores = function (base, line) {

        var score = [];
        for (let l = 0; l < line.length; l++) score[l] = 0;
        for (let l = 0; l < line.length; l++) {
            for (let word of line[l]) {
                for (let l2 = 0; l2 < line.length; l2++) {
                    if (line[l2].includes(word)) score[l2] += 1;
                }
            }
        }
        var result = [];
        for (let r = 0; r < score.length; r++) {
            var ls = score[r];
            var ld = distance(base, line[r]);
            var ll = line[r].length;
            result.push({
                line: line[r],
                score: [
                    ls / ld / ll,
                    ls / ld / 1,
                    ls / 1 / 1,
                    1 / ld / ll,
                    1 / ld / 1,
                    1 / 1 / ll
                ]
            });
        }
        return result;
    }

    var getBest = function (base, result) {

        for (let method = 0; method < 6; method++) {
            var best = [];
            for (let r of result) {
                var highest = 0;
                if (r.score[method] > highest) {
                    best = [r.line];
                    highest = r.score[method];
                }
                else if (r.score[method] === highest) best.push(r.line);
            }
            if (best.length === 1) return best[0];
        }
        return base;
    }

    return function (sequence, maxCorpusLength) {

        var extensions = getExtensions(sequence, corpus);
        var scores = getScores(sequence, extensions);
        var best = getBest(sequence, scores);

        while (corpus.length >= maxCorpusLength)
            corpus.splice(Math.floor(Math.random()*corpus.length), 1);

        corpus.push(sequence);

        return best;
    }
})();


I'm a bit tired tonight, but I'll try to roughly describe it tomorrow.
« Last Edit: March 25, 2020, 01:26:48 pm by Zero »

*

Zero

  • Eve
  • ***********
  • 1287
Re: The usual sequence
« Reply #1 on: March 25, 2020, 01:46:01 pm »
So, what it does is first get all the stored sequences that contain every item you gave as input. That's the getExtensions() function.

Then, getScores() calculates a score for each of them, like this:
- for each sequence
- for each word
- give 1 point to every sequence which contains this word
This results in a base score. Then we add the Levenshtein distance to the input and the length of the sequences in the mix, and try to get a rank with one of these formulas: (try first formula, if ex-aequo, try second, ...etc):
- score / distance / length
- score / distance
- score
- distance / length
- distance
- length
If it's still ex-aequo, return the input.

The use case is to give either sequences or incomplete sequences as input, and get as return value a possible "more complete" version of it.

*

krayvonk

  • Electric Dreamer
  • ****
  • 125
Re: The usual sequence
« Reply #2 on: March 25, 2020, 03:14:58 pm »
What the hell is the Levenschtein distance? sounds fancy, but is it any good?
What does "ex-aequo" mean, above water level?

And give us a demo of the output gibberish!

*

krayvonk

  • Electric Dreamer
  • ****
  • 125
Re: The usual sequence
« Reply #3 on: March 25, 2020, 04:03:56 pm »
I just had an idea!  :D

I dont know if youve already thought of it,  but now you know,  what if you werent just bringing back the same text but bringing back a reply,  that would be cool.

*

Zero

  • Eve
  • ***********
  • 1287
Re: The usual sequence
« Reply #4 on: March 25, 2020, 04:50:10 pm »
Levenshtein distance is cool. It tells you how much identical two sequences are.

Ex-aequo means "they have the same score".

It's not supposed to output gibberish, on the contrary :)

If you say:
"blue sky is nice"
"blue sky is nice"
"blue sky is nice"

Then you say:
"blue sky"

What will it say? You got it.

Now, the getExtensions() part is not good, there are better ways to do it.

*

krayvonk

  • Electric Dreamer
  • ****
  • 125
Re: The usual sequence
« Reply #5 on: March 25, 2020, 06:14:41 pm »
Levenschtein distance taken literally comes up with a few truths about things, but fails miserably on others (not being negative just harshly truthful).  Definitely interest building tho, I remember it makes it in the puzzle sections in glossy magazines sometimes.   If you get the meaning with the near spell together, thats when im off going nuts in my house about things.
Sounds like a winner,   I think this a.i. business is going to be simple, and im predicting my embarressment.    Just run the robot timestep to timestep on a stop watch calculus derivative style is probably all you need to maybe even make consciousness given enough power,  but im still not sure about it.  But if thats the case, I wouldnt bother feeling like a high*er* genius.  :-[

 


Will LLMs ever learn what is ... is?
by HS (Future of AI)
November 10, 2024, 06:28:10 pm
Who's the AI?
by frankinstien (Future of AI)
November 04, 2024, 05:45:05 am
Project Acuitas
by WriterOfMinds (General Project Discussion)
October 27, 2024, 09:17:10 pm
Ai improving AI
by infurl (AI Programming)
October 19, 2024, 03:43:29 am
Atronach's Eye
by WriterOfMinds (Home Made Robots)
October 13, 2024, 09:52:42 pm
Running local AI models
by spydaz (AI Programming)
October 07, 2024, 09:00:53 am
Hi IM BAA---AAACK!!
by MagnusWootton (Home Made Robots)
September 16, 2024, 09:49:10 pm
Attempting Hydraulics
by MagnusWootton (Home Made Robots)
August 19, 2024, 04:03:23 am
LLaMA2 Meta's chatbot released
by spydaz (AI News )
August 24, 2024, 02:58:36 pm
ollama and llama3
by spydaz (AI News )
August 24, 2024, 02:55:13 pm
AI controlled F-16, for real!
by frankinstien (AI News )
June 15, 2024, 05:40:28 am
Open AI GPT-4o - audio, vision, text combined reasoning
by MikeB (AI News )
May 14, 2024, 05:46:48 am
OpenAI Speech-to-Speech Reasoning Demo
by MikeB (AI News )
March 31, 2024, 01:00:53 pm
Say good-bye to GPUs...
by MikeB (AI News )
March 23, 2024, 09:23:52 am
Google Bard report
by ivan.moony (AI News )
February 14, 2024, 04:42:23 pm
Elon Musk's xAI Grok Chatbot
by MikeB (AI News )
December 11, 2023, 06:26:33 am

Users Online

524 Guests, 0 Users

Most Online Today: 571. Most Online Ever: 2369 (November 21, 2020, 04:08:13 pm)

Articles