1
# generic methods -----------
2

3
#' print method for summary.textmodel
4
#'
5
#' @param x a `summary.textmodel` object
6
#' @param digits minimal number of *significant digits*, see
7
#'   [print.default()]
8
#' @param ... additional arguments not used
9
#' @method print summary.textmodel
10
#' @importFrom stringi stri_trans_totitle stri_replace_all_fixed
11
#'   stri_detect_fixed
12
#' @keywords textmodel internal
13
#' @export
14
print.summary.textmodel <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
15 1
    label <- stri_trans_totitle(stri_replace_all_fixed(names(x), ".", " "))
16
    # print the formatted remaining elements
17 1
    for (i in seq_along(x)) {
18 1
        cat("\n")
19 1
        cat(label[i], ':\n', sep = '')
20 1
        if (stri_detect_fixed(label[i], "Feature")) {
21 1
            print(t(x[[i]]), digits = digits)
22
        } else {
23 1
            print(x[[i]], digits = digits)
24
        }
25

26
    }
27
}
28

29
#' Assign the summary.textmodel class to a list
30
#' @param x a named list
31
#' @keywords internal
32
#' @export
33
as.summary.textmodel <- function(x) {
34 1
    class(x) <- c("summary.textmodel", "list")
35 1
    x
36
}
37

38
#
39
# #' Print methods for textmodel features estimates
40
# #'
41
# #' @param x a textmodel_features object
42
# #' @param digits minimal number of \emph{significant digits}, see
43
# #'   \code{\link{print.default}}
44
# #' @param n how many coefficients to print before truncating
45
# #' @param ... additional arguments not used
46
# #' @method print coef.textmodel
47
# #' @export
48
# print.coef.textmodel <- function(x, digits = max(3L, getOption("digits") - 3L), n = 30L, ...) {
49
#     x <- unclass(x)
50
#     if (length(x) > n)
51
#         cat("(showing first", length(x), "elements)\n")
52
#     NextMethod(digits = digits)
53
# }
54
#
55
# #' Assign the textmodel_coefficients class to a numeric vector
56
# #' @param x a numeric vector
57
# #' @keywords internal
58
# as.coef.textmodel <- function(x) {
59
#     class(x) <- c("coef.textmodel", "numeric")
60
#     return(x)
61
# }
62

63
#' Print methods for textmodel features estimates
64

65
#' This is a helper function used in `print.summary.textmodel`.
66
#' @param x a coefficients_textmodel object
67
#' @param digits minimal number of *significant digits*, see
68
#'   [print.default()]
69
#' @param ... additional arguments not used
70
#' @method print coefficients_textmodel
71
#' @keywords internal textmodel
72
#' @export
73
print.coefficients_textmodel <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
74 1
    if (is.data.frame(x)) {
75 0
        n <- nrow(x)
76 0
        x <- as.data.frame(x)
77
    } else {
78 1
        n <- length(x)
79 1
        x <- unclass(x)
80
    }
81 1
    cat("(showing first", n, "elements)\n")
82 1
    print(x, digits = digits)
83
}
84

85
#' Coerce various objects to coefficients_textmodel
86

87
#' This is a helper function used in `summary.textmodel_*`.
88
#' @param x an object to be coerced
89
#' @importFrom stats coefficients
90
#' @importFrom stats coef
91
#' @keywords internal
92
#' @export
93
as.coefficients_textmodel <- function(x) {
94 1
    UseMethod('as.coefficients_textmodel')
95
}
96

97
#' @noRd
98
#' @method as.coefficients_textmodel data.frame
99
#' @keywords internal
100
#' @export
101
as.coefficients_textmodel.data.frame <- function(x) {
102 1
    class(x) <- c("coefficients_textmodel", "data.frame")
103 1
    return(x)
104
}
105

106
#' @noRd
107
#' @method as.coefficients_textmodel numeric
108
#' @keywords internal
109
#' @export
110
as.coefficients_textmodel.numeric <- function(x) {
111 1
    class(x) <- c("coefficients_textmodel", "numeric")
112 1
    return(x)
113
}
114

115
#' @noRd
116
#' @method as.coefficients_textmodel numeric
117
#' @keywords internal
118
#' @export
119
as.coefficients_textmodel.matrix <- function(x) {
120 1
    as.coefficients_textmodel(as.data.frame(x))
121
}
122

123
#' Implements print methods for textmodel_statistics
124
#'
125
#' @param x a textmodel_wordscore_statistics object
126
#' @param digits minimal number of *significant digits*, see
127
#'   [print.default()]
128
#' @param ... further arguments passed to or from other methods
129
#' @method print statistics_textmodel
130
#' @keywords internal textmodel
131
#' @export
132
print.statistics_textmodel <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
133 1
    NextMethod(digits = digits, row.names = TRUE)
134
}
135

136
#' Coerce various objects to statistics_textmodel
137
#'
138
#' This is a helper function used in `summary.textmodel_*`.
139
#' @param x an object to be coerced
140
#' @keywords internal textmodel
141
#' @export
142
as.statistics_textmodel <- function(x) {
143 1
    UseMethod("as.statistics_textmodel")
144
}
145

146
#' @noRd
147
#' @method as.statistics_textmodel data.frame
148
#' @keywords internal textmodel
149
#' @export
150
as.statistics_textmodel.data.frame <- function(x) {
151 1
    class(x) <- c("statistics_textmodel", "data.frame")
152 1
    return(x)
153
}
154

155
#' @noRd
156
#' @method as.statistics_textmodel matrix
157
#' @keywords internal textmodel
158
#' @export
159
as.statistics_textmodel.matrix <- function(x) {
160 0
    as.statistics_textmodel(as.data.frame(x))
161
}
162

163
# extension of quanteda methods ---------------
164

165
#' @export
166 1
ndoc.textmodel <- function(x)
167 1
    ndoc(x$x)
168

169
#' @export
170 1
nfeat.textmodel <- function(x)
171 1
    nfeat(x$x)
172

173
#' @export
174 1
docnames.textmodel <- function(x)
175 1
    docnames(x$x)
176

177
#' @export
178 1
featnames.textmodel <- function(x)
179 1
    featnames(x$x)

Read our documentation on viewing source code .

Loading