如何在tm包中查看weightSMART函数的源代码?

时间:2017-08-08 16:37:53

标签: r text-mining devtools

我想在我的代码中使用weightSMART()的源代码,而不是使用该函数来编辑函数的默认权重。如何查看weightSMART()的源代码?当我尝试" weightSMART"和getAnywhere(weightSMART())在控制台中显示它。

> getAnywhere(weightSMART())
A single object matching ‘weightSMART’ was found
It was found in the following places
package:tm
namespace:tm
with value

function (m, spec = "nnn", control = list()) 
{
stopifnot(inherits(m, c("DocumentTermMatrix", "TermDocumentMatrix")), 
    is.character(spec), nchar(spec) == 3L, is.list(control))
term_frequency <- match.arg(substr(spec, 1L, 1L), c("n", 
    "l", "a", "b", "L"))
document_frequency <- match.arg(substr(spec, 2L, 2L), c("n", 
    "t", "p"))
normalization <- match.arg(substr(spec, 3L, 3L), c("n", "c", 
    "u", "b"))
isDTM <- inherits(m, "DocumentTermMatrix")
if (isDTM) 
    m <- t(m)
if (normalization == "b") {
    charlengths <- tapply(nchar(Terms(m))[m$i] * m$v, m$j, 
        sum)
}
m$v <- switch(term_frequency, n = m$v, l = 1 + log2(m$v), 
    a = {
        s <- tapply(m$v, m$j, max)
        0.5 + (0.5 * m$v)/s[as.character(m$j)]
    }, b = as.numeric(m$v > 0), L = {
        s <- tapply(m$v, m$j, mean)
        ((1 + log2(m$v))/(1 + log2(s[as.character(m$j)])))
    })
rs <- row_sums(m > 0)
if (any(rs == 0)) 
    warning("unreferenced term(s): ", paste(Terms(m)[rs == 
        0], collapse = " "))
df <- switch(document_frequency, n = 1, t = log2(nDocs(m)/rs), 
    p = max(0, log2((nDocs(m) - rs)/rs)))
df[!is.finite(df)] <- 0
cs <- col_sums(m)
if (any(cs == 0)) 
    warning("empty document(s): ", paste(Docs(m)[cs == 0], 
 collapse = " "))
norm <- switch(normalization, n = rep.int(1, nDocs(m)), c =     sqrt(col_sums(m^2)), 
    u = {
        if (is.null(pivot <- control$pivot)) stop("invalid control   argument pivot")
        if (is.null(slope <- control$slope)) stop("invalid control argument slope")
        (slope * sqrt(col_sums(m^2)) + (1 - slope) * pivot)
    }, b = {
        if (is.null(alpha <- control$alpha)) stop("invalid control argument alpha")
        norm <- double(nDocs(m))
        norm[match(names(charlengths), seq_along(norm))] <- charlengths^alpha
        norm
    })
   m <- m * df
   m$v <- m$v/norm[m$j]
   attr(m, "weighting") <- c(paste("SMART", spec), "SMART")
   if (isDTM) 
      t(m)
  else m
  }
<bytecode: 0x12ffe9c08>
<environment: namespace:tm>
attr(,"class")
[1] "WeightFunction" "function"      
attr(,"name")
[1] "SMART"
attr(,"acronym")
[1] "SMART"

这是源代码吗?

0 个答案:

没有答案
相关问题