The TFIDF

Introduktion till naturlig språkbehandling i R

Kasey Jones

Research Data Scientist

Fallgropar med 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"
Introduktion till naturlig språkbehandling i R

Gemensamma ord

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

Jämför t1 och t2

  • 3/4 ord från t1 finns i t2
  • 3/5 ord från t2 finns i t1

Jämför t1 och t3

  • 2/4 ord från t1 finns i t3
  • 2/6 ord från t3 finns i t1
Introduktion till naturlig språkbehandling i R

Tacos spelar roll

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

Ord i varje text:

  • John: t1, t2
  • Joe: t1, t2, t3
  • Tacos: t1, t3
Introduktion till naturlig språkbehandling 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: Term Frequency
    • Andelen ord i en text som utgörs av det aktuella ordet
    • john är 1/4 ord i clean_t1, tf = .25
  • IDF: Inverse Document Frequency
    • Vikten av hur vanligt ett ord är i alla dokument
    • john finns i 3/3 dokument, IDF = 0
Introduktion till naturlig språkbehandling i R

IDF-formeln

 

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

  • N: totalt antal dokument i korpusen
  • $n_{t}$: antal dokument där termen förekommer

Exempel:

  • Taco IDF: $log (\frac{3}{2}) = .405$
  • Buddy IDF: $log (\frac{3}{1}) = 1.10$
  • John IDF: $log (\frac{3}{3}) = 0$
Introduktion till naturlig språkbehandling 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 för "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
Introduktion till naturlig språkbehandling i R

Beräkna TFIDF-matrisen

# 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: kolumnen som innehåller termerna
  • ID: kolumnen med dokument-ID
  • n: ordräkningen från count()
Introduktion till naturlig språkbehandling i R

Utdata från 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
 ...
Introduktion till naturlig språkbehandling i R

Nu kör vi en övning!

Introduktion till naturlig språkbehandling i R

Preparing Video For Download...