-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0774836
Showing
9 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.nide | ||
.DS_Store | ||
.*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.nide |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |