Produces the (query, reference) candidate pairs worth comparing, so
np_compare() never has to score every same-state pair. Three modes:
Usage
np_block(
query,
reference,
by = "state",
by_x = NULL,
by_y = NULL,
token = TRUE,
token_col = "name_key",
stopwords = np_stopwords(),
min_token_len = 2L,
max_ref_freq = NULL,
min_pair_idf = NULL,
concat_adjacent = FALSE,
ref_index = NULL
)Arguments
- query, reference
Normalized
np_query/np_referenceframes.- by
Character vector of exact-match block columns, or
NULL. Applied to both sides unlessby_x/by_yoverride.- by_x, by_y
Per-side block columns for cross-column matching (e.g.
by_x = c("state","name_key"),by_y = c("state","dba_key")pairs a query name against a reference DBA). Default toby. Columns are matched position-for-position, so the two must be the same length.- token
If
TRUE(default), also require a sharedtoken_coltoken.- token_col
Column to tokenize. Default
"name_key".- stopwords
Tokens to drop (see
np_stopwords()).- min_token_len
Minimum token length to index. Default 2.
- max_ref_freq
Optional: drop tokens occurring in more than this many reference records (kills corpus-common words).
NULL= stopwords only.- min_pair_idf
Optional IDF-weighted pruning. A single common token (e.g. "CALIFORNIA", "CHURCH") should not by itself justify a candidate pair, but a global
max_ref_freqcan't see that such a token is concentrated within one block (state). Withmin_pair_idfset, each shared token is weighted by its inverse document frequencylog(N_ref / df)and a pair is kept only when the sum of its shared tokens' IDF is at least this value. One rare token, or several moderately common ones, clears the bar; a lone common token does not.NULL(default) disables this. Only used whentoken.- concat_adjacent
If
TRUE, also index the concatenation of each adjacent pair of (post-stopword) tokens as an extra token. This recovers de-spacing differences symmetrically: "STEP FORWARD" emits the compound "STEPFORWARD" which matches a reference already spelled "STEPFORWARD", and vice versa. The compounds are rare (high IDF), so withmin_pair_idfset they only justify a pair when genuinely shared. DefaultFALSE. Only used whentoken.- ref_index
Optional precomputed
np_ref_index()for the reference's token index + document frequency. When supplied (and compatible withtoken_col/stopwords/min_token_len), the reference is not re-tokenized — the same candidate pairs are produced far faster on repeated calls (across passes and batches). Only used whentoken.
Value
A data frame of candidate pairs with integer columns .x (query row)
and .y (reference row), class np_blocks.
Details
exact (
token = FALSE) — pairs that match exactly on allbycolumns (a hash join). E.g.by = "state"orby = c("state", "zip5").token (
token = TRUE, default) — pairs that share the exactbykey and at least one distinctive token oftoken_col(a name-token inverted index). This is what makes blocking scale: "ALASKA X" is only paired with same-state orgs that share a rare token, not all ~40k orgs in the state.token, cross-key (
by = NULL,token = TRUE) — shared token only, no exact key. The loose recall-recovery pass (finds a same-name org in another state — the firm-vs-establishment case).
Both frames must be normalized (np_normalize()). Combine passes by
rbind-ing results and de-duplicating (see np_block_union()).
Query rows whose name_placeholder is TRUE ("SEE SCHEDULE O", "N/A") get
no candidates. Reference rows are not filtered. Frames normalized before that
column existed are blocked as before.