concept length in category nlp

This is an excerpt from Manning's book Getting Started with Natural Language Processing.
The measurement of distance in geometrical space originates with the old good Pyphagorean theorem that you should be familiar with from the high school mathematics course. Here’s a refresher: in a right triangle, the square of the hypotenuse (the side opposite to the right angle) length equals to the sum of the squares of the other two sides’ lengths. That is, to measure the distance between two points in the geometrical space, we can draw a right triangle such that the distance between the two points will equal the length of the hypotenuse, and calculate this distance using Pyphagorean theorem. Why this works? Because the length of each side is simply the difference in the coordinates, and we know the coordinates! In other words:
Figure 1.7 In a right triangle, the length of hypotenuse can be estimated from the lengths of the other two sides
![]()
Figure 1.7 In a right triangle, the length of hypotenuse can be estimated from the lengths of the other two sides
![]()
Listing 7.6 Code to calculate statistics on word lemmas def statistics_lem(source_docs): #A length = 0 vocab = [] for review_id in source_docs.keys(): review_doc = source_docs.get(review_id) lemmas = [] for token in review_doc: lemmas.append(token.lemma_) #B length += len(lemmas) vocab += lemmas avg_length = float(length)/len(source_docs) vocab_size = len(set(vocab)) diversity = float(length)/float(vocab_size) return avg_length, vocab_size, diversity categories = ["Positive", "Negative"] rows = [] rows.append(["Category", "Avg_Len(Review)", "Vocabulary Size", "Diversity"]) stats = {} stats["Positive"] = statistics_lem(pos_docs) stats["Negative"] = statistics_lem(neg_docs) for cat in categories: rows.append([cat, f"{stats.get(cat)[0]:.6f}", f"{stats.get(cat)[1]:.6f}", f"{stats.get(cat)[2]:.6f}"]) columns = zip(*rows) column_widths = [max(len(item) for item in col) for col in columns] for row in rows: print(''.join(' {:{width}} '.format(row[i], width=column_widths[i]) for i in range(0, len(row)))) #C
Here is what you will get if you run this code: the length of both texts_neg (texts from the movie reviews, with punctuation marks removed and negation marked via mark_negation) and labels keeping the actual labels of all reviews is still 2,000 as expected. The content of the first review, which as before starts with “the central focus of …” will only be affected where the review contains negative words, for instance: