В НЛП с помощью tf-idf, как найти частоту конкретного слова из корпуса (включая большое количество документации) в Python - PullRequest
1 голос
/ 11 апреля 2019

Как найти частоту отдельного слова из корпуса, используя Tf-idf.Ниже приведен мой пример кода, теперь я хочу напечатать частоту слова.Как мне этого добиться?

from sklearn.feature_extraction.text import CountVectorizer

vectorizer = CountVectorizer()
corpus = ['This is the first document.',
      'This is the second second document.',
      'And the third one.',
      'Is this the first document?',]
X = vectorizer.fit_transform(corpus)
X
print(vectorizer.get_feature_names())
X.toarray()
vectorizer.vocabulary_.get('document')

print(vectorizer.get_feature_names())

X.toarray()

vectorizer.vocabulary_.get('document')

1 Ответ

0 голосов
/ 11 апреля 2019

Ваш vectorizer.vocabulary_ имеет счетчик для каждого слова:

print(vectorizer.volcabulary_)

{'this': 8,
 'is': 3,
 'the': 6,
 'first': 2,
 'document': 1,
 'second': 5,
 'and': 0,
 'third': 7,
 'one': 4}

Расчет частоты слова является простым, тогда:

vocab = vectorizer.vocabulary_
tot = sum(vocab.values())
frequency = {vocab[w]/tot for w in vocab.keys()}
...