Javascript 如何查找对象之间的关系 对于有类似问题的人(在找到解决方案后编写):

Javascript 如何查找对象之间的关系 对于有类似问题的人(在找到解决方案后编写):,javascript,python,arrays,recursion,go,Javascript,Python,Arrays,Recursion,Go,根据下面的答案,您可能会注意到,这个问题有很多不同的解决方案。我之所以选择Evan's,是因为它是我在自己的代码中实现的最简单的方法。然而,从我的尝试来看,其他所有答案都有效。链接这是肯定有趣的,我建议阅读,如果你有兴趣。Prolog也是作为一种可能的解决方案提出来的,我对它不熟悉,但如果你已经知道了,它可能值得考虑。另外,如果您只想使用代码,下面有一些Javascript和Python示例。然而,每一种解决方案都有不同的方法,我不确定哪种方法最有效(请自行测试) 进一步的方法/阅读:

根据下面的答案,您可能会注意到,这个问题有很多不同的解决方案。我之所以选择Evan's,是因为它是我在自己的代码中实现的最简单的方法。然而,从我的尝试来看,其他所有答案都有效。链接这是肯定有趣的,我建议阅读,如果你有兴趣。Prolog也是作为一种可能的解决方案提出来的,我对它不熟悉,但如果你已经知道了,它可能值得考虑。另外,如果您只想使用代码,下面有一些Javascript和Python示例。然而,每一种解决方案都有不同的方法,我不确定哪种方法最有效(请自行测试)

进一步的方法/阅读:


很抱歉,标题让人困惑,我想不出一个恰当的方式来表达我的问题——欢迎有更好的想法

因为我很难描述我的问题,我会尽可能多地解释我的目标和代码:

注意:我这里的代码是Go,但我也很乐意用其他语言回答,如果您有任何问题,我会尽快回答

基本上,我有一个“Word”对象数组,如下所示:

type Word struct{
     text     string
     synonyms []string
}
areWordsRelated(word1 Word, word2 Word) bool{
    for _, elem := range word1.synonyms{
         if elem == word2.text{
             return true
         }
    }
    return false
}
这是数组中4个单词的示例:

  []Word{
      {text: "cat" synonyms: ["feline", "kitten", "mouser"]}
      {text: "kitten" synonyms: ["kitty", "kit"]} 
      {text: "kit" synonyms: ["pack", "bag", "gear"]}
      {text: "computer" synonyms: ["electronics", "PC", "abacus"]}
   }
我的挑战是写一个方法来测试两个单词之间的关系。当然,在上面的例子中,在“cat”和“kitten”这两个词之间进行测试是很容易的。我可以检查“Cat”的同义词列表,然后测试它是否包含“kitten”。代码如下:

type Word struct{
     text     string
     synonyms []string
}
areWordsRelated(word1 Word, word2 Word) bool{
    for _, elem := range word1.synonyms{
         if elem == word2.text{
             return true
         }
    }
    return false
}
然而,我不知道如何测试一段更遥远的关系。

例如:

areWordsRelated("cat","pack") //should return true 
//because "cat" is related to "kitten" which is related to "pack"
areWordsRelated("cat", "computer") //should return false
我试着递归地做,但我所有的尝试似乎都不起作用。任何示例代码(我的代码在Go中,但Python、Java或Javascript也可以)、伪代码或只是解释都非常好。

Python解决方案:

class Word:

   # Dictionary of Words, keyed by name.
   word_dict = {}

   def __init__(self, name, synonyms):
      self.name = name
      self.synonyms = synonyms

      # Update the dictionary.
      Word.word_dict[name] = self
      for s in synonyms:
         if not s in Word.word_dict:
            Word.word_dict[s] = Word(s, [])

   def isAncestor(self, other):
      if other in self.synonyms:
         return True
      for s in self.synonyms:
         if Word.word_dict[s].isAncestor(other):
            return True
      return False

def areWordsRelated(word1, word2):
   if not word1 in Word.word_dict or not word2 in Word.word_dict:
      return False
   return Word.word_dict[word1].isAncestor(word2) or Word.word_dict[word2].isAncestor(word1)

words = []
words.append(Word("cat", ["feline", "kitten", "mouser"]))
words.append(Word("kitten", ["kitty", "kit"]))
words.append(Word("kit", ["patck", "bag", "gear"]))
words.append(Word("computer", ["electronics", "PC", "abacus"]))

print(areWordsRelated("cat", "kit"))
print(areWordsRelated("kit", "cat"))
print(areWordsRelated("cat", "computer"))
print(areWordsRelated("dog", "computer"))
输出:

True
真的
假的
假的

如果你给我一些反馈,我可以编辑它,因为它不完全符合你的要求,但它是jist。我将编辑一个技术解释,说明为了满足您的确切示例,必须更改哪些内容

package main

import "fmt"

func main() {
    words := []Word{
            {text: "cat", synonyms: []string{"feline", "kitten", "mouser"}},
            {text: "kitten", synonyms: []string{"kitty", "kit"}} ,
            {text: "kit", synonyms: []string{"pack", "bag", "gear"}},
            {text: "computer", synonyms: []string{"electronics", "PC", "abacus"}},
    }

    fmt.Println(areWordsRelated(words, words[0], words[2]))
    fmt.Println(areWordsRelated(words, words[0], words[3]))
}

type Word struct{
     text     string
     synonyms []string
}

func areWordsRelated(words []Word, word1, word2 Word) bool {
    for _, elem := range word1.synonyms{
        if elem == word2.text{
            return true
        } else {
            for _, word := range words {
                if word.text == elem {
                    if (areWordsRelated(words, word, word2)) {
                        return true
                    }
                }
            }
        }
    }
    return false
}
编辑:这与您要求的不完全一样,因为它没有在“pack”和“cat”之间建立连接,因为pack不是由实际的word对象表示的,我定义了将word2作为对象接收的方法(只是根据您的示例)。我可以改为把它做成一个字符串,这样它可以在返回之前检查“kit”的同义词数组中的“pack”,但是想法还是一样的。。。下面是对算法的高级解释

迭代同义词,如果不匹配,则在原始集合中找到
Word
对象,并将其作为第一个参数调用。这将递归地耗尽每个路径,直到找到匹配的路径为止,或者没有剩余的路径,在这种情况下,您处于返回false的循环之外。上面的代码在go Playerd中运行,并正确返回
true\n false
。请注意,递归调用是在if中进行的,以防止过早返回false(这也是一种性能增强,因为我们会在找到true后立即返回,而不是继续递归路径)


这里是一个用JavaScript编写的递归算法示例,其中加入了一些jQuery以使搜索数组更容易。它可能会被优化,但应该给你一些东西开始

$(函数(){
变量字=[{
文字:“猫”,
同义词:[“猫”、“小猫”、“老鼠”]
}, {
文字:“小猫”,
同义词:[“kitty”,“kit”]
}, {
文字:“工具包”,
同义词:[“包”、“包”、“装备”]
}, {
文字:“计算机”,
同义词:[“电子”、“PC”、“算盘”]
}];
console.log(相关单词('cat','pack',words));
console.log(相关词('cat','rack',words));
});
函数arewords相关(parentWord、childWord、list){
var parentWordItems=$.grep(列表,函数(元素){
return element.text==parentWord;
});
if(parentWordItems.length==0){
返回错误
}否则{
var parentWordItem=parentWordItems[0];
var remainingItems=$.grep(列表,函数(元素){
return element.text!==parentWord;
});
if(parentWordItem.synonyms.indexOf(childWord)>=0){
返回true;
}否则{
for(var i=0;i

您看到的是二级关系(与您已经知道如何查找的“简单”第一名示例相反),这意味着您必须做以下两件事之一:

(1) 存储量大的解决方案需要维护一个单独的二级关系列表,然后只需在该(较长)列表中进行搜索—这需要维护(可能更多)有关单词关系的数据。例如,如果有10000个单词,每个单词大约有10个同义词,那么存储的一级关系就有100000个。但是你会有几十亿个二级关系。当然,这很快就会变得很笨拙

在这种情况下,每个条目如下所示: {文本:“猫”同义词:[“猫”、“小猫”、“老鼠”]秒:[“包”,…] ... 您只需编写一个单独的函数来检查“同义词”或“秒”中的关系

(2) 编程解决方案仍然是只存储一级关系,然后执行嵌入式循环

在这种情况下:

//// This checks for 1st degree relationship
areWordsRelated1(word1 Word, word2 Word) bool{
    for _, elem := range word1.synonyms{
         if elem == word2.text{
             return true
         }
    }
    return false
}

//// This checks for 2nd degree by checking 1st and then, if not, 
//// then trying the 1st degree function on the children of word2
//// before giving up and returning false
areWordsRelated2(word1 Word, word2 Word) bool{
    for _, elem1 := range word1.synonyms{
         if elem1 == word2.text{
             return true
         } else {
         for _, elem2 := range elem1.synonyms{
             if areWordsRelated1(word1, elem2) {
                 return true
             }
         }
    }
    return false
}

注意:我注意到在您的样本数据中,“cat”与“kitten”相关,但“kitten”与“cat”没有相反的关系

首先,不清楚你是怎么做的