Wordexer

word indexer

by khrome

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/mootools/1.4.5/mootools-core-full-compat.js"></script>

JavaScript

var Wordexer = function(){
    this.index = {}
}
Wordexer.prototype.crawl = function(uri){
    (new Request({
        url : uri,
        onSuccess : function(data){
            console.log('links', data, data.search(/<a .*?>/));
        }
    })).send();
}
Wordexer.prototype.addWord = function(word){
    return this.addToIndex(word.split(), this.index);
}
Wordexer.prototype.addPhrase = function(phrase){
    phrase = phrase.replace(/[\.'"?,\(\)]/g, '');
    var words = phrase.split(' ');
    console.log('words', phrase, words);
    var lastWord = false;
    words.each(function(word){
        //console.log('WORD', '|'+word+'|', typeOf( word));
        //var chars = (word+'').split();
        var node = this.addWord(word, this.index);
        if(lastWord){
            if(!lastWord.next) lastWord.next = [];
            lastWord.next.push(node);
            if(!node.previous) node.previous = [];
            node.previous.push(node);
        }
        var lastWord = node;
    }.bind(this));
}
Wordexer.prototype.addToIndex = function(stack, index){
    if(stack.length === 0){ //we're done
        if(!index['#']) index['#'] = 0; 
        index['#']++;
        return index;
    }else{ //keep working
        var ch = stack.shift();
        if(!index[ch]) index[ch] = {};
        this.addToIndex(stack, index[ch]);
    }
}
Wordexer.prototype.worm = function(stack, index){
    if(stack.length === 0){ //we're done
        return index['#'];
    }else{ //keep working
        var ch = stack.shift();
        if(!index[ch]) return undefined;
        return this.worm(stack, index[ch]);
    }
}
Wordexer.prototype.wordCount = function(word){
    return this.worm(word.split(), this.index);
}

var index = new Wordexer();
index.crawl('https://crossorigin.me/http://en.wikipedia.org/wiki/Philosophy');
index.addPhrase('blah, blah blah,...