concept similarity in category recommender system

This is an excerpt from Manning's book Practical Recommender Systems.
Generally, people tend to buy something because they like it. In fact, Susan creates more problems than not for two reasons: the system will spend energy looking for similarities but won’t find any and, if Susan does buy something more popular, you suddenly have a link between a popular film and something that was only ever viewed or purchased by one user.
You’ll look at how to calculate similarity between sets of items.
Chapter 6 described non-personalized recommendations and the association rules. Association rules are a way to connect content without looking at the item or the users who consumed them. Personalized recommendations, however, almost always contain calculations of similarity. An example of such recommendations could be Netflix’s More Like This recommendation shown in figure 7.1, where it uses an algorithm to find similar content.
In later sections, you’ll learn ways of measuring whether two items or two users are similar. You’ll first look at how to calculate similarity on binary data (bought versus not bought) and then move on to measure the similarity between users based on their ratings (and items based on how the same users rate them). You can use the tools that measure similarity as an instrument to understand the similarities among your catalog’s content. In chapter 8, you’ll put that to good use when implementing collaborative filtering algorithms.
You can calculate similarity in many ways, but the overall problem can be defined as follows: Given two items, i1 and i2, the similarity between them is given by the function sim(i1, i2).
This function’s return values will increase the more similar the items are. We can say that the similarity between the same item is Sim(i1, i1) = 1, and two items that have nothing in common will be Sim(i1, nothing in common with i1) = 0. Figure 7.2 illustrates two examples of similarity functions. It’s worth thinking about which you should choose. Which is better depends on your domain and data.
Listing 9.4. Similarity function test
def test_simple_similarity(self): builder = ItemSimilarityMatrixBuilder(0) no_items = len(set(self.ratings['movie_id'])) cor = builder.build(ratings=self.ratings, save=False) self.assertIsNotNone(cor) self.assertEqual(cor.shape[0], no_items, "Expected correlations matrix to have a row for each item") self.assertEqual(cor.shape[1], no_items, "Expected correlations matrix to have a column for each item") self.assertEqual(cor[WONDER_WOMAN][AVENGERS], - 1, "Expected Wolverine and Star Wars to have similarity 0.5") self.assertEqual(cor[AVENGERS][AVENGERS], 1, "Expected items to be similar to themselves similarity 1")