简体   繁体   中英

Remove stopwords from most common words from set of sentences in Python

I have 5 sentences in a np.array and I want to find the most common n number of words that appear. For example if n=5 I would want the 5 most common words. I have an example below:

0    rt my mother be on school amp race
1    rt i am a red hair down and its a great
2    rt my for your every day and my chocolate
3    rt i am that red human being a man
4    rt my mother be on school and wear

Following is the code I used to get the most common n words.

import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

A = np.array(["rt my mother be on school amp race", 
              "rt i am a red hair down and its a great", 
              "rt my for your every day and my chocolate",
              "rt i am that red human being a man",
              "rt my mother be on school and wear"])

        n = 5
        vectorizer = CountVectorizer()
        X = vectorizer.fit_transform(A)

        vocabulary = vectorizer.get_feature_names()
        ind = np.argsort(X.toarray().sum(axis=0))[-n:]

        top_n_words = [vocabulary[a] for a in ind]

        print(top_n_words)

The results are as follow:

['school', 'am', 'and', 'my', 'rt']

However, what I want is to disregard stop-words like ' and ', ' am ' and ' my ' from these most common words. How could I accomplish this?

You just need to include the parameter stop_words='english' to CountVectorizer()

vectorizer = CountVectorizer(stop_words='english')

You should now get:

['wear', 'mother', 'red', 'school', 'rt']


Refer the documentation here: https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html

import numpy as np
from nltk.corpus import stopwords
from nltk.corpus import wordnet
from sklearn.feature_extraction.text import CountVectorizer

stop_words = set(stopwords.words('english'))

A = np.array(["rt my mother be on school amp race",
              "rt i am a red hair down and its a great",
              "rt my for your every day and my chocolate",
              "rt i am that red human being a man",
              "rt my mother be on school and wear"])
data = []
for i in A:
    d = i.split()
    s = ""
    for w in d:
        if w not in stop_words:
            s+=" "+w
    s = s.strip()
    data.append(s)

vect = CountVectorizer()
x = vect.fit_transform(data)
keyword = vect.get_feature_names()
list = x.toarray()
list = np.transpose(list)
l_total=[]
for i in list:
    l_total.append(sum(i))
n=len(keyword)
for i in range(n):
    for j in range(0, n - i - 1):
        if l_total[j] > l_total[j + 1]:
            l_total[j], l_total[j + 1] = l_total[j + 1], l_total[j]
            keyword[j], keyword[j + 1] = keyword[j + 1], keyword[j]
keyword.reverse()
print(keyword[:5])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM