Created
September 2, 2018 16:01
-
-
Save RameshRM/5d31e75fe07fdbe3c72e014cfd85c932 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
const repos = require('./repos'); | |
const FS = require('fs'); | |
const trieDataSet = require('./trie-dataset'); | |
const Util = require('util'); | |
module.exports = Trie; | |
function Trie() { | |
this.root = new TrieNode(); | |
} | |
Trie.prototype.search = function(keywords) { | |
function doSearch(key, node) { | |
// console.log(key, node && node.ref); | |
return node && node[key]; | |
} | |
let node = trieDataSet.root; | |
let matches = []; | |
let matchIdx; | |
let matchNode; | |
for (var i = 0; i < keywords.length; i++) { | |
if (node.ref[keywords[i]]) { | |
matchIdx = i; | |
matchNode = node.ref; | |
matches.push(node.ref); | |
node = node.ref[keywords[i]]; | |
} | |
} | |
// console.log(matches); | |
matchNode = matchNode[keywords[matchIdx]]; | |
// console.log(JSON.stringify(matches)); | |
// console.log(JSON.stringify(matches[0])); | |
// console.log(matchNode); | |
// matches.forEach(function forEach(item) { | |
// // console.log(';***',Object.keys(item)[0]); | |
// }); | |
let wordList = []; | |
let wordLists = []; | |
console.log(keywords.substring(0, matchIdx + 1)); | |
function buildWordList(input) { | |
if (input && input.isWord) { | |
wordLists.push(Util.format('%s%s', keywords.substring(0, matchIdx + 1), wordList.join(''))); | |
wordList = []; | |
return wordLists; | |
} | |
if (input && input.ref && Object.keys(input.ref).length > 0) { | |
Object.keys(input.ref).forEach(function forEach(item) { | |
wordList.push(item); | |
return buildWordList(input.ref[item], wordLists); | |
}); | |
} | |
} | |
let result = buildWordList(matchNode, []); | |
console.log(wordLists); | |
return; | |
function buildWord(input) { | |
console.log(input); | |
Object.keys(input).forEach(function forEach(item) { | |
console.log(item); | |
}); | |
console.log('8888'); | |
} | |
getWords(matches); | |
// console.log(JSON.stringify(matches)); | |
}; | |
Trie.prototype.buildList = function buildList(inputList) { | |
let _this = this; | |
inputList.reduce(function reduce(acc, item) { | |
_this.build(item); | |
return acc; | |
}, undefined); | |
}; | |
Trie.prototype.build = function build(input) { | |
let trieNode = this.root; | |
for (var i = 0; i < input.length; i++) { | |
if (!trieNode.ref[input[i]]) { | |
trieNode.ref[input[i]] = new TrieNode(); | |
trieNode = trieNode.ref[input[i]]; | |
} else { | |
trieNode = trieNode.ref[input[i]]; | |
} | |
} | |
trieNode.isWord = true; | |
}; | |
function TrieNode(key) { | |
this.ref = {}; | |
this.isWord; | |
} | |
let trie = new Trie(); | |
trie.buildList(repos.map(function map(item) { | |
return item.name; | |
})); | |
trie.search("m"); | |
FS.writeFileSync('trie-dataset.json', JSON.stringify(trie)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment