84.6k views
1 vote
Provide an example of counting words by utilising the power of an external library.

User Sindre
by
7.9k points

1 Answer

5 votes

Final answer:

To count words and letters using an external library in Python, you can utilize the power of the CountVectorizer class from the sklearn.feature_extraction.text module.

Step-by-step explanation:

To count words and letters using an external library, you can utilize the power of Python's CountVectorizer class from the sklearn.feature_extraction.text module. This library allows you to convert a collection of text documents into a matrix of word counts. Here's an example:

from sklearn.feature_extraction.text import CountVectorizer
corpus = ['This is the first document.','This document is the second document.','And this is the third one.']
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)
print(vectorizer.get_feature_names())
print(X.toarray())

In this example, the CountVectorizer is used to count words and letters in the given collection of documents, with the result being displayed as a matrix of word counts.

User Tech Vanguard
by
8.7k points