Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, I would store the relationships as arrays so that you can have duplicate "keys." Key methods: an initial dictionary including every word related to each individual word; a recursive chain expander using map and reduce; filtering chains based on equivalency.</p> <pre><code>Array.prototype.getUnique = function(){ var u = {}, a = []; for(var i = 0, l = this.length; i &lt; l; ++i){ if(u.hasOwnProperty(this[i])) { continue; } a.push(this[i]); u[this[i]] = 1; } return a; } var links = {}; var pairs = [ ["car", "wheel"], ["wheel", "tyre"], ["bed", "sheets"], ["guitar", "strings"], ["guitar", "pickup"], ["rubber", "tyre"], ["truck", "wheel"], ["pickup", "car"] ]; pairs.map(function(pair) { links[pair[0]] = links[pair[0]] || []; links[pair[1]] = links[pair[1]] || []; links[pair[0]].push(pair[1]); links[pair[1]].push(pair[0]); }); var append = function(list) { var related = list.map(function(item) { return links[item]; }).reduce(function(listA, listB) { return listA.concat(listB); }).filter(function(item) { // make sure related only includes new links return list.indexOf(item) == -1 }).getUnique(); return related.length ? append(list.concat(related)) : list.concat(related); }; var branches = []; for( var word in links ) { branches.push(append(links[word].concat(word))); } var compareArrays = function(listA, listB) { if( listA.length != listB.length ) return false; return listA.map(function(element) { if( listB.indexOf(element) == -1 ) return 0; return 1; }).filter(function(el) { return el == 1; }).length == listA.length; }; var _branches = branches; var chains = branches.filter(function(branch1, i) { var isUnique = _branches.filter(function(branch2) { // are they equivalent return compareArrays(branch1, branch2); }).length == 1; delete _branches[i]; return isUnique; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload