Найти наиболее распространенные слова из набора предложений в Python - PullRequest
0 голосов
/ 09 июля 2019

У меня есть 5 предложений в массиве np.array, и я хочу найти наиболее распространенное число n отображаемых слов. Например, если n было 3, я хотел бы 3 наиболее распространенных слова. У меня есть пример ниже:

0    oh i am she cool though might off her a brownie lol
1    so trash wouldnt do colors better tweet
2    love monkey brownie as much as a tweet
3    monkey get this tweet around i think
4    saw a brownie to make me some monkey

Если бы n было 3, я бы хотел напечатать слова: брауни, обезьяна, твит. Есть ли простой способ сделать что-то подобное?

1 Ответ

2 голосов
/ 09 июля 2019

Вы можете сделать это с помощью CountVectorizer, как показано ниже:

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

A = np.array(["oh i am she cool though might off her a brownie lol", 
              "so trash wouldnt do colors better tweet", 
              "love monkey brownie as much as a tweet",
              "monkey get this tweet around i think",
              "saw a brownie to make me some monkey" ])

n = 3
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)
['tweet', 'monkey', 'brownie']

Надеюсь, это поможет!

...