Skip to content

C++ tokens functions return stochastic ordering #2100

Description

@kbenoit

A user pointed out to me that the tokens ordering is indeterminate, when creating ngrams. Below, I set the threads to 1, just in case it was multithreading that was causing the issue.

library("quanteda")
## Package version: 3.0.0
## Unicode version: 10.0
## ICU version: 61.1
## Parallel computing: 1 of 12 threads used.
## See https://quanteda.io for tutorials and examples.

quanteda_options(threads = 1)
set.seed(123)

toks <- tokens(data_corpus_inaugural)
toks1 <- tokens_ngrams(toks, n = 2)
toks2 <- tokens_ngrams(toks, n = 2)

setequal(types(toks1), types(toks2))
## [1] TRUE
all(types(toks1) == types(toks2))
## [1] TRUE
tail(types(toks1))
## [1] "._Driven"      "Driven_by"     "by_conviction" "love_with"    
## [5] "God_protect"   "troops_."
tail(types(toks2))
## [1] "._Driven"      "Driven_by"     "by_conviction" "love_with"    
## [5] "God_protect"   "troops_."

This was already a known issue wit tokens_compound(), as in https://stackoverflow.com/questions/66256443/tokens-compound-in-quanteda-changes-the-order-of-features.

Is this something we should fix? Or just treat their ordering as indeterminate, like a Python set (but only on creation)?

Created on 2021-03-25 by the reprex package (v1.0.0)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions