如何理解JavaScript中的原編程 和 函數式編程

在 《Paradigms of Artificial Intelligence Programming》一書中有一個簡單的隨機句子生產器程序,原網址今天好像不能打開

不過在http://blog.csdn.net/zzljlu/article/details/7830259 中的末尾有實現 爲了瞭解JS 的函數式編程和原編程威力

也用JS實現了一下, 沒有註釋 望諒解

---------------------------------------------------------------------------------------------------------

var grammer;
var simpleGrammar = [['sentence', '_', ['nounPhrase', 'verbPhrase']], ['nounPhrase', '_', ['article', 'noun']], ['verbPhrase', '_', ['verb', 'nounPhrase']], ['article', '_', 'the', 'a'], ['noun', '_', 'man', 'woman', 'ball', 'table'], ['verb', '_', 'hit', 'took', 'saw', 'liked']];
var biggerGrammar = [['sentence', '_', ['nounPhrase', 'verbPhrase']], ['nounPhrase', '_', ['article', 'adj1', 'noun', 'pp1'], ['name'], ['pronoun']], ['verbPhrase', '_', ['verb', 'nounPhrase', 'pp1']], ['pp1', '_', null, ['pp', 'pp1']], ['adj1', '_', null, ['adj', 'adj1']], ['pp', '_', ['prep', 'nounPhrase']], ['prep', '_', 'to', 'in', 'by', 'with', 'on'], ['adj', '_', 'big', 'little', 'blue', 'green', 'adiabatic'], ['article', '_', 'the', 'a'], ['name', '_', 'pat', 'kim', 'lee', 'terry', 'robin'], ['noun', '_', 'man', 'ball', 'woman', 'table'], ['verb', '_', 'hit', 'took', 'saw', 'liked'], ['pronoun', '_', 'the', 'she', 'it', 'these', 'those', 'that']];
function ruleLeft(rule) {
    return (rule instanceof Array) && rule[0];
};
function ruleRight(rule) {
    return (rule instanceof Array) && rule.slice(2);
};
function rewrites(category) {
    return ruleRight(assoc(category, grammer));
};
function generate(phrase) {
    if ((phrase instanceof Array)) {
        return mappend(generate, phrase);
    } else if (rewrites(phrase)) {
        return generate(randomElt(rewrites(phrase)));
    } else {
        return [phrase];
    };
};
function assoc(category, grammer) {
    if ((grammer instanceof Array)) {
        for (var len = grammer.length, i = 0; i < len; i = ++i) {
            var rule = grammer[i];
            if (category === rule[0]) {
                return rule;
            };
        };
    };
};
function mappend(fn, array) {
    return array.map(fn).reduce(function (x, y) {
        return x.concat(y);
    });
};
function randomElt(array) {
    return array[Math.floor(Math.random() * array.length)];
};
function generateTree(phrase) {
    if ((phrase instanceof Array)) {
        return phrase.map(generateTree);
    } else if (rewrites(phrase)) {
        var temp = generateTree(randomElt(rewrites(phrase)));
        temp.unshift(phrase);
        return temp;
    } else {
        return [phrase];
    };
};
function generateAll(phrase) {
    if ((phrase instanceof Array) && phrase.length === 0) {
        return [[]];
    } else if ((phrase instanceof Array)) {
        return combineAll(generateAll(phrase[0]), generateAll(phrase.slice(1)));
    } else if (rewrites(phrase)) {
        return mappend(generateAll, rewrites(phrase));
    } else {
        return [[phrase]];
    };
};
function combineAll(xarray, yarray) {
    return mappend(function (y) {
        return xarray.map(function (x) {
            return x.concat(y);
        });
    }, yarray);
};
grammer = simpleGrammar;
console.log(generateAll('sentence').length);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章