TFIDF

R ile Doğal Dil İşlemeye Giriş

Kasey Jones

Research Data Scientist

Kelime torbası tuzakları

t1 <- "My name is John. My best friend is Joe. We like tacos."
t2 <- "Two common best friend names are John and Joe."
t3 <- "Tacos are my favorite food. I eat them with my buddy Joe."
clean_t1 <- "john friend joe tacos"
clean_t2 <- "common friend john joe names"
clean_t3 <- "tacos favorite food eat buddy joe"
R ile Doğal Dil İşlemeye Giriş

Ortak kelimeleri paylaşma

clean_t1 <- "john friend joe tacos"
clean_t2 <- "common friend john joe names"
clean_t3 <- "tacos favorite food eat buddy joe"

t1 ve t2 karşılaştırın

  • t1’deki 4 kelimenin 3’ü t2’de
  • t2’deki 5 kelimenin 3’ü t1’de

t1 ve t3 karşılaştırın

  • t1’deki 4 kelimenin 2’si t3’te
  • t3’teki 6 kelimenin 2’si t1’de
R ile Doğal Dil İşlemeye Giriş

Tacos önemli

t1 <- "My name is John. My best friend is Joe. We like tacos."
t2 <- "Two common best friend names are John and Joe."
t3 <- "Tacos are my favorite food. I eat them with my friend Joe."

Her metindeki kelimeler:

  • John: t1, t2
  • Joe: t1, t2, t3
  • Tacos: t1, t3
R ile Doğal Dil İşlemeye Giriş

TFIDF

clean_t1 <- "john friend joe tacos"
clean_t2 <- "common friend john joe names"
clean_t3 <- "tacos favorite food eat buddy joe"
  • TF: Terim Frekansı
    • Bir metindeki kelimelerin bu terim olma oranı
    • john, clean_t1 içinde 1/4, tf = .25
  • IDF: Ters Belge Frekansı
    • Terimin tüm belgelerde ne kadar yaygın olduğunun ağırlığı
    • john 3/3 belgede var, IDF = 0
R ile Doğal Dil İşlemeye Giriş

IDF denklemi

 

$ IDF = log \frac{N}{n_{t}} $

  • N: derlemdeki toplam belge sayısı
  • $n_{t}$: terimin göründüğü belge sayısı

Örnek:

  • Taco IDF: $log (\frac{3}{2}) = .405$
  • Buddy IDF: $log (\frac{3}{1}) = 1.10$
  • John IDF: $log (\frac{3}{3}) = 0$
R ile Doğal Dil İşlemeye Giriş

TF + IDF

clean_t1 <- "john friend joe tacos"
clean_t2 <- "common friend john joe names"
clean_t3 <- "tacos favorite food eat buddy joe"

"tacos" için TFIDF:

  • clean_t1: TF * IDF = (1/4) * (.405) = 0.101
  • clean_t2: TF * IDF = (0/4) * (.405) = 0
  • clean_t3: TF * IDF = (1/6) * (.405) = 0.068
R ile Doğal Dil İşlemeye Giriş

TFIDF matrisini hesaplama

# Create a data.frame
df <- data.frame('text' = c(t1, t2, t3), 'ID' = c(1, 2, 3))
df %>%
  unnest_tokens(output = "word", token = "words", input = text) %>%
  anti_join(stop_words) %>%
  count(ID, word, sort = TRUE) %>%
  bind_tf_idf(word, ID, n)
  • word: terimleri içeren sütun
  • ID: belge kimliklerini içeren sütun
  • n: count() tarafından üretilen kelime sayısı
R ile Doğal Dil İşlemeye Giriş

bind_tf_idf çıktısı

# A tibble: 15 x 6
       X word         n    tf   idf tf_idf
   <dbl> <chr>    <int> <dbl> <dbl>  <dbl>
 1     1 friend       1 0.25  0.405 0.101 
 2     1 joe          1 0.25  0     0     
 3     1 john         1 0.25  0.405 0.101 
 4     1 tacos        1 0.25  0.405 0.101 
 5     2 common       1 0.2   1.10  0.220 
 6     2 friend       1 0.2   0.405 0.0811
 ...
R ile Doğal Dil İşlemeye Giriş

TFIDF Alıştırması

R ile Doğal Dil İşlemeye Giriş

Preparing Video For Download...