在Javascript/PHP中创建字典
我正在尝试从树形的在Javascript/PHP中创建字典,javascript,php,arrays,Javascript,Php,Arrays,我正在尝试从树形的.txt文件创建字典。在文本文件的每一行上都有一个单词,我提取数组中的所有单词 现在关于树,每个节点包含一个字母,如果它是单词的最后一个字母,那么它包含一个定义,每个节点都有一个数组子节点,其中包含所有其他单词以相同方式开始的字母 所以我用这种方式定义了节点: function Node(letter,definition,children) { this.letter = letter, this.definition = "", this.children
.txt
文件创建字典。在文本文件的每一行上都有一个单词,我提取数组中的所有单词
现在关于树,每个节点包含一个字母,如果它是单词的最后一个字母,那么它包含一个定义,每个节点都有一个数组子节点,其中包含所有其他单词以相同方式开始的字母
所以我用这种方式定义了节点:
function Node(letter,definition,children) {
this.letter = letter,
this.definition = "",
this.children = []
};
我有一个包含所有节点的数组字典。将对每个节点进行组织(以便我们知道“a”在字典[0]中,“b”在字典[1]中,依此类推)
我定义了一些函数来帮助构建字典:
- 检查字典是否包含单词的第一个字母(c是字符,dictio是字典数组,ascii是字符的ascii-97值)
- 创建具有给定字符的节点
function checkChar(c,dictio,ascii){ if(dictio[ascii].letter == c ){ return true; } return false; };
function createChar(c){ var noeud = { letter: c, def: '', children: [] }; return noeud; };
- 创建具有给定字符的节点
- 将字符添加到字典中
函数addChar(c、dictio、ascii){ 词汇儿童[ascii]=createChar(c); };<?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 for getting a word definition from tree/dictionary. // if word exists return definition, else return false function getDefinition($word,$tree){ $node = $tree; $length = strlen($word); foreach(str_split($word) as $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 your tree/dictionary. This part is execute ONCE only for 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) as $index => $letter){ if($node->hasChild($letter)){ $node = $node->getChild($letter); } else{ $node = $node->addChild($letter); } if(($index+1) == $length ){ //print 'definition for word: '.$word."\n"; $node->setDefinition('definition for world: '.$word); } } } //use this function when a user type a word that you want to check if exists and return the definition to user. this flow should be in AJAX request from client print getDefinition('bar',$anchor)."\n";
- 我在最大的函数上遇到了麻烦:main在上面添加了单词并调用了我编写的所有这些小函数。我很难做到
我甚至不知道我所做的是正确的还是错误的,如果有人能给我指出正确的方向,或者建议一种用javascript或php从TXT文件中编写字典的方法,那就太好了。首先,你要问的是你的方向是否正确。嗯,我想你是的。这可能不是今年的最佳实施方案,但您所说的所有内容都是一致的,而且看起来相当可靠 我不认为给你的问题一个直接的解决方案是一种教诲,因为你在和树打交道,而且你似乎没有太多的经验 但我可以给你一些提示和参考。实现“最大函数:)”的一个非常方便的方法是使用递归函数,该函数将在每个子函数上调用自身 我建议你去看看。它显示了一些树的示例,这些树看起来有点像您的树,并实现了一个完整的搜索算法,您可以根据自己的需要进行调整,而不会遇到太多问题 希望英语没有那么差,它会帮助你好的 这是一个包含单词的txt文件示例
//words.txt
hello
world
foo
bar
word\u dictionary.php用于解析txt文件,并具有检查树/字典中是否存在单词的方法
<?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 for getting a word definition from tree/dictionary.
// if word exists return definition, else return false
function getDefinition($word,$tree){
$node = $tree;
$length = strlen($word);
foreach(str_split($word) as $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 your tree/dictionary. This part is execute ONCE only for 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) as $index => $letter){
if($node->hasChild($letter)){
$node = $node->getChild($letter);
}
else{
$node = $node->addChild($letter);
}
if(($index+1) == $length ){
//print 'definition for word: '.$word."\n";
$node->setDefinition('definition for world: '.$word);
}
}
}
//use this function when a user type a word that you want to check if exists and return the definition to user. this flow should be in AJAX request from client
print getDefinition('bar',$anchor)."\n";
这是一个有趣的概念。。。您的目标是什么?功能节点(字母、定义、子节点)={}
-->语法错误?@IsmaelMiguel是的,对不起,没有很好地复制,没有=符号和Jason:只是一些尝试学习和理解更多树和JS的工作。您可以尝试使用对象
s来实现这一点。您可以创建一个新的对象
,而不是将函数分散在代码中,它将具有所有必需的工作方法。所有内容都包含在一个实例中。你能在你的问题中给出更多的信息吗?“定义”的目的是什么?如果你能提供这些,我可以给你一个PHP实现。好的,谢谢,我将研究这个方向:)谢谢,你确实帮了我:)