Skip to content

Redesigning tokens_segment() #1011

Description

@koheiw

I hate to say, but we have to revisit tokens_segment (and probably corpus_segment), because tokens segmentation seems really important to solve two important issues (#516 and #521).

Negations #516

dict <- data_dictionary_LSD2015[1:2]

toks <- tokens("The share of the car is not good, but the color is nice.") # neutral sentence
mx <- dfm(toks, dictionary = dict)

# select 3 word-window after 'not'
toks_negation <- as.tokens(list(as.list(toks)[[1]][7:9])) 
# toks_negation <- tokens_segment(toks, 'not', window_after = 3) # proposed approach
mx_negation <- dfm(toks_negation, dictionary = dict)

as.vector(mx[,2] - mx[,1]) # 2 (incorrect)
as.vector(mx[,2] - mx[,1]) - (as.vector(mx_negation[,2] - mx_negation[,1]) * 2) # 0 (correct)

Target-word based collocation #521

# select 2 word-window before and after 'car'
toks_target <- as.tokens(list(as.list(toks)[[1]][3:7]))
# toks_negation <- tokens_segment(toks, 'not', window = 2) # proposed approach
mx_target <- dfm(toks_target)

textstat_keyness(rbind(dfm(toks_target), dfm(toks)))

# chi2         p n_target n_reference
# is     0.1307190 0.7176878        1           2
# of     0.0000000 1.0000000        1           1
# the    0.0000000 1.0000000        1           3
# car    0.0000000 1.0000000        1           1
# not    0.0000000 1.0000000        1           1
# share -0.3508772 0.5536170        0           1
# good  -0.3508772 0.5536170        0           1
# ,     -0.3508772 0.5536170        0           1
# but   -0.3508772 0.5536170        0           1
# color -0.3508772 0.5536170        0           1
# nice  -0.3508772 0.5536170        0           1
# .     -0.3508772 0.5536170        0           1

We can add this function in a different name from tokens_segment(), but the name seems the most appropriate. Current tokens_segment() should be called tokens_segment_tag() as tag extraction is a special case of segmentation.

Alternatively, we could treat this operations as selection of tokens, in stead of segmentation, and add a function called tokens_select_window().

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions