我正在尝试解决我自己关于更改tm
包中的 tf-idf 加权函数的问题:https ://stackoverflow.com/questions/15045313/changeing-tf-idf-weight-function-weight-not-by-occurrences-长期但按数量
在这样做时,我正在查看weightTfIdf
包含以下代码的函数m
,即 TermDocumentMatrix。
cs <- col_sums(m)
和
rs <- row_sums(m)
但我找不到函数的任何文档row_sums
或col_sums
;当我尝试使用它们编写自己的加权函数时,出现错误:Error in weighting(x) : could not find function "col_sums"
这些函数在哪里定义?
我从R
下面粘贴了完整的功能信息:
function (m, normalize = TRUE)
{
isDTM <- inherits(m, "DocumentTermMatrix")
if (isDTM)
m <- t(m)
if (normalize) {
cs <- col_sums(m)
if (any(cs == 0))
warning("empty document(s): ", paste(Docs(m)[cs ==
0], collapse = " "))
names(cs) <- seq_len(nDocs(m))
m$v <- m$v/cs[m$j]
}
rs <- row_sums(m > 0)
if (any(rs == 0))
warning("unreferenced term(s): ", paste(Terms(m)[rs ==
0], collapse = " "))
lnrs <- log2(nDocs(m)/rs)
lnrs[!is.finite(lnrs)] <- 0
m <- m * lnrs
attr(m, "Weighting") <- c(sprintf("%s%s", "term frequency - inverse document frequency",
if (normalize) " (normalized)" else ""), "tf-idf")
if (isDTM)
t(m)
else m
}
<environment: namespace:tm>
attr(,"class")
[1] "WeightFunction" "function"
attr(,"Name")
[1] "term frequency - inverse document frequency"
attr(,"Acronym")
[1] "tf-idf"