Can I print bag of words and term frequency in text format?

Viewed 96
from sklearn.feature_extraction.text import CountVectorizer  

#instantiate vectorizer
vectorizer=CountVectorizer()   

#Document creation 
document1='this is a sunny day';document2= 'today is a very very very pleasant day and we have fun fun fun';document3= 'this is an amazin experience'

#list 
list_of_words= [document1,document2,document3]

#bag of words
bag_of_words = vectorizer.fit(list_of_words)

#verify vocabulary of repeated word 
print (vectorizer.vocabulary_.get('very')) 

print (vectorizer.vocabulary_.get('fun'))

#transform
bag_of_words=vectorizer.transform(list_of_words)

print(bag_of_words)>>>>
(0, 3) 1 (0, 7) 1 (0, 9) 1 (0, 10) 1 (1, 2) 1 (1, 3) 1 (1, 5) 3 (1, 6) 1 (1, 7) 1 (1, 8) 1 (1, 11) 1 (1, 12) 3 (1, 13) 1 (2, 0) 1 (2, 1) 1 (2, 4) 1 (2, 7) 1 (2, 10) 1
1 Answers

Please have a look at the below. The stopwords list provided by nltk can be optionally used to remove any stopwords from the texts.

from sklearn.feature_extraction.text import CountVectorizer  
import pandas as pd
import nltk
from nltk.corpus import stopwords

nltk.download('stopwords') # you need to run this only once to download the stopwords list
stop_words_list = list(stopwords.words('english')) # load the stop words

#Document creation 
document1='this is a sunny day';document2= 'today is a very very very pleasant day and we have fun fun fun';document3= 'this is an amazin experience'

#list 
list_of_documents= [document1,document2,document3]

#instantiate vectorizer
coun_vect = CountVectorizer(stop_words=stop_words_list)

# fit and transform
count_matrix = coun_vect.fit_transform(list_of_documents)
count_array = count_matrix.toarray()

# create a dataframe with words and their respective frequency (each row represents a document starting from document1)
df = pd.DataFrame(data=count_array, columns = coun_vect.get_feature_names_out())
print(df)
# df.to_csv("bag_of_words.csv") # optionally save the df to a csv file
Related