Support quanteda 4.5.0 (fix token type-table handling) - #6
Conversation
Since quanteda 4.5.0, subsetting a tokens object no longer compacts its type table: types inherited from the parent object are retained even when they no longer appear in the subset. Model vocabularies are built from the type table, so these phantom types inflated the vocabulary and broke the type/feature alignment in computeFcm(), causing "Subscript out of bounds" errors in coherence() and chains_scores(). Drop the unused types via a new recompileTypes() helper (using quanteda's tokens_recompile(force = TRUE)) at the single point where user-supplied tokens/dfm enter a model, in sentopicsmodel(). The helper is guarded to remain backward compatible with quanteda >= 3.2.0, where subsetting already compacts the type table. The recompaction is deliberately not applied in makeVocabulary(), since the conversion helpers build tokens from an external vocabulary whose zero-count entries must be preserved. Also update the as.tokens.dfm test fixture, which relied on the pre-4.5.0 behaviour of a subset tokens object.
sentopics_sentiment<- cleared stale per-topic sentiment docvars with an unqualified `docvars(x$tokens, c) <- NULL`. Only the docvars getter is imported from quanteda, so the replacement function was not found, raising "could not find function 'docvars<-'". Qualify the call as quanteda::docvars(...) <- NULL.
|
Hi @kbenoit, thank you for the pull request. I'm not merging it yet as i'm not sure this is the good fix. This will solve the issue for new models but the same errors with computeFcm() could still surface from converted models. So i'd like to investigate a bit further and either:
But digging further I encountered a compilation error trying to install quanteda 4.5.0. I'm on a Mac with M4 processor (ARM). Could it be that the identifier |
|
Hi @odelmarcelle thanks for pointing that out. I also work on an M4 Mac, but with TBB installed I did not encounter the problem. I uninstalled TBB and confirmed that it generates that error. This is fixed in quanteda/quanteda#2511, pending approval by @koheiw. On the backward compatibility, @koheiw is this something that can be disabled by sentopics to ensure backward compatibility, or should it recompile older objects when loading them. What's your recommendation for a fix? |
|
After digging into it, this looks like the most desirable solution. Because I'll merge soon into master and prepare a CRAN release for version 0.7.7. |
|
@kbenoit version 0.7.7 is on CRAN |
Why
quanteda 4.5.0 is currently held back on CRAN because it breaks its reverse dependency sentopics. This PR fixes sentopics so that quanteda 4.5.0 can be released.
The root cause is a behaviour change in quanteda 4.5.0: subsetting a tokens object no longer compacts its type table. A subset such as
ECB_press_conferences_tokens[1:10]now retains all types inherited from the parent object (e.g. 1168) instead of only the types that actually appear in the subset (e.g. 53).sentopics builds a model's vocabulary directly from
attr(toks, "types"), so these phantom types inflated every model's vocabulary. This surfaced as:chains_scores()/coherence()errors —computeFcm()indexed the full type table into a dfm that only contains the appearing features, givingError: Subscript out of bounds.types: Lengths (53, 1168) differ).What this PR does
Recompile the token type table at model creation. A new internal helper
recompileTypes()drops types that no longer appear, applied at the single point where user-suppliedtokens/dfmenter a model (sentopicsmodel(), the shared backend forLDA(),JST(),rJST()). This restores the pre-4.5.0 vocabulary.makeVocabulary(): theas.LDA/as.rJST/ ... conversion helpers build tokens from an external vocabulary whose zero-count entries must be preserved to keep the topic-word matrices aligned.quanteda (>= 3.2.0), where subsetting already compacts the type table.Fix an unqualified
docvars<-call insentopics_sentiment<-. Only thedocvarsgetter is imported from quanteda, so clearing stale per-topic sentiment docvars raisedcould not find function 'docvars<-'. Now qualified asquanteda::docvars(...) <- NULL.Update a stale test fixture (
as.tokens.dfm) that relied on the pre-4.5.0 behaviour of a subset tokens object.Verification
R CMD checkpasses on both examples and tests against quanteda 4.5.0:checking examples ... OKchecking tests ... OK(351 passing, 0 failures)No minimum-version bump is required; the fix works across quanteda 3.2.0 – 4.5.0+.