I'm trying to create a dictionary from a .txt file in the shape of a tree. On every line of the text file there's a word, I extract all those words in an array.
Now regarding the tree, Each node contains a letter, if it's the last letter of a word, it contains a definition, and each node have an array Children that contains letters from all others words starting the same way.
So I have nodes defined this way:
function Node(letter,definition,children) {
this.letter = letter,
this.definition = "",
this.children = []
};
I have an array Dictionary that will contain all the nodes. Every Node will be organized (so that we know 'a' is in Dictionary[0] and 'b' in Dictionary[1] and so on).
I defined some functions to help build the dictionary:
check if Dictionary contains the first letter of the word we have (c is the character, dictio is the dictionary array and ascii is the ascii-97 value of the character)
function checkChar(c,dictio,ascii){ if(dictio[ascii].letter == c ){ return true; } return false; };create a node with the given character
function createChar(c){ var noeud = { letter: c, def: '', children: [] }; return noeud; };
Add the character to the dictionary
function addChar(c,dictio,ascii){ dictio.children[ascii] = createChar(c); };
And I'm having trouble on the biggest function: the main on that adds the word and calls all of these small functions I've written. Which I'm having trouble making.
I don't even know if what I'm doing is right or wrong, if anyone could point me to the right direction or suggest a method in javascript or php to do dictionary from a TXT file that would be great.
function Node(letter,definition,children) = {};--> syntax error?Objects. Instead of you having functions scattered across your code, you create a newObjectand it will have all the required methods to work. Everything contained in a single instance.