Reputation: 23
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?
Edit- in other words, I would like to return a summary object of the following function and format it like a summary.
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)
}
I have tried
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))))
}
Upvotes: 2
Views: 1627
Reputation: 99341
I think this will get you started. Here is a slightly modified version of your function, just added the class myClass
to the result.
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)
}
And the print and summary methods (really simplified example).
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))))
}
And then a test run using a random sample of popular words
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
Upvotes: 2
Reputation: 24565
I am not sure if this is what you want:
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
Or if you want a data frame:
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
Upvotes: 0