Create Dictionary in Javascript / PHP -
i'm trying create dictionary .txt
file in shape of tree. on every line of text file there's word, extract words in array.
now regarding tree, each node contains letter, if it's last letter of word, contains definition, , each node have array children contains letters others words starting same way.
so have nodes defined way:
function node(letter,definition,children) { this.letter = letter, this.definition = "", this.children = [] };
i have array dictionary contain nodes. every node organized (so know 'a' in dictionary[0] , 'b' in dictionary[1] , on).
i defined functions build dictionary:
check if dictionary contains first letter of word have (c character, dictio dictionary array , ascii ascii-97 value of character)
function checkchar(c,dictio,ascii){ if(dictio[ascii].letter == c ){ return true; } return false; };
create node given character
function createchar(c){ var noeud = { letter: c, def: '', children: [] }; return noeud; };
add character dictionary
function addchar(c,dictio,ascii){ dictio.children[ascii] = createchar(c); };
and i'm having trouble on biggest function: main on adds word , calls of these small functions i've written. i'm having trouble making.
i don't know if i'm doing right or wrong, if point me right direction or suggest method in javascript or php dictionary txt file great.
ok...
so example of txt file containing words
//words.txt hello world foo bar
word_dictionary.php parsing txt file , has method checking if word exists in tree/dictionary
<?php //word_dictionary.php class node{ private $letter; private $definition = ''; private $children = array(); function __construct($letter){ $this->letter = $letter; } function haschild($letter){ return array_key_exists($letter,$this->children); } function addchild($letter){ $this->children[$letter] = new node($letter); return $this->children[$letter]; } function getchild($letter){ return $this->children[$letter]; } function setdefinition($definition){ $this->definition = $definition; } function getdefinition(){ return $this->definition; } function hasdefinition(){ return (bool)$this->definition; } } // method getting word definition tree/dictionary. // if word exists return definition, else return false function getdefinition($word,$tree){ $node = $tree; $length = strlen($word); foreach(str_split($word) $index => $letter){ if($node->haschild($letter)){ $node = $node->getchild($letter); } else{ // word not exists return false; } if(($index+1) == $length){ // means last letter in word return ($node->hasdefinition()) ? $node->getdefinition() : false; } } } // start build tree/dictionary. part execute once building tree. $anchor = new node(''); $handle = fopen('words.txt','r'); while(($word = fgets($handle))){ $word = rtrim($word); $length = strlen($word); $node = $anchor; foreach(str_split($word) $index => $letter){ if($node->haschild($letter)){ $node = $node->getchild($letter); } else{ $node = $node->addchild($letter); } if(($index+1) == $length ){ //print 'definition word: '.$word."\n"; $node->setdefinition('definition world: '.$word); } } } //use function when user type word want check if exists , return definition user. flow should in ajax request client print getdefinition('bar',$anchor)."\n";
hope bit ;)
Comments
Post a Comment