3

我正在尝试解决我自己关于更改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_sumscol_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"
4

1 回答 1

15

您正在寻找的功能在slam包中。由于slam只是导入而不是依赖项,因此查看文档需要做一些工作。这是一个示例会话,说明如何解决这个问题并查看文档。

> # I'm assuming you loaded tm first
> library(tm)
> # See if we can view the code
> col_sums
Error: object 'col_sums' not found
> # Use getAnywhere to grab the function even if the function is 
> # in a namespace that isn't exported
> getAnywhere("col_sums")
A single object matching ‘col_sums’ was found
It was found in the following places
  namespace:slam
with value

function (x, na.rm = FALSE, dims = 1, ...) 
UseMethod("col_sums")
<environment: namespace:slam>
> # So the function is in the slam package
> slam::col_sums
function (x, na.rm = FALSE, dims = 1, ...) 
UseMethod("col_sums")
<environment: namespace:slam>
> # We can tell help to look in the slam package now that we know
> # where the function is from    
> help(col_sums, package = "slam")
> # alternatively
> library(slam)
> ?col_sums
> # If we want to view the actual code for col_sums we need to 
> # do a little work too
> methods("col_sums")
[1] col_sums.default*               col_sums.simple_triplet_matrix*

   Non-visible functions are asterisked
> # We probably want the default version?  Otherwise change to the other one
> getAnywhere("col_sums.default")
A single object matching ‘col_sums.default’ was found
It was found in the following places
  registered S3 method for col_sums from namespace slam
  namespace:slam
with value

function (x, na.rm = FALSE, dims = 1, ...) 
base:::colSums(x, na.rm, dims, ...)
<environment: namespace:slam>

所以这个col_sums函数只是基本函数 colSums 的一个包装器。

于 2013-02-24T19:40:39.073 回答