Tf-idf с gensim

Введение в обработку естественного языка на Python

Katharine Jarmul

Founder, kjamistan

Что такое tf-idf?

  • Term frequency - inverse document frequency
  • Позволяет определить наиболее важные слова в каждом документе
  • В корпусе могут быть общие слова помимо стоп-слов
  • Их вес должен быть снижен
  • Пример из астрономии: «Небо»
  • Самые частотные слова не попадают в ключевые
  • Частые слова, характерные для документа, сохраняют высокий вес
Введение в обработку естественного языка на Python

Формула tf-idf

$$w_{i,j} = tf_{i,j} * \log (\frac{N}{df_i})$$

$$w_{i,j} = \textnormal t \textnormal f \textnormal - \textnormal i \textnormal d \textnormal f \space \textnormal w \textnormal e \textnormal i \textnormal g \textnormal h \textnormal t \space \textnormal f \textnormal o \textnormal r \space \textnormal t \textnormal o \textnormal k \textnormal e \textnormal n \space i \space \textnormal i \textnormal n \space \textnormal d \textnormal o \textnormal c \textnormal u \textnormal m \textnormal e \textnormal n \textnormal t \space j $$

$$tf_{i,j} = \textnormal n \textnormal u \textnormal m \textnormal b \textnormal e \textnormal r \space \textnormal o \textnormal f \space \textnormal o \textnormal c \textnormal c \textnormal u \textnormal r \textnormal e \textnormal n \textnormal c \textnormal e \textnormal s \space \textnormal o \textnormal f \space \textnormal t \textnormal o \textnormal k \textnormal e \textnormal n \space i \space \textnormal i \textnormal n \space \textnormal d \textnormal o \textnormal c \textnormal u \textnormal m \textnormal e \textnormal n \textnormal t \space j $$

$$df_i = \textnormal n \textnormal u \textnormal m \textnormal b \textnormal e \textnormal r \space \textnormal o \textnormal f \space \textnormal d \textnormal o \textnormal c \textnormal u \textnormal m \textnormal e \textnormal n \textnormal t \textnormal s \space \textnormal t \textnormal h \textnormal a \textnormal t \space \textnormal c \textnormal o \textnormal n \textnormal t \textnormal a \textnormal i \textnormal n \space \textnormal t \textnormal o \textnormal k \textnormal e \textnormal n \space i $$

$$N = \textnormal t \textnormal o \textnormal t \textnormal a \textnormal l \space \textnormal n \textnormal u \textnormal m \textnormal b \textnormal e \textnormal r \space \textnormal o \textnormal f \space \textnormal d \textnormal o \textnormal c \textnormal u \textnormal m \textnormal e \textnormal n \textnormal t \textnormal s$$

Введение в обработку естественного языка на Python

Tf-idf с gensim

from gensim.models.tfidfmodel import TfidfModel

tfidf = TfidfModel(corpus)
tfidf[corpus[1]]
[(0, 0.1746298276735174),
 (1, 0.1746298276735174),
 (9, 0.29853166221463673),
 (10, 0.7716931521027908),
...
]
Введение в обработку естественного языка на Python

Давайте потренируемся!

Введение в обработку естественного языка на Python

Preparing Video For Download...