【问题标题】:Create Dictionary in Javascript / PHP在 Javascript / PHP 中创建字典
【发布时间】:2015-06-06 08:39:03
【问题描述】:

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

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

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

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

我有一个包含所有节点的数组字典。每个节点都会被组织起来(这样我们就知道 'a' 在 Dictionary[0] 中,而 'b' 在 Dictionary[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.children[ascii] = createChar(c); };

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

我什至不知道我在做什么是对还是错,如果有人能指出我正确的方向或建议一种在 javascript 或 php 中从 TXT 文件中做字典的方法,那就太好了。

【问题讨论】:

  • 这是一个有趣的概念……你的目标是什么?
  • function Node(letter,definition,children) = {}; --> 语法错误?
  • @IsmaelMiguel 是的,抱歉,没有很好地复制,没有 = 符号和 Jason:只是一些工作来尝试学习和理解更多的树和 JS
  • 您可以尝试使用Objects 来实现它。您无需将函数分散在代码中,而是创建一个新的Object,它将具有所有必需的工作方法。所有内容都包含在一个实例中。
  • 您能否提供更多信息,例如:您问题中的真实示例? “定义”的目的是什么?如果你能提供这些,我可以给你一个 PHP 实现。

标签: javascript php arrays


【解决方案1】:

好的...

所以这是一个包含单词的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";

希望对你有所帮助;)

【讨论】:

  • 谢谢,你帮了大忙 :)
【解决方案2】:

首先,您是在问自己是否朝着正确的方向前进。嗯,我想你是。这可能不是今年最好的实现,但您所说的所有内容仍然相互一致,而且看起来很扎实。

我不认为给你一个直接的解决方案是有教益的,因为你正在研究树木,而且你似乎对它们没有太多经验。

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

我建议你看看this wikipedia article。它展示了一些看起来有点像你的树的例子,并实现了一个完整的搜索算法,你可以适应你的需要而没有太多问题。

希望英语没那么糟糕,它会帮助你

【讨论】:

  • 好的,谢谢,我会朝那个方向研究 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
相关资源
最近更新 更多