-
-
Save RameshRM/8d8c12a091ff648c53d0bbd5eb32ba08 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'); | |
const Path = require('path'); | |
const modUtils = require('../mod-utils'); | |
const DATASET_PATH = process.env.DATASET_PATH; | |
const Debug = require('debug')('typeahead'); | |
module.exports = Trie; | |
function Trie() { | |
this.root = new TrieNode(); | |
this.dataSetFlatMap = {}; | |
this.buildDataSetFlatMap = function(input) { | |
this.dataSetFlatMap = Array.isArray(input) && input.length > 0 && input.reduce(function reduce(acc, inputItem) { | |
acc[inputItem.name.toLowerCase()] = inputItem; | |
return acc; | |
}, {}); | |
}; | |
} | |
Trie.prototype.save = function save(callback) { | |
let _this = this; | |
FS.writeFile(Path.join(DATASET_PATH, 'trie.json'), modUtils.tryJsonStringify(this), function(err) { | |
Debug('Stored Trie DataSet', Path.join(DATASET_PATH, 'trie.json')); | |
return callback(undefined, { | |
TrieEntity: _this | |
}); | |
}); | |
}; | |
Trie.prototype.search = function(keywords, callback) { | |
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); | |
// Object.keys(input.ref[item].ref).forEach(function forEach(refItem){ | |
// wordList.push(refItem); | |
// // console.log('***',wordList,refItem,input.ref[item][refItem]); | |
// console.log(wordList); | |
// buildWordList(input.ref[item].ref[refItem], wordLists); | |
// }); | |
// wordList.push('____'); | |
}); | |
} | |
} | |
let node = this.root; | |
let matchIdx; | |
let matchNode; | |
let wordList = []; | |
let wordLists = []; | |
let lastMatch; | |
for (var i = 0; i < keywords.length; i++) { | |
if (node.ref[keywords[i]]) { | |
matchIdx = i; | |
matchNode = node.ref; | |
node = node.ref[keywords[i]]; | |
lastMatch = node; | |
} | |
} | |
// console.log('****',lastMatch); | |
// console.log(matchNode); | |
let matchResult = {}; | |
matchResult[keywords[matchIdx]] = { | |
ref: lastMatch | |
}; | |
// matchNode = lastMatch; | |
// console.log(matchResult, matchResult[keywords[matchIdx]]); | |
// console.log(matchNode[keywords[matchIdx]], matchIdx); | |
if (matchNode && matchIdx > -1) { | |
matchNode = matchNode[keywords[matchIdx]]; | |
let returnResult = []; | |
Object.keys(matchNode.ref).forEach(function forEach(key) { | |
// returnResult.push([key]); | |
if (matchNode.ref[key].isWord) { | |
} else { | |
console.log('***', key, ':::'); | |
let matchList = buildWordList2(matchNode.ref[key], [], key); | |
matchList && matchList.reduce(function reduce(acc, item) { | |
// console.log(Util.format('%s%s',key,item)); | |
acc.push(Util.format('%s%s', key, item)); | |
// console.log(item); | |
return acc; | |
}, returnResult); | |
// console.log(key, '***', buildWordList2(matchNode.ref[key], [], key)); | |
// returnResult[returnResult.length - 1] = returnResult[returnResult.length - 1].concat(buildWordList2(matchNode.ref[key], returnResult[returnResult.length - 1])); | |
} | |
}); | |
// console.log(returnResult); | |
function buildWordList2(input, result, ref) { | |
let maxKeys = Object.keys(input.ref); | |
console.log(input); | |
return; | |
while (input && input.ref && input.isWord != true) { | |
console.log(input.ref, maxKeys[i]); | |
break; | |
}; | |
return ; | |
// console.log(input); | |
for (var i = 0; i < maxKeys.length;i++) { | |
} | |
return; | |
maxKeys = maxKeys.map(function map(item) { | |
console.log(input.ref[item], item); | |
item = item + buildWordList2(input.ref[item], [], (ref + item)) || ''; | |
return item; | |
}); | |
return maxKeys; | |
return; | |
Object.keys(input.ref).forEach(function forEach(key) { | |
result = result.concat(Object.keys(input.ref[key].ref)); | |
// console.log('**',Object.keys(input.ref[key].ref)); | |
// if (key) { | |
// // console.log(result); | |
// // result && result.push(key); | |
// } else { | |
// return result; | |
// } | |
if (input.ref[key].isWord) { | |
return result; | |
} else { | |
return buildWordList2(input.ref[key], result); | |
} | |
}); | |
} | |
buildWordList(matchNode, []); | |
let dataSetMap = this.dataSetFlatMap; | |
return Array.isArray(wordLists) && wordLists.map(function map(item) { | |
return { | |
name: item, | |
avatar_url: dataSetMap[item] && dataSetMap[item.toLowerCase()].owner && dataSetMap[item].owner.avatar_url || 'https://github.com/identicons/jasonlong.png' | |
}; | |
}).sort(function sort(first, second) { | |
if (first && first.name < second && second.name) { | |
return -1; | |
} | |
if (first && first.name > second && second.name) { | |
return 1; | |
} | |
return 0; | |
}); | |
} | |
}; | |
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; | |
}; | |
Trie.buildDataSet = function(input, callback) { | |
let trie = new Trie(); | |
trie.buildDataSetFlatMap(input); | |
trie.buildList(input.map(function map(item) { | |
Debug('Building Trie', item.name); | |
return item.name; | |
})); | |
return trie.save(callback); | |
}; | |
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