Skip to content

Danger when using tokens from a un-ordered corpus #25

Description

@odelmarcelle

I like tokenizing text by myself before using compute_sentiment. My usual framework is to start from a quanteda::corpus, from which I create a sento_corpus and a quanteda::tokens object.

I just realized that since as.sento_corpus re-order the quanteda::corpus, the order of the sento_corpus and the tokens object do not match. This leads to the wrong allocation of sentiment to texts.

I realize that in an ideal world, the safest way would be to use as.list(tokens(x)) when calling compute_sentiment. But I feel that this error is very difficult to notice as there is no warning, and I see situations where you would handle tokenization separately from the sento_corpus object.

Reproducible example:

> library(quanteda)
> library(sentometrics)
> e <- data.frame(text = c("good good good", "bad bad bad bad bad"), date = c("2000-01-26", "2000-01-03"))
> 
> corp <- corpus(e)
> st <- as.sento_corpus(corp)
We detected no features, so we added a dummy feature 'dummyFeature'.
> lex <- sento_lexicons(list_lexicons["GI_en"])
> 
> compute_sentiment(st, lex)
      id       date word_count GI_en--dummyFeature
1: text2 2000-01-03          5                  -1
2: text1 2000-01-26          3                   1
> compute_sentiment(st, lex, tokens = as.list(tokens(corp)))
      id       date word_count GI_en--dummyFeature
1: text2 2000-01-03          3                   1
2: text1 2000-01-26          5                  -1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions