Skip to content

Commit

Permalink
initial import - server runs
Browse files Browse the repository at this point in the history
  • Loading branch information
arieljake committed Jul 3, 2012
0 parents commit 0774836
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.nide
.DS_Store
.*~
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.nide
11 changes: 11 additions & 0 deletions BaseOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


var BaseOptions = module.exports = function()
{

};

BaseOptions.prototype.getOptions = function()
{
return [];
};
Empty file added Icon
Empty file.
41 changes: 41 additions & 0 deletions OptionsFromBSTNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var BaseOptions = require("./BaseOptions.js");
var BSTTraverser = require("../Burst/BSTTraverser.js");
var ATNTraverser = require("../Burst/ATNTraverser.js");

var OptionsFromBSTNode = module.exports = function (trie)
{
this.trie = trie;
};

OptionsFromBSTNode.prototype = new BaseOptions();

OptionsFromBSTNode.prototype.getOptionsFor = function (term)
{
var results = this.trie.get(term);
var options = [];

this.produceOptions(results.node,options,results.prefix);

options.sort();

return options;
};

OptionsFromBSTNode.prototype.produceOptions = function(node,options,prefix)
{
if (node.getType() == "CON")
{
BSTTraverser.traverse(node.bst,addOption,prefix);
}
else if (node.getType() == "ATN")
{
ATNTraverser.traverse(node,addOption,prefix);
}

function addOption(item,itemPrefix)
{
var option = itemPrefix + item.value;

options.push(option);
}
}
1 change: 1 addition & 0 deletions dict/string.txt

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var BurstTrie = require("../Burst/BurstTrie.js");
var OptionsFromBSTNode = require("./OptionsFromBSTNode.js");
var LinePrefix = require("../Burst/LinePrefix.js");

var myTrie = new BurstTrie();
var myOptions = new OptionsFromBSTNode();
var loadedTerms = loadDict(myTrie,100);

getOptions(loadedTerms[0], myOptions);

function addTerm(term,trie)
{
trie.add(term);

console.log("adding " + term);
}

function getOptions(term,options)
{
var results = myTrie.get(term);
options.node = results.node;

console.log("options for " + term);
console.dir(options.getOptions(results.prefix));
console.log(options.node.getType());
console.log(options.node.asString(new LinePrefix("")));
}

function loadDict(trie,maxWords)
{
var fs = require("fs");
var data = fs.readFileSync( "dict/string.txt", "utf8");
var words = data.split(" ");
var numWords = Math.min(words.length,maxWords);
var terms = [];
var word;

for (var i=0; i < numWords; i++)
{
terms.push(words[i]);
}

terms.sort(randOrd);

for (var i=0; i < numWords; i++)
{
trie.add(terms[i]);
}

console.log(trie.toString());

return terms;
}

function randOrd()
{
return (Math.round(Math.random())-0.5);
}
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "node-autocomplete",
"description": "autocomplete server for node.js",
"version": "0.1.0",
"author": "Ariel Jakobovits <[email protected]>",
"contributors": [
{
"name":"Ariel Jakobovits",
"email":"[email protected]"
}
],
"keywords": [
"autocomplete",
"trie"
],
"engines": {
"node": ">= 0.6"
},
"dependencies": {
"burst-trie": ">=0.1.0"
},
"devDependencies": {},
"repository": {
"type": "git",
"url": "git://github.com/arieljake/node-autocomplete.git"
},
"license": "MIT"
}
68 changes: 68 additions & 0 deletions web.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var express = require('express');
var BurstTrie = require("burst-trie");
var OptionsFromBSTNode = require("./OptionsFromBSTNode.js");

var port = process.env.PORT || 5000;
var app = express.createServer();
var myTrie = BurstTrie.createTrie();
var myOptions = new OptionsFromBSTNode(myTrie);
var myTrieWriter = BurstTrie.createTrieWriter();

loadDict(myTrie,1000);

app.configure(function ()
{
app.use(express.query());
app.use(express.methodOverride());
});

app.get('/autocomplete/:value', function(request, response)
{
var term = request.params["value"];
var options = myOptions.getOptionsFor(term);

response.send(options);

console.log("Responded to " + term + " with " + options.length + " options");
});

app.get('/dict', function(request, response)
{
response.send("<pre>" + myTrieWriter.writeTrie(myTrie) + "</pre>");
});

app.listen(port, function()
{
console.log("Listening on " + port);
console.log("Send autocomplete requests to /autocomplete/:value");
console.log("View trie by sending request to /dict");
});

function loadDict(trie,maxWords)
{
var fs = require("fs");
var data = fs.readFileSync( "dict/string.txt", "utf8");
var words = data.split(" ");
var numWords = Math.min(words.length,maxWords);
var terms = [];
var word;

for (var i=0; i < numWords; i++)
{
terms.push(words[i]);
}

terms.sort(randOrd);

for (var i=0; i < numWords; i++)
{
trie.add(terms[i]);
}

return terms;
}

function randOrd()
{
return (Math.round(Math.random())-0.5);
}

0 comments on commit 0774836

Please sign in to comment.