用Javascript/PHP创建字典


Create Dictionary in Javascript / PHP

我正试图从树形状的.txt文件创建一个字典。在文本文件的每一行都有一个单词,我提取数组中的所有单词。

现在关于树,每个节点都包含一个字母,如果它是单词的最后一个字母则它包含一个定义,并且每个节点都有一个数组Children,它包含以相同方式开始的所有其他单词的字母。

所以我有这样定义的节点:

function Node(letter,definition,children) {
   this.letter = letter,
   this.definition = "",
   this.children = [] 
};

我有一个数组字典,它将包含所有节点。每个节点都将被组织起来(这样我们就知道"a"在Dictionary[0]中,"b"在Dicionary[1]中,依此类推)。

我定义了一些函数来帮助构建字典:

  • 检查Dictionary是否包含单词的第一个字母(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){dictio.childs[ascii]=createChar(c);};

  • 我在最大的函数上遇到了麻烦:main on添加了单词并调用了我编写的所有这些小函数。这是我遇到的麻烦。

我甚至不知道我所做的是对是错,如果有人能给我指明正确的方向,或者用javascript或php从TXT文件中提出一个做字典的方法,那就太好了。

好的。。。

这是一个包含单词的txt文件的例子

//words.txt
hello
world
foo
bar

word_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";

希望能有所帮助;)

首先,您要问的是您是否正朝着正确的方向前进。我想你是。这可能不是今年最好的实施方式,但你说的所有事情都是连贯一致的,而且看起来很扎实。

我不认为给你一个直接解决问题的方法是说教,因为你在处理树木,而且你似乎没有太多的经验。

但我可以给你一些提示和参考。实现"最大函数:)"的一种非常方便的方法是使用递归函数,该函数会在每个子函数上调用自己。

我建议你看看这篇维基百科文章。它展示了一个看起来有点像你的树的例子,并实现了一个完整的搜索算法,你可以在没有太多问题的情况下适应你的需求。

希望英语没有那么差,它会帮助你