2014-11-03 134 views
2

我将如何尝试为以下函数编写摘要方法,使用它来查找文本块中的单词总数和文本中使用的最频繁的单词?函数的摘要方法

编辑 - 换句话说,我想返回以下函数的摘要对象并将其格式化为摘要。

findwords = function(tf) { 
    txt = unlist(strsplit(tf,' ')) 
    wl = list() 
    for(i in 1:length(txt)) { 
    wrd = txt[i] 
    wl[[wrd]] = c(wl[[wrd]],i) 
    } 
    return(wl) 
} 

我已经试过

summary.findwords = function(obj) { 
txt = unlist(strsplit(obj,' ')) 
cat(“the total number of words\n”) 
print(length(txt)) 
cat(“the frequency of words\n”) 
print(rev(sort(table(txt)))) 
} 
+0

你的意思是你想返回一个'summary'对象和格式化它像一个总结的随机样本? – 2014-11-03 17:08:22

+0

是的。我很抱歉的混淆。这个问题我很不清楚。我找不到很多解释它的信息。但是,是的,这是我想尝试做 – ronan10 2014-11-03 17:11:48

回答

1

我认为这会让你开始。这里是你的函数的一个稍微修改过的版本,只是将类myClass添加到结果中。

findwords = function(tf) { 
    txt = unlist(strsplit(tf,' ')) 
    wl = list() 
    for(i in seq_along(txt)) { 
     wrd = txt[i] 
     wl[[wrd]] = c(wl[[wrd]], i) 
    } 
    class(wl) <- "myClass" 
    return(wl) 
} 

和打印和汇总方法(真的简化的例子)。

print.myClass <- function(x, ...){ 
    cl <- oldClass(x) 
    oldClass(x) <- cl[cl != "myClass"] 
    NextMethod("print") 
    invisible(x) 
} 

summary.myClass <- function(x) { 
    stopifnot(inherits(x, "myClass")) 
    cat("\t\n", 
     sprintf("Unique Words (Length): %s\n", length(x)), 
     sprintf("Total Words: %s", sum(sapply(x, length)))) 
} 

然后测试运行使用的流行词

library(qdapDictionaries) 
data(Top25Words) 
samp <- paste(sample(Top25Words, 200, TRUE), collapse = " ") 
fw <- findwords(samp) 
class(fw) 
# [1] "myClass" 
head(fw, 3) 
# $that 
# [1] 1 36 54 63 76 165 182 191 
# 
# $the 
# [1] 2 68 70 92 97 132 151 168 186 
# 
# $they 
# [1] 3 75 199 

summary(fw) 

# Unique Words (Length): 25 
# Total Words: 200 
+0

谢谢,这真的很有帮助! :)在这里还有一件事我不确定,为什么你要运行函数'summary(findwords(samp))'而不是summary.myClass(findwords(samp))? – ronan10 2014-11-03 18:56:47

+0

因为我们已经为类“myClass”定义了一个方法。所以,只要'fw'是类“myClass”,我们就永远不会调用'summary.myClass'。 'summary'会根据其'x'参数的类别发送到正确的方法 – 2014-11-03 18:59:17

+0

(+1)好的OO技巧,我从来没有这样编程过... – 2014-11-03 19:01:38

0

我不知道这是否是你想要什么:

str = "How would I attempt to write a Summary Method for the following function, using it to find the total number of words in a block of text and the most frequent words used in the text" 

ll = unlist(strsplit(str, ' ')) 
length(ll) 
[1] 36 

rev(sort(table(ll))) 
ll 
     the  words  to  text  of  in   a  write  would  using  used  total Summary 
     4   2   2   2   2   2   2   1   1   1   1   1   1 
    number  most Method  it   I  How function, frequent  for following  find  block attempt 
     1   1   1   1   1   1   1   1   1   1   1   1   1 
     and 
     1 

或者,如果你想有一个数据帧:

data.frame(rev(sort(table(ll)))) 
     rev.sort.table.ll... 
the       4 
words      2 
to       2 
text       2 
of       2 
in       2 
a       2 
write      1 
would      1 
using      1 
used       1 
total      1 
Summary      1 
number      1 
most       1 
Method      1 
it       1 
I       1 
How       1 
function,     1 
frequent      1 
for       1 
following     1 
find       1 
block      1 
attempt      1 
and       1 
+0

嗨@mso谢谢你的回答。它看起来是正确的,但说实话,我不完全确定一个“总结方法”是由什么组成的。从我看到的其他例子来看,它们具有共同的函数cat()和print()。我很乐意接受你的回答我只是不完全确定自己究竟应该是什么样子 – ronan10 2014-11-03 17:08:11

+0

你是否被要求在作业中写一个“总结方法”? – rnso 2014-11-03 17:36:30

+0

这只是一个示例问题 - 我通过在线课程学习R,我觉得我没有人问。我真的不明白,当它要求为该功能的总结方法 – ronan10 2014-11-03 17:49:24