2014-11-03 132 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