File: meta.R

package info (click to toggle)
r-cran-tm 0.7-8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,420 kB
  • sloc: xml: 3,365; ansic: 177; cpp: 108; sh: 17; makefile: 2
file content (164 lines) | stat: -rw-r--r-- 4,537 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Author: Ingo Feinerer

TextDocumentMeta <-
function(author, datetimestamp, description, heading, id, language, origin, ...,
         meta = NULL)
{
    if (is.null(meta))
        meta <- list(author = author, datetimestamp = datetimestamp,
                     description = description, heading = heading, id = id,
                     language = language, origin = origin, ...)

    stopifnot(is.list(meta))
    if (!is.null(meta$author) && !inherits(meta$author, "person"))
        meta$author <- as.character(meta$author)
    if (!is.null(meta$datetimestamp) && !inherits(meta$datetimestamp, "POSIXt"))
        meta$datetimestamp <- as.character(meta$datetimestamp)
    if (!is.null(meta$description))
       meta$description <- as.character(meta$description)
    if (!is.null(meta$heading))
       meta$heading <- as.character(meta$heading)
    if (!is.null(meta$id))
       meta$id <- as.character(meta$id)
    if (!is.null(meta$language))
       meta$language <- as.character(meta$language)
    if (!is.null(meta$origin))
       meta$origin <- as.character(meta$origin)

    class(meta) <- "TextDocumentMeta"
    meta
}

print.TextDocumentMeta <-
function(x, ...)
{
    cat(sprintf("  %s: %s",
                format(names(x), justify = "left"),
                sapply(x, as.character)),
        sep = "\n")
    invisible(x)
}

CorpusMeta <-
function(..., meta = NULL)
{
    if (is.null(meta))
        meta <- list(...)

    stopifnot(is.list(meta))

    class(meta) <- "CorpusMeta"
    meta
}

meta.SimpleCorpus <-
function(x, tag = NULL, type = c("indexed", "corpus"), ...)
{
    if (identical(tag, "id")) {
        n <- names(content(x))
        return(if (is.null(n)) as.character(seq_along(x)) else n)
    }

    if (!is.null(tag) && missing(type))
        type <- if (tag %in% names(x$meta)) "corpus" else "indexed"
    type <- match.arg(type)
    if (identical(type, "indexed"))
        if (is.null(tag)) x$dmeta else x$dmeta[tag]
    else if (identical(type, "corpus"))
        if (is.null(tag)) x$meta else x$meta[[tag]]
    else
        stop("invalid type")
}

meta.VCorpus <- meta.PCorpus <-
function(x, tag = NULL, type = c("indexed", "corpus", "local"), ...)
{
    if (!is.null(tag) && missing(type)) {
        type <- if (tag %in% names(x$dmeta)) "indexed"
        else if (tag %in% names(x$meta)) "corpus"
        else "local"
    }
    type <- match.arg(type)
    if (identical(type, "indexed"))
        if (is.null(tag)) x$dmeta else x$dmeta[tag]
    else if (identical(type, "corpus"))
        if (is.null(tag)) x$meta else x$meta[[tag]]
    else if (identical(type, "local"))
        lapply(x, meta, tag)
    else
        stop("invalid type")
}

`meta<-.SimpleCorpus` <-
function(x, tag, type = c("indexed", "corpus"), ..., value)
{
    type <- match.arg(type)
    if (identical(type, "indexed"))
        x$dmeta[, tag] <- value
    else if (type == "corpus")
        x$meta[[tag]] <- value
    else
        stop("invalid type")
    x
}

`meta<-.VCorpus` <- `meta<-.PCorpus` <-
function(x, tag, type = c("indexed", "corpus", "local"), ..., value)
{
    type <- match.arg(type)
    if (identical(type, "indexed"))
        x$dmeta[, tag] <- value
    else if (type == "corpus")
        x$meta[[tag]] <- value
    else if (identical(type, "local")) {
        for (i in seq_along(x))
            meta(x[[i]], tag) <- value[i]
    } else
        stop("invalid type")
    x
}

# Simple Dublin Core to tm metadata mapping
# http://en.wikipedia.org/wiki/Dublin_core#Simple_Dublin_Core
Dublin_Core_tm_map <-
list("contributor" = "contributor",
     "coverage" = "coverage",
     "creator" = "author",
     "date" = "datetimestamp",
     "description" = "description",
     "format" = "format",
     "identifier" = "id",
     "language" = "language",
     "publisher" = "publisher",
     "relation" = "relation",
     "rights" = "rights",
     "source" = "source", # or better "origin"?
     "subject" = "subject",
     "title" = "heading",
     "type" = "type"
     )

DublinCore <-
function(x, tag = NULL)
{
    tmm <- unlist(Dublin_Core_tm_map, use.names = FALSE)
    dcm <- names(Dublin_Core_tm_map)

    if (is.null(tag)) {
        m <- lapply(tmm, function(t) meta(x, t))
        names(m) <- dcm
        class(m) <- "TextDocumentMeta"
        m
    } else
        meta(x, tmm[charmatch(tolower(tag), dcm)])
}

`DublinCore<-` <-
function(x, tag, value)
{
    tmm <- unlist(Dublin_Core_tm_map, use.names = FALSE)
    dcm <- names(Dublin_Core_tm_map)

    meta(x, tmm[charmatch(tolower(tag), dcm)]) <- value
    x
}