jbrooksuk/node-summary — pull request #20 by ageitgey (2014-07-16 05:42:13+00:00)
Title: Intersect function doesn't work causing summary results to be unreliable

Hi,

I don't think this module actually works as intended because there is an issue with how you are calculating sentence intersections.

The `intersect_safe` function assumes that the lists it is comparing are already sorted. If the lists aren't sorted, it doesn't return the correct intersection.

The way it's used in the code, the lists being compared aren't sorted. So the `intersect_safe` function doesn't return the correct word intersections between sentences which throws the end results off.

A simple fix is to just use the built-in lodash `intersection` function instead of using the custom one. It looks like you used to do this but the behavior changed in 3d9e24d11a5a5088e85cd861cdf5293a105ffa58.

Here's a test case that shows the broken behavior:

```javascript
var _ = require('lodash-node/underscore');

function intersect_safe(a, b) {
var ai = 0, bi=0;
var result = [];

while(ai < a.length && bi < b.length){
if (a[ai] < b[bi] ){ ai++; }
else if (a[ai] > b[bi] ){ bi++; }
else /* they're equal */
{
result.push(a[ai]);
ai++;
bi++;
}
}

return result;
}

s1 = "The game's setting is memorable and quirky, with inexplicable animal-human hybrid inhabitants like the Goatician, a goat-man Magician who sells health upgrades, or the Troupple King, a gigantic, apple-shaped fish who, after a ceremonial dance, spits out various flavors of helpful potions.";
s2 = "There's a bunch of upgrades, equipment and items to purchase from the vendors in Shovel Knight's world, but you'll earn enough cash to buy all of them well before the game's ending.";

l1 = s1.split(" ");
l2 = s2.split(" ");

console.log("Unsorted lists - intersect_safe results the wrong results:")
console.log(intersect_safe(l1, l2));
console.log(_.intersection(l1, l2));

console.log("If the lists were sorted, the results would be correct-ish:")
l1.sort();
l2.sort();
console.log(intersect_safe(l1, l2));
console.log(_.intersection(l1, l2));
```

And here's the output:
```
Unsorted lists - intersect_safe results the wrong results:
[]
[ 'game\'s', 'and', 'the', 'a', 'upgrades,', 'of' ]

If the lists were sorted, the results would be correct-ish:
[ 'a', 'and', 'game\'s', 'of', 'the', 'the', 'upgrades,' ]
[ 'a', 'and', 'game\'s', 'of', 'the', 'upgrades,' ]
```

Thanks!