在 R 中使用 text2vec 计算基于未加权词袋的 TCM?
Compute unweighted bag-of-words based TCM using text2vec in R?
我正在尝试使用 R
中的 text2vec
包从语料库计算术语-术语共现矩阵(或 TCM)(因为它有一个很好的并行后端)。我遵循了 this tutorial,但在检查一些玩具示例时,我注意到 create_tcm
函数对术语共现值进行了某种缩放或加权。我知道它在内部使用 skip-grams,但文档没有提到它如何缩放它们 - 显然,更远的 terms/unigrams 权重较低。
这是一个例子:
tcmtest = function(sentences){
tokens <- space_tokenizer(sentences)
it = itoken(tokens, progressbar = FALSE)
vocab <- create_vocabulary(it, ngram = c(ngram_min = 1L, ngram_max = 1L))
vectorizer <- vocab_vectorizer(vocab, grow_dtm = FALSE, skip_grams_window = 5L)
return(create_tcm(it, vectorizer))
}
> tcmtest(c("a b", "a b c"))
3 x 3 sparse Matrix of class "dgTMatrix"
b c a
b . 1 2.0
c . . 0.5
a . . .
> tcmtest(c("a b", "c a b"))
3 x 3 sparse Matrix of class "dgTMatrix"
b c a
b . 0.5 2
c . . 1
a . . .
> tcmtest(c("a b", "c a a a b"))
3 x 3 sparse Matrix of class "dgTMatrix"
b c a
b . 0.25 2.833333
c . . 1.833333
a . . .
问题:有什么方法可以禁用此行为,以便平等对待 skip-gram window 中的每个 term/unigram?也就是说,如果一个术语在语料库中两次出现在另一个术语的上下文 window 中,它应该在 TCM 矩阵中显示为“2”。
额外的问题:默认缩放是如何工作的?如果在最后一个示例中添加更多的 "a",那么 b-c 值似乎呈线性下降,而 b-a 值实际上增加了——尽管更多的出现或 "a" 看起来离 "b" 更远。
更新 2017-02-01
Pushed update to github - 现在您可以直接在 create_tcm
.
中指定权重向量
加权函数已定义here。
如果您需要 window 中每个术语的权重相等,则需要将权重函数调整为始终 return 1
(只需克隆 repo,更改函数定义并使用 [=13 从源构建包=] 或 R CMD build
):
inline float weighting_fun(uint32_t offset) {
return 1.0;
}
但是已经有几个人要求这个功能,我可能会在下一个版本中包含这个选项。
我正在尝试使用 R
中的 text2vec
包从语料库计算术语-术语共现矩阵(或 TCM)(因为它有一个很好的并行后端)。我遵循了 this tutorial,但在检查一些玩具示例时,我注意到 create_tcm
函数对术语共现值进行了某种缩放或加权。我知道它在内部使用 skip-grams,但文档没有提到它如何缩放它们 - 显然,更远的 terms/unigrams 权重较低。
这是一个例子:
tcmtest = function(sentences){
tokens <- space_tokenizer(sentences)
it = itoken(tokens, progressbar = FALSE)
vocab <- create_vocabulary(it, ngram = c(ngram_min = 1L, ngram_max = 1L))
vectorizer <- vocab_vectorizer(vocab, grow_dtm = FALSE, skip_grams_window = 5L)
return(create_tcm(it, vectorizer))
}
> tcmtest(c("a b", "a b c"))
3 x 3 sparse Matrix of class "dgTMatrix"
b c a
b . 1 2.0
c . . 0.5
a . . .
> tcmtest(c("a b", "c a b"))
3 x 3 sparse Matrix of class "dgTMatrix"
b c a
b . 0.5 2
c . . 1
a . . .
> tcmtest(c("a b", "c a a a b"))
3 x 3 sparse Matrix of class "dgTMatrix"
b c a
b . 0.25 2.833333
c . . 1.833333
a . . .
问题:有什么方法可以禁用此行为,以便平等对待 skip-gram window 中的每个 term/unigram?也就是说,如果一个术语在语料库中两次出现在另一个术语的上下文 window 中,它应该在 TCM 矩阵中显示为“2”。
额外的问题:默认缩放是如何工作的?如果在最后一个示例中添加更多的 "a",那么 b-c 值似乎呈线性下降,而 b-a 值实际上增加了——尽管更多的出现或 "a" 看起来离 "b" 更远。
更新 2017-02-01
Pushed update to github - 现在您可以直接在 create_tcm
.
加权函数已定义here。
如果您需要 window 中每个术语的权重相等,则需要将权重函数调整为始终 return 1
(只需克隆 repo,更改函数定义并使用 [=13 从源构建包=] 或 R CMD build
):
inline float weighting_fun(uint32_t offset) {
return 1.0;
}
但是已经有几个人要求这个功能,我可能会在下一个版本中包含这个选项。