I am dealing with a large corpus in the form of a list of tokens/words. The corpus contains ~1900,000 words, I have run a code to get the most frequent words and now the corpus has 140,000 word.
I would like to remove the words that appear more than 95% of the document and less than 5%
sample of the corpus
['problems', 'guess', 'sleep', 'holy']
First I found the most frequent words
from nltk.probability import FreqDist
corpus_frequency = FreqDist(corpus)
corpus_commom=corpus_frequency.most_common()
Then, I applied this for loop to find the list of words that appear more than 95%
most_frequent=[mytuple for mytuple in corpus_commom if mytuple[1]<len(corpus*95)/100]
But this code takes very long time to run, and it does not return any output.
I also tried to follow some answers I found and apply CountVectorizer but I get an error message
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(min_df=0.05, max_df=0.95, lowercase=True)
X = cv.fit_transform(corpus)
The error message
ValueError: After pruning, no terms remain. Try a lower min_df or a higher max_df.
Update
The CountVectorizer works if I do this
cv = CountVectorizer(corpus,max_df=0.95)
count_vector=cv.fit_transform(corpus)
But it returns a list of numbers. I would like the output to be my corpus(list of words) filtered.
Can anyone give me a tip on how to achieve this? Thanks