TFIDF

Nhập môn Xử lý Ngôn ngữ Tự nhiên với R

Kasey Jones

Research Data Scientist

Hạn chế của bag-of-words

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"
Nhập môn Xử lý Ngôn ngữ Tự nhiên với R

Chung nhiều từ

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

So sánh t1 và t2

  • 3/4 từ của t1 có trong t2
  • 3/5 từ của t2 có trong t1

So sánh t1 và t3

  • 2/4 từ của t1 có trong t3
  • 2/6 từ của t3 có trong t1
Nhập môn Xử lý Ngôn ngữ Tự nhiên với R

Tacos quan trọng

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."

Từ trong mỗi văn bản:

  • John: t1, t2
  • Joe: t1, t2, t3
  • Tacos: t1, t3
Nhập môn Xử lý Ngôn ngữ Tự nhiên với R

TFIDF

clean_t1 <- "john friend joe tacos"
clean_t2 <- "common friend john joe names"
clean_t3 <- "tacos favorite food eat buddy joe"
  • TF: tần suất thuật ngữ
    • Tỷ lệ từ trong văn bản là thuật ngữ đó
    • john chiếm 1/4 từ trong clean_t1, tf = .25
  • IDF: tần suất nghịch tài liệu
    • Mức độ phổ biến của thuật ngữ trên toàn bộ tài liệu
    • john xuất hiện trong 3/3 tài liệu, IDF = 0
Nhập môn Xử lý Ngôn ngữ Tự nhiên với R

Công thức IDF

 

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

  • N: tổng số tài liệu trong corpus
  • $n_{t}$: số tài liệu có chứa thuật ngữ

Ví dụ:

  • IDF của Taco: $log (\frac{3}{2}) = .405$
  • IDF của Buddy: $log (\frac{3}{1}) = 1.10$
  • IDF của John: $log (\frac{3}{3}) = 0$
Nhập môn Xử lý Ngôn ngữ Tự nhiên với R

TF + IDF

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

TFIDF cho "tacos":

  • 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
Nhập môn Xử lý Ngôn ngữ Tự nhiên với R

Tính ma trận TFIDF

# 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: cột chứa các thuật ngữ
  • ID: cột chứa ID tài liệu
  • n: số lần xuất hiện do count() tạo ra
Nhập môn Xử lý Ngôn ngữ Tự nhiên với R

Kết quả bind_tf_idf

# 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
 ...
Nhập môn Xử lý Ngôn ngữ Tự nhiên với R

Luyện TFIDF

Nhập môn Xử lý Ngôn ngữ Tự nhiên với R

Preparing Video For Download...