2015-04-01 49 views
2

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

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

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

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){ dictio.children [ASCII] = createChar(C); };

  • 而我在最大的功能上遇到了麻烦:主要是增加了这个词,并调用了我写的所有这些小函数。我有麻烦制作。

我甚至不知道我在做什么是对还是错,如果任何人都可以点我朝着正确的方向或暗示的JavaScript或php的方法做字典从TXT文件,该文件会很棒。

+0

这是一个有趣的概念......你的目标是什么? – Jason 2015-04-01 16:00:53

+0

'function Node(letter,definition,children)= {};' - >语法错误? – 2015-04-01 16:05:15

+0

@IsmaelMiguel对不起,没有复制,没有=标志 和Jason:只是一些工作,试图学习和了解更多的树木和JS – 2015-04-01 16:31:04

回答

0

好吧......

所以这是包含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"; 

希望它有点帮助;)

+0

谢谢,你确实帮了我:) – 2015-04-01 22:28:08

0

首先,你问你是否正朝着正确的方向前进。好吧,我想你是。这可能不是今年的最佳实施,但你所说的所有事情都是相互一致的,而且看起来很稳固。

我不认为给你一个直接的解决方案,你的问题将教学,因为你正在与树木工作,似乎你没有太多的经验与他们。

但我可以给你一些提示和参考。实现你的“最大功能:)”的一个非常方便的方法是使用一个递归函数,这个函数会在每个孩子身上调用它自己。我建议你看一下this wikipedia article。它显示了树看起来有点像你的例子,并实现一个完整的搜索算法,你可以适应你的需求没有太多的问题。

希望的英语还不错,而且它会帮助你

+0

好吧,谢谢,我打算看看那个方向:) – 2015-04-01 16:31:36